:root {
  color-scheme: light;
  --ink: #14261c;
  --product-name: #24312b;
  --primary-metric: #0b6a34;
  --secondary-metric: #2f5f96;
  --muted-meta: #66756c;
  --muted: var(--muted-meta);
  --line: #d5e0da;
  --bg: #f3f6f4;
  --green: #0e7a45;
  font-family: "Segoe UI", "PingFang SC", "Microsoft JhengHei", sans-serif;
  font-size: 13px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  color: var(--ink);
  background: var(--bg);
  padding-bottom: 32px;
  touch-action: manipulation;
}
button, input, select { font: inherit; }
button { cursor: pointer; }
h1, h2, p { margin: 0; }

.top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 8px 2px;
}
h1 { font-size: 15px; line-height: 1.2; }
.asof { color: var(--muted); font-size: 11px; }

.supplier, .tools, #products, .add-box, .mapping, #status-banner {
  width: min(1100px, 100%);
  margin-inline: auto;
  padding-inline: 8px;
}
.supplier-line, .tools-row, .add-actions, .dialog-head, .dialog-actions, .dock {
  display: flex;
  align-items: center;
  gap: 6px;
}
#workspace, #catalog-panel, .supplier { overflow: visible; }
input, select {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 2px 6px;
  min-height: 26px;
}
.supplier-line input, .tools-row input, .add-actions input { flex: 1; min-width: 0; }
#supplier-search {
  min-height: 48px;
  font-size: 18px;
  padding: 8px 12px;
}
#supplier-change, #supplier-clear {
  min-height: 48px;
  min-width: 64px;
  font-size: 16px;
  padding: 0 12px;
}
.tools {
  position: -webkit-sticky;
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 4;
  background: var(--bg);
  padding-top: 6px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--line);
}
.tools-row input { min-height: 40px; font-size: 16px; }
.tools-row select { flex: 0 1 108px; min-width: 0; min-height: 40px; }
.legend {
  color: var(--muted);
  font-size: 10px;
  line-height: 1.2;
  padding: 1px 8px 2px;
}
.picks { display: grid; gap: 4px; padding: 4px 8px 6px; }
.pick, .text-btn, #confirm, #review-open {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  min-height: 28px;
  padding: 2px 8px;
}
.pick { text-align: left; }
.pick strong { font-weight: 650; }
.supplier-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 1px;
  width: 100%;
  min-height: 52px;
  padding: 6px 12px;
}
.supplier-card strong {
  display: block;
  max-width: 100%;
  font-size: 18px;
  font-weight: 750;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.supplier-card .code {
  font-size: 13px;
  color: #7d8b83;
}
#confirm, #review-open {
  background: var(--green);
  color: #fff;
  border-color: var(--green);
  font-weight: 700;
}
.text-btn { background: #fff; }

.banner {
  margin: 2px 8px;
  padding: 3px 6px;
  border-radius: 6px;
  background: #fff6e8;
  color: #8a5a00;
  font-size: 11px;
}
/* Product card: name and barcode/stock on the left, quantity on the right, sales/purchase grid below. */
.product {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  column-gap: 6px;
  row-gap: 2px;
  align-items: start;
  padding: 3px 4px;
  border-bottom: 1px solid var(--line);
  line-height: 1.2;
  background: transparent;
}
.identity { grid-column: 1; grid-row: 1; min-width: 0; align-self: center; }
.code-line { display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 10px; min-width: 0; }
.code-line .item-code { flex: 0 1 auto; min-width: 0; }
/* Stays whole after the barcode; wraps to its own line only when the barcode line is full. */
.stock-inline {
  flex: none;
  white-space: nowrap;
  color: #5d6b63;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.25;
}
.stock-inline strong { color: #0b5a3a; font-size: 13px; font-weight: 800; }
.stock-inline.is-low strong { color: #c62828; }
.stock-inline.is-unknown strong { color: #8a938e; font-weight: 600; }
.item-code {
  display: flex;
  flex-wrap: wrap;
  column-gap: 6px;
  color: #66756c;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.2;
}
.item-barcode, .item-sku, .item-uom { overflow-wrap: anywhere; }
.product-name, .name {
  color: var(--product-name);
  font-size: 14px;
  font-weight: 650;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  overflow-wrap: anywhere;
  cursor: pointer;
}
.name.expanded { display: block; -webkit-line-clamp: unset; }
.src-slot:empty { display: none; }
.src-badge {
  display: inline-block;
  margin-top: 2px;
  padding: 1px 6px;
  border-radius: 999px;
  background: #e8efff;
  color: #1a4fa8;
  font-size: 11px;
  font-weight: 700;
  line-height: 1.5;
  white-space: nowrap;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}
.review-main .src-badge { display: inline-block; width: fit-content; }
.src-badge.is-manual { background: #eef0ee; color: #3d4a44; }
.src-badge.is-exported { background: #e7f6ee; color: #0b5c34; }
.supplier-card.is-pending {
  display: block;
  padding: 6px 8px;
  border: 1px solid #ecd27a;
  border-radius: 8px;
  background: #fff4c2;
}
.pc-open {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  text-align: left;
}
.pc-open strong { font-size: 17px; font-weight: 800; }
.pc-open .code { font-size: 13px; color: #5d5530; }
.pc-open .code b { color: #2f2a10; }
.pc-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.pc-actions button {
  min-height: 38px;
  padding: 0 10px;
  border: 1px solid #d9bf5c;
  border-radius: 6px;
  background: #fff;
  font-size: 13px;
  font-weight: 700;
}
.pc-actions [data-act="export"] { background: var(--green); border-color: var(--green); color: #fff; }
.pc-actions button:disabled { opacity: 0.45; }
.pc-message { margin-top: 4px; font-size: 12px; color: #5d5530; }
.po-clear {
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid #c9b25a;
  border-radius: 6px;
  background: #fff8dc;
  font-size: 12px;
  font-weight: 700;
}
.po-clear:disabled { opacity: 0.4; }
.po-status.is-pending { background: #fff4c2; color: #6b5400; }
.toast {
  position: fixed;
  left: 50%;
  bottom: 44px;
  transform: translateX(-50%);
  z-index: 6;
  max-width: min(92vw, 520px);
  padding: 8px 12px;
  border-radius: 8px;
  background: #12382a;
  color: #fff;
  font-size: 13px;
  line-height: 1.35;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
}
.pd-reset {
  min-width: 48px;
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  background: #5e6b64;
  color: #fff;
  font-size: 14px;
  font-weight: 700;
}
.item-name.in-draft { color: #c62828; }
.product.ordered-highlight { background: #fff6d6; }
.primary-metric {
  color: var(--primary-metric);
  font-size: 13px;
  font-weight: 750;
}
.secondary-metric {
  color: var(--secondary-metric);
  font-size: 12px;
  font-weight: 650;
}
.muted-meta, .code {
  color: var(--muted-meta);
  font-weight: 500;
  font-size: 10px;
}
.order {
  grid-column: 2;
  grid-row: 1;
  display: flex;
  align-items: center;
  align-self: center;
  gap: clamp(3px, 1vw, 6px);
  touch-action: manipulation;
}
/* Scales with the viewport: about 34 × 33 px on a 390 px phone, 42 × 36 px on a desktop. */
.step {
  min-width: clamp(34px, 9vw, 42px);
  height: clamp(32px, 8.5vw, 36px);
  border: 0;
  border-radius: 7px;
  color: #fff;
  font-size: clamp(13px, 3.4vw, 14px);
  font-weight: 750;
  padding: 0 4px;
  touch-action: manipulation;
}
.qty-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 2.2em;
  line-height: 1.05;
}
.qty-uom {
  max-width: 5.5em;
  color: var(--muted-meta);
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.clear-qty { background: #5e6b64; }
.plus { background: var(--green); }
.plus6 { background: #0b5c34; }
.step:disabled { opacity: 0.35; }
.qty {
  min-width: 22px;
  text-align: center;
  font-size: clamp(16px, 4.4vw, 19px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}
.po-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 4px 12px;
  margin: 6px 8px 2px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.po-supplier { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px; min-width: 0; }
.po-supplier strong { font-size: 18px; font-weight: 800; color: var(--product-name); overflow-wrap: anywhere; }
.po-supplier span { color: var(--muted); font-size: 12px; }
.po-stats { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; font-size: 13px; color: #3d4a44; }
.po-stats b { font-size: 16px; font-variant-numeric: tabular-nums; }
.po-status { padding: 1px 8px; border-radius: 999px; background: #f1f2f1; font-weight: 700; font-size: 12px; }
.po-status.is-confirmed { background: #e7f6ee; color: #0b5c34; }
/* Sales / purchase: label, 1 / 6 / 12 months, last trade. Fixed number columns line up from card to card. */
.mgrid {
  grid-column: 1 / -1;
  grid-row: 2;
  display: grid;
  grid-template-columns: 2.8em repeat(3, minmax(4.9em, max-content)) minmax(0, 1fr);
  border-top: 1px solid #dde4e0;
  border-left: 1px solid #dde4e0;
  border-radius: 3px;
  font-size: 12px;
  line-height: 15px;
  font-variant-numeric: tabular-nums;
}
.mc {
  border-right: 1px solid #dde4e0;
  border-bottom: 1px solid #dde4e0;
  padding: 0 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mh { background: #f2f5f3; font-weight: 700; text-align: center; padding: 0 1px; text-overflow: clip; }
.mh.s, .mn.s { color: #0b6a34; }
.mh.b, .mn.b { color: #1a56c4; }
.mn { text-align: right; font-weight: 700; }
.mc-na { color: #8a938e; font-weight: 600; }
/* Wraps instead of hiding part of the last trade on very narrow screens. */
.ml { color: #66756c; font-size: 11px; white-space: normal; overflow-wrap: anywhere; }
/* Factory groups: same barcode prefix. */
/* Factory groups: same barcode prefix. --fc / --fc-ink are set per prefix (fixed palette in app.js). */
.factory { --fc: #1d4ed8; --fc-ink: #fff; border-left: 6px solid var(--fc); margin: 6px 0 2px; }
.factory-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 8px;
  padding: 2px 6px;
  background: #f4f6f5;
  border-bottom: 1px solid var(--line);
  color: #24302a;
  font-size: 12px;
  line-height: 18px;
}
.fh-chip {
  display: inline-block;
  min-width: 5.2em;
  padding: 1px 12px;
  border-radius: 5px;
  background: var(--fc);
  color: var(--fc-ink);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.factory-head strong { font-weight: 800; }
.fh-count { color: #5d6b63; font-size: 11px; font-weight: 600; }
/* Same Factory Products: orange, so it never reads as part of the supplier's own list. */
.factory-toggle {
  display: block;
  width: 100%;
  min-height: 32px;
  padding: 4px 8px;
  border: 0;
  border-top: 1px solid #fdba74;
  border-bottom: 1px solid #fdba74;
  background: #fff1e0;
  color: #9a3412;
  font-size: 13px;
  font-weight: 800;
  text-align: left;
  touch-action: manipulation;
}
.factory-others .empty { padding: 3px 8px; background: #fff8ef; color: #9a3412; }
.product.is-other { background: #fff8ef; }
.product.is-other .item-name,
.product.is-other .item-name.in-draft { color: #c2410c; }
.product.is-other.ordered-highlight { background: #fff6d6; }
.add-box { margin-top: 6px; }
.net-block { margin: 8px 0; }
.net-url { font-size: 13px; color: var(--ink); word-break: break-all; }
.qr { margin-top: 4px; }
.mapping { margin: 6px auto 12px; font-size: 12px; }
.mapping p { margin: 4px 0; }
.muted { color: var(--muted); font-size: 12px; }
.error { color: #9d2c2c; font-weight: 700; }
.success { background: #e7f6ee; color: #0b5c34; border-radius: 6px; padding: 6px; }
.empty { color: var(--muted); font-size: 12px; padding: 4px 0; }
.dock {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 32px;
  background: #12382a;
  color: #fff;
  padding: 0 8px;
  justify-content: space-between;
  z-index: 3;
}
dialog {
  width: min(560px, calc(100% - 12px));
  border: 0;
  border-radius: 10px;
  padding: 8px;
}
dialog::backdrop { background: rgba(8, 20, 14, 0.55); }
.dialog-head, .dialog-actions { justify-content: space-between; margin-bottom: 6px; }
.review-line {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 2.4em minmax(2.8em, 5.6em);
  align-items: center;
  column-gap: 8px;
  padding: 1px 0;
  border-top: 1px solid var(--line);
  min-height: 28px;
}
.review-main { min-width: 0; }
.review-main strong, .review-main .code {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.review-main .item-code {
  white-space: normal;
  overflow: visible;
}
.review-main strong { font-size: 12px; font-weight: 650; line-height: 1.15; }
.review-num, .review-uom {
  align-self: stretch;
  display: flex;
  align-items: center;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.review-num {
  justify-content: flex-end;
  font-size: 20px;
  font-weight: 800;
}
.review-uom {
  justify-content: flex-start;
  font-size: 15px;
  font-weight: 750;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.review-line .ghost { justify-self: end; min-height: 22px; padding: 0 6px; }
.review-meta {
  margin: 0 0 4px;
  font-size: 12px;
  line-height: 1.3;
  color: #3d4a44;
  overflow-wrap: anywhere;
}
.review-meta.is-bad { color: #9d2c2c; }
.price-input {
  width: 6.5em;
  min-height: 32px;
  margin-left: 4px;
  font-size: 14px;
}
.dialog-actions { flex-wrap: wrap; }
#export-file { flex: 1 1 100%; min-height: 44px; }
.check { display: flex; gap: 6px; align-items: center; margin: 8px 0; }
.scan {
  position: fixed;
  inset: 0;
  background: rgba(8, 20, 14, 0.72);
  display: grid;
  place-items: center;
  padding: 8px;
  z-index: 5;
}
.scan-card { width: min(480px, 100%); background: #fff; border-radius: 10px; padding: 8px; }
.scan-view {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  max-height: 56vh;
  overflow: hidden;
  border-radius: 8px;
  background: #111;
}
.scan-view video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.scan-guide {
  position: absolute;
  left: 8%;
  right: 8%;
  top: 28%;
  height: 44%;
  border: 3px solid rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.35);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.scan-line {
  position: absolute;
  left: 4%;
  right: 4%;
  top: 50%;
  height: 2px;
  background: #ff3b30;
  box-shadow: 0 0 6px #ff3b30;
  animation: scan-sweep 1.8s ease-in-out infinite;
}
@keyframes scan-sweep {
  0%, 100% { transform: translateY(-40%); }
  50% { transform: translateY(40%); }
}
.scan.is-found .scan-guide { border-color: #34c759; box-shadow: 0 0 0 100vmax rgba(52, 199, 89, 0.25); }
.scan.is-found .scan-line { display: none; }
.scan-tools { display: flex; justify-content: flex-end; min-height: 4px; margin-top: 6px; }
#scan-torch { min-height: 40px; }
#scan-torch[aria-pressed="true"] { background: #fff6d6; }
#scan-message { margin-top: 6px; font-size: 15px; line-height: 1.4; }
#scan-message.is-error { color: #9d2c2c; font-weight: 650; }
.scan-manual { display: flex; gap: 6px; margin-top: 8px; }
.scan-manual input { flex: 1; min-width: 0; min-height: 44px; font-size: 17px; }
.scan-manual button { min-height: 44px; min-width: 64px; }
@media (prefers-reduced-motion: reduce) { .scan-line { animation: none; } }

.tabs {
  display: flex;
  gap: 4px;
  width: min(1100px, 100%);
  margin-inline: auto;
  padding: 2px 8px 4px;
}
.tab {
  flex: 1 1 0;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  font-size: 15px;
  font-weight: 700;
}
.tab[aria-selected="true"] { background: var(--green); border-color: var(--green); color: #fff; }
body[data-tab="product"] #dock,
body[data-tab="sales"] #dock { display: none !important; }

.pd { width: min(1100px, 100%); margin-inline: auto; padding: 0 8px 16px; }
.pd-search {
  position: -webkit-sticky;
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 4;
  display: flex;
  gap: 6px;
  padding: 6px 0;
  background: var(--bg);
}
.pd-search input { flex: 1; min-width: 0; min-height: 48px; font-size: 18px; padding: 6px 12px; }
#pd-scan { min-height: 48px; min-width: 72px; font-size: 16px; }
#pd-message { padding: 2px 0; font-size: 14px; }
.pd-pick { display: flex; flex-direction: column; align-items: flex-start; min-height: 48px; padding: 4px 10px; }
.pd-pick strong { font-size: 15px; }
.pd-pick .code { font-size: 12px; }
.pd-grid {
  display: grid;
  grid-template-columns: minmax(240px, 300px) minmax(0, 1fr);
  grid-template-areas:
    "sales main"
    "history history"
    "label label";
  gap: 10px;
  align-items: start;
  margin-top: 6px;
}
.pd-main { grid-area: main; min-width: 0; }
.pd-sales { grid-area: sales; background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 8px; }
.pd-label { grid-area: label; min-width: 0; }
.pd-sales h2, .pd-label h2 { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.pd-name { font-size: 20px; line-height: 1.25; color: var(--product-name); }
.pd-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.pd-facts { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 6px 0; font-size: 14px; }
.pd-facts dt { color: var(--muted); }
.pd-facts dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
#pd-units { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 6px; }
.pd-unit { min-height: 40px; min-width: 64px; border: 1px solid var(--line); border-radius: 6px; background: #fff; font-weight: 700; }
.pd-unit[aria-pressed="true"] { background: #e7f3ec; color: var(--green); border-color: var(--green); }
.pd-price1 {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 6px 10px;
  border-radius: 8px;
  background: #fff6d6;
}
.pd-price1 span { font-size: 14px; color: var(--muted); font-weight: 700; }
.pd-price1 strong { font-size: 28px; font-weight: 800; font-variant-numeric: tabular-nums; }
.pd-price-table { margin-top: 6px; border-collapse: collapse; font-size: 14px; }
.pd-price-table th { text-align: left; color: var(--muted); font-weight: 600; padding: 2px 16px 2px 0; }
.pd-price-table td { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.pd-print { margin-top: 10px; }
#pd-print {
  min-height: 48px;
  padding: 0 18px;
  border: 0;
  border-radius: 8px;
  background: #12382a;
  color: #fff;
  font-size: 16px;
  font-weight: 750;
}
#pd-print:disabled { opacity: 0.5; }
#pd-print-status { margin-top: 4px; font-size: 13px; }
.pd-sales-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.pd-sales-table th, .pd-sales-table td { padding: 5px 2px; border-top: 1px solid var(--line); vertical-align: top; }
.pd-sales-table thead th { border-top: 0; color: var(--muted); font-size: 12px; font-weight: 600; text-align: right; }
.pd-sales-table thead th:first-child { text-align: left; }
.pd-sales-table tbody th { text-align: left; font-size: 14px; }
.pd-sales-table tbody th span { display: block; color: var(--muted); font-size: 11px; font-weight: 500; }
.pd-sales-table td { text-align: right; font-size: 14px; font-weight: 700; color: #0b6a34; white-space: nowrap; }
.pd-sales-table td .error { display: block; font-size: 11px; }
.pd-sales .muted { margin-top: 6px; font-size: 11px; }
#pd-label-note { margin-top: 4px; font-size: 12px; overflow-wrap: anywhere; }
.pd-cost {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-top: 6px;
  padding: 4px 10px;
  border: 1px dashed #b7c4bd;
  border-radius: 8px;
  font-size: 14px;
}
.pd-cost span { color: var(--muted); font-weight: 700; }
.pd-cost strong { font-size: 18px; font-variant-numeric: tabular-nums; color: #1a56c4; }
.pd-cost em { color: var(--muted); font-style: normal; font-size: 12px; }

.pd-history { grid-area: history; min-width: 0; background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 8px; }
.pd-history-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.pd-history-head h2 { font-size: 13px; color: var(--muted); }
#pd-history-open { min-height: 40px; font-weight: 700; color: #1a56c4; }
.pd-history-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.pd-history-table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.pd-history-table th, .pd-history-table td { padding: 5px 4px; border-top: 1px solid var(--line); vertical-align: top; text-align: left; }
.pd-history-table thead th { border-top: 0; color: var(--muted); font-size: 12px; font-weight: 600; white-space: nowrap; }
.pd-history-table .num { text-align: right; white-space: nowrap; }
.pd-history-table .date { white-space: nowrap; font-weight: 700; }
.pd-history-table .supplier { min-width: 9em; }
.pd-history-table .doc { white-space: nowrap; }
.pd-history-table .doc span, .pd-history-table .supplier span { display: block; color: var(--muted); font-size: 11px; }
.pd-history-table .cost { color: #1a56c4; font-weight: 700; }
.pd-history-table tr.is-return td { color: #9d2c2c; }
.pd-history-dialog { width: min(980px, calc(100% - 12px)); max-height: calc(100vh - 24px); }
.pd-history-tools { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
#pd-history-sort { min-height: 40px; }
.pd-history-dialog .pd-history-scroll { max-height: calc(100vh - 140px); overflow-y: auto; }
.pd-history-dialog thead th { position: sticky; top: 0; background: #fff; }

@media (max-width: 640px) {
  .pd-grid {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "main" "sales" "history" "label";
  }
}

.pd-order {
  margin-top: 10px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.pd-order h2 { font-size: 14px; margin-bottom: 4px; }
.pd-order-product { display: flex; flex-direction: column; gap: 1px; font-size: 13px; color: #3d4a44; }
.pd-order-product strong { font-size: 15px; color: var(--product-name); }
.pd-order-field { display: block; margin-top: 8px; font-size: 12px; color: var(--muted); font-weight: 600; }
#pd-order-supplier { width: 100%; min-height: 44px; font-size: 15px; margin-top: 2px; }
#pd-order-supplier-note { margin-top: 4px; font-size: 12px; }
.pd-order-qty { display: flex; align-items: center; gap: 6px; margin-top: 2px; }
#pd-order-qty { width: 6em; min-height: 44px; font-size: 20px; font-weight: 800; text-align: center; }
.pd-order-uom { font-weight: 700; font-size: 15px; min-width: 3em; }
.pd-step { min-width: 52px; min-height: 44px; border: 0; border-radius: 8px; background: var(--green); color: #fff; font-size: 15px; font-weight: 750; }
#pd-order-add {
  margin-top: 8px;
  width: 100%;
  min-height: 48px;
  border: 0;
  border-radius: 8px;
  background: var(--green);
  color: #fff;
  font-size: 16px;
  font-weight: 750;
}
#pd-order-add:disabled { opacity: 0.45; }
#pd-order-status { margin-top: 6px; font-size: 13px; }
#pd-order-status.success-text { color: #0b5c34; font-weight: 650; }

@media (min-width: 700px) {
  .name { font-size: 16px; }
}

@media (min-width: 800px) {
  .product { column-gap: 12px; }
  #supplier-search { min-height: 52px; font-size: 20px; }
  .supplier-card { min-height: 58px; }
  .supplier-card strong { font-size: 20px; }
}

/* Wide screens: the sales/purchase grid sits between the name and the quantity, one card per line. */
@media (min-width: 900px) {
  .product { grid-template-columns: minmax(0, 1fr) auto auto; padding: 3px 6px; }
  .mgrid {
    grid-column: 2;
    grid-row: 1;
    align-self: center;
    grid-template-columns: 2.8em repeat(3, 6.6em) 15em;
    font-size: 13px;
    line-height: 17px;
  }
  .ml { font-size: 12px; }
  .order { grid-column: 3; }
  .qty-box { width: 5.6em; }
}

/* iPhone: tighter spacing and type; tap targets stay at 40 px or more. */
@media (max-width: 480px) {
  .top { padding: 2px 8px 0; }
  .tabs { padding: 2px 6px 4px; gap: 4px; }
  .tab { min-height: 40px; font-size: 14px; }
  #supplier-search { min-height: 42px; font-size: 16px; padding: 6px 10px; }
  #supplier-change, #supplier-clear { min-height: 42px; min-width: 56px; font-size: 14px; }
  .picks { gap: 4px; padding: 2px 6px 4px; }
  .supplier-card { min-height: 44px; padding: 4px 10px; }
  .supplier-card strong { font-size: 15px; }
  .supplier-card .code { font-size: 12px; }
  .supplier-card.is-pending { padding: 5px 8px; }
  .pc-open strong { font-size: 15px; }
  .pc-open .code { font-size: 12px; }
  .pc-actions { gap: 4px; margin-top: 4px; }
  .pc-actions button { min-height: 36px; padding: 0 8px; font-size: 12px; }
  .po-header { margin: 4px 6px 2px; padding: 6px 8px; gap: 2px 8px; }
  .po-supplier strong { font-size: 15px; }
  .po-stats { font-size: 12px; gap: 2px 8px; }
  .po-stats b { font-size: 14px; }
  .tools-row input, .tools-row select { min-height: 38px; font-size: 15px; }
  .product { column-gap: 4px; padding: 3px 4px 3px 6px; }
  .name { font-size: 14px; -webkit-line-clamp: 3; }
  .stock-inline { font-size: 11px; }
  .stock-inline strong { font-size: 13px; }
  .item-code { font-size: 11px; }
  .src-badge { font-size: 10px; white-space: normal; }
  .qty-uom { font-size: 10px; }
  .mgrid { font-size: 11.5px; }
  .ml { font-size: 10.5px; }

  .pd { padding: 0 6px 12px; }
  .pd-search { padding: 4px 0; gap: 4px; }
  .pd-search input { min-height: 42px; font-size: 16px; padding: 4px 10px; }
  #pd-scan { min-height: 42px; min-width: 60px; font-size: 14px; }
  .pd-pick { min-height: 42px; padding: 3px 8px; }
  .pd-pick strong { font-size: 14px; }
  .pd-grid { gap: 8px; margin-top: 4px; }
  .pd-name { font-size: 16px; line-height: 1.2; }
  .pd-sub, .pd-facts { font-size: 12px; }
  .pd-facts { margin: 4px 0; gap: 1px 8px; }
  .pd-unit { min-height: 36px; min-width: 56px; font-size: 13px; }
  .pd-price1 { padding: 4px 8px; gap: 8px; }
  .pd-price1 span { font-size: 12px; }
  .pd-price1 strong { font-size: 22px; }
  .pd-price-table { font-size: 12px; margin-top: 4px; }
  .pd-price-table th { padding: 1px 12px 1px 0; }
  .pd-cost { margin-top: 4px; padding: 3px 8px; font-size: 12px; gap: 8px; }
  .pd-cost strong { font-size: 15px; }
  .pd-order { margin-top: 8px; padding: 6px 8px; }
  .pd-order h2 { font-size: 13px; }
  .pd-order-product { font-size: 12px; }
  .pd-order-product strong { font-size: 14px; }
  .pd-order-field { margin-top: 6px; font-size: 11px; }
  #pd-order-supplier { min-height: 40px; font-size: 14px; }
  .pd-order-qty { gap: 4px; }
  #pd-order-qty { width: 4.6em; min-height: 40px; font-size: 18px; }
  .pd-order-uom { font-size: 13px; min-width: 2.4em; }
  .pd-step, .pd-reset { min-width: 44px; min-height: 40px; font-size: 14px; }
  #pd-order-add { min-height: 44px; font-size: 15px; margin-top: 6px; }
  #pd-order-status { font-size: 12px; }
  .pd-print { margin-top: 8px; }
  #pd-print { min-height: 44px; font-size: 15px; padding: 0 14px; }
  .pd-sales, .pd-history { padding: 6px; }
  .pd-sales-table th, .pd-sales-table td { padding: 3px 2px; }
  .pd-sales-table tbody th { font-size: 13px; }
  .pd-sales-table td { font-size: 13px; }
  .pd-history-table { font-size: 12px; }
  .pd-history-table th, .pd-history-table td { padding: 3px 3px; }
  #pd-history-open { min-height: 36px; font-size: 12px; }
}

.pd-stock {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px 10px;
  margin: 4px 0;
  padding: 4px 10px;
  border-radius: 8px;
  background: #e8f1ff;
  font-size: 13px;
}
.pd-stock span { color: var(--muted); font-weight: 700; }
.pd-stock strong { font-size: 20px; font-weight: 800; color: #1a4fa8; font-variant-numeric: tabular-nums; }
.pd-stock em { font-style: normal; color: var(--muted); font-size: 12px; }
.pd-stock.is-zero strong { color: #6b5400; }
.pd-stock.is-negative strong { color: #9d2c2c; }
.pd-stock.is-bad { background: #f6f1f1; }
.pd-stock.is-bad strong { color: #9d2c2c; }
.pd-order-selected { margin-top: 3px; font-size: 12px; color: #3d4a44; }
.pd-lifetime-title { margin-top: 10px; font-size: 13px; color: var(--product-name); }
.pd-lifetime { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; margin-top: 4px; }
.pd-lifetime div { display: flex; flex-direction: column; }
.pd-lifetime span { color: var(--muted); font-size: 11px; }
.pd-lifetime strong { font-size: 15px; color: #0b6a34; font-variant-numeric: tabular-nums; }
.pd-history-table tfoot td { border-top: 2px solid #9fb3a9; font-weight: 800; background: #f4f8f6; }
.pd-summary-line { margin-top: 4px; font-size: 12px; color: #3d4a44; overflow-wrap: anywhere; }
.pd-summary { margin-top: 8px; padding: 8px 10px; border: 1px solid #b7c4bd; border-radius: 8px; background: #f8faf9; }
.pd-summary h3 { font-size: 14px; margin-bottom: 4px; }
.pd-summary dl { display: grid; grid-template-columns: minmax(9em, auto) 1fr; gap: 4px 12px; margin: 0; font-size: 13px; }
.pd-summary dt { color: var(--muted); font-weight: 700; }
.pd-summary dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.pd-summary dd span { display: block; color: var(--muted); font-size: 11px; font-weight: 500; }
.label-preview { max-width: 100%; overflow: hidden; }
.label-image {
  display: block;
  width: min(100%, 520px);
  height: auto;
  border: 1px solid #9aa6a0;
  background: #fff;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}
.pd-label-file { display: inline-block; margin-top: 4px; font-size: 12px; text-decoration: none; }

@media (max-width: 480px) {
  .pd-stock { padding: 3px 8px; font-size: 12px; }
  .pd-stock strong { font-size: 17px; }
  .pd-lifetime strong { font-size: 13px; }
  .pd-summary { padding: 6px 8px; }
  .pd-summary dl { grid-template-columns: 1fr; gap: 2px; font-size: 12px; }
  .pd-summary dd { margin-bottom: 4px; }
}

/* Sales Analysis add-on */
.sa { width: min(1100px, 100%); margin-inline: auto; padding: 0 8px 16px; }
.sa-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  margin: 4px 0 8px;
}
.sa-toolbar h2 { font-size: 15px; flex: 1 1 auto; }
.sa-create { display: flex; gap: 6px; flex: 1 1 220px; }
.sa-create input { flex: 1; min-width: 0; min-height: 40px; }
.sa-create button, #sa-refresh {
  min-height: 40px;
  border: 1px solid var(--green);
  border-radius: 6px;
  background: var(--green);
  color: #fff;
  font-weight: 700;
  padding: 0 12px;
}
.sa-project-card {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.sa-project-card strong { display: block; font-size: 16px; }
.sa-project-card .muted { display: block; font-size: 12px; margin-top: 2px; }
.sa-project-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.sa-project-actions button:not(.text-btn) {
  min-height: 40px;
  border: 1px solid var(--green);
  border-radius: 6px;
  background: #e7f3ec;
  color: var(--green);
  font-weight: 700;
  padding: 0 12px;
}
.sa-danger { color: #9d2c2c !important; }
.sa-board-toolbar #sa-board-title { font-size: 16px; }
.sa-legend { font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.sa-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.sa-col { min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.sa-slot {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 8px;
}
.sa-slot.is-empty { background: #f7faf8; }
.sa-slot.is-disabled { opacity: 0.55; }
.sa-slot header {
  display: flex;
  align-items: baseline;
  gap: 6px;
}
.sa-slot-num {
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  color: var(--muted);
  font-size: 12px;
}
.sa-slot header strong {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  color: var(--product-name);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sa-slot-edit { min-height: 32px; font-size: 13px; }
.sa-slot-meta { font-size: 11px; margin: 2px 0 4px; overflow-wrap: anywhere; }
.sa-metrics {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  font-variant-numeric: tabular-nums;
}
.sa-metrics div {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  background: #f3f8f5;
  border-radius: 4px;
  padding: 2px 6px;
}
.sa-metrics span { font-size: 10px; color: var(--muted); font-weight: 600; }
.sa-metrics strong { font-size: 15px; color: var(--primary-metric); line-height: 1.2; }
.sa-slot-dialog { width: min(480px, calc(100% - 16px)); }
.sa-slot-form { display: flex; flex-direction: column; gap: 8px; }
.sa-field { display: flex; flex-direction: column; gap: 2px; font-size: 12px; color: var(--muted); font-weight: 600; }
.sa-field input { min-height: 40px; font-size: 15px; font-weight: 600; color: var(--ink); }
.sa-slot-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.sa-carton-line { display: flex; align-items: center; gap: 6px; }
.sa-carton-line input { flex: 1; min-width: 0; }
.sa-reorder { display: flex; gap: 6px; }
.sa-reorder button { min-height: 40px; flex: 1; }
@media (max-width: 720px) {
  .sa-columns { grid-template-columns: 1fr; }
  .sa-slot-row { grid-template-columns: 1fr; }
}
