:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #dfe2e8;
  --text: #1a1d23;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --in: #0f7b4f;
  --out: #b4452b;
  --danger: #c0392b;
  --warn-bg: #fef6e4;
  --warn-text: #8a5a00;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 10px rgba(16, 24, 40, .04);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171c;
    --surface: #1e2127;
    --border: #333843;
    --text: #e8eaed;
    --muted: #9aa2af;
    --accent: #5b8def;
    --accent-text: #0d1117;
    --in: #3ec98a;
    --out: #f07a5c;
    --danger: #ef6f62;
    --warn-bg: #3a3017;
    --warn-text: #f0c674;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}
:root[data-theme="dark"] {
  --bg: #15171c; --surface: #1e2127; --border: #333843; --text: #e8eaed;
  --muted: #9aa2af; --accent: #5b8def; --accent-text: #0d1117;
  --in: #3ec98a; --out: #f07a5c; --danger: #ef6f62;
  --warn-bg: #3a3017; --warn-text: #f0c674;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
}

/* ─────────────────────────────────────────── 版面 */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 16px 0;
}
.topbar h1 { margin: 0 0 10px; font-size: 18px; font-weight: 650; letter-spacing: .01em; }
.tabs { display: flex; gap: 2px; }
.tab {
  appearance: none; background: none; border: 0; cursor: pointer;
  padding: 9px 14px; font: inherit; color: var(--muted);
  border-bottom: 2px solid transparent; border-radius: 6px 6px 0 0;
}
.tab:hover { color: var(--text); background: color-mix(in srgb, var(--text) 5%, transparent); }
.tab.is-active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

main { max-width: 1040px; margin: 0 auto; padding: 16px; }
.view { display: none; }
.view.is-active { display: block; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}

/* ─────────────────────────────────────────── 元件 */
input, select, button, a.btn {
  font: inherit; color: var(--text);
}
input[type="text"], input[type="number"], input[type="search"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  min-width: 0;
}
input:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

.btn {
  appearance: none; cursor: pointer;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 15px;
  font-weight: 550;
  text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center;
  white-space: nowrap;
}
.btn:hover { background: color-mix(in srgb, var(--text) 6%, var(--surface)); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: color-mix(in srgb, #000 12%, var(--accent)); }
.btn-in { color: var(--in); border-color: color-mix(in srgb, var(--in) 45%, var(--border)); }
.btn-out { color: var(--out); border-color: color-mix(in srgb, var(--out) 45%, var(--border)); }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.btn-ghost { background: none; }

.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 13px; margin: 10px 0 0; min-height: 1.2em; }
.hint.warn { color: var(--warn-text); }
.req { color: var(--danger); }
.empty { color: var(--muted); text-align: center; padding: 28px 10px; margin: 0; }

/* ─────────────────────────────────────────── 掃碼區 */
.scan-input { display: flex; gap: 8px; flex-wrap: wrap; }
.scan-input input { flex: 1 1 220px; font-size: 17px; letter-spacing: .04em; }

.camera-wrap {
  position: relative; margin-top: 12px;
  background: #000; border-radius: var(--radius); overflow: hidden;
  aspect-ratio: 4 / 3; max-height: 60vh;
}
.camera-wrap video { width: 100%; height: 100%; object-fit: cover; display: block; }
.reticle {
  position: absolute; inset: 22% 10%;
  border: 2px solid rgba(255, 255, 255, .85);
  border-radius: 8px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .35);
  pointer-events: none;
}
.camera-stop {
  position: absolute; right: 10px; bottom: 10px;
  background: rgba(0, 0, 0, .6); color: #fff; border-color: rgba(255, 255, 255, .4);
}
.camera-stop:hover { background: rgba(0, 0, 0, .8); }

/* ─────────────────────────────────────────── 查詢結果 */
.result-head { display: flex; gap: 14px; align-items: flex-start; flex-wrap: wrap; }
.thumb {
  width: 76px; height: 76px; object-fit: contain;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; flex: none;
}
.result-title { flex: 1 1 240px; min-width: 0; }
.result-title h2 { margin: 0 0 4px; font-size: 19px; line-height: 1.3; }
.result-title p { margin: 2px 0; font-size: 13px; }

.badges { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  font-size: 12px; padding: 2px 8px; border-radius: 999px;
  background: color-mix(in srgb, var(--text) 8%, transparent);
  border: 1px solid var(--border);
}
.badge.ok { color: var(--in); border-color: color-mix(in srgb, var(--in) 40%, var(--border)); }
.badge.bad { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.badge.warn { background: var(--warn-bg); color: var(--warn-text); border-color: transparent; }

.stock-box {
  text-align: right; flex: none; padding-left: 10px;
  border-left: 1px solid var(--border);
}
.stock-label { display: block; font-size: 12px; color: var(--muted); }
.stock-box strong { font-size: 30px; font-variant-numeric: tabular-nums; line-height: 1.1; }
.stock-box.low strong { color: var(--warn-text); }
.stock-box.zero strong { color: var(--danger); }

.actions { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border); }
.qty-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.qty-row label { font-size: 13px; color: var(--muted); }
.qty-row input[type="number"] { width: 96px; font-size: 16px; }
.qty-row input[type="text"] { flex: 1 1 180px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

/* ─────────────────────────────────────────── 候選資料 */
.candidates { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border); }
.candidates h3 { margin: 0 0 4px; font-size: 15px; }
.candidates-lead { margin: 0 0 12px; font-size: 13px; color: var(--muted); line-height: 1.5; }
.candidates-lead strong { color: var(--text); }

.cand {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 11px 13px;
  margin-bottom: 10px;
  background: color-mix(in srgb, var(--text) 2%, var(--surface));
}
.cand.is-used {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, var(--surface));
}
.cand-head { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 9px; }
.badge.src { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }

.cand-body { display: flex; gap: 11px; align-items: flex-start; }
.cand-thumb {
  width: 54px; height: 54px; object-fit: contain; flex: none;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px;
}
.cand-main { flex: 1; min-width: 0; }
.cand-title { margin: 0 0 5px; font-size: 15px; font-weight: 600; line-height: 1.35; }
.cand-fields { margin: 0 0 5px; display: flex; gap: 12px; flex-wrap: wrap; }
.cand-field { font-size: 13px; }
.cand-field em { font-style: normal; color: var(--muted); margin-right: 4px; }
.cand-note, .cand-samples { margin: 2px 0 0; font-size: 12px; color: var(--muted); }

.cand-foot {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  margin-top: 10px; padding-top: 9px; border-top: 1px solid var(--border);
}
.cand-fills { font-size: 12px; }
.cand-warn {
  margin: 10px 0 0; padding: 8px 11px; font-size: 12px;
  background: var(--warn-bg); color: var(--warn-text); border-radius: 7px;
}

/* 與存檔不同的欄位 */
.cand-field.is-diff, .cand-title.is-diff {
  background: var(--warn-bg); color: var(--warn-text);
  border-radius: 5px; padding: 1px 6px;
}
.diff-was {
  display: inline-block; margin-left: 7px; font-size: 12px;
  color: var(--muted); font-weight: 400;
}

/* ─────────────────────────────────────────── 查詢歷程 */
.variants { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border); }
.variants h3 { margin: 0 0 4px; font-size: 15px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.variants-lead { margin: 0 0 11px; font-size: 13px; color: var(--muted); line-height: 1.5; }
.variant-list { list-style: none; margin: 0; padding: 0; }
.variant-item { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 7px; overflow: hidden; }
.variant-toggle {
  display: flex; gap: 9px; align-items: center; flex-wrap: wrap;
  width: 100%; text-align: left; cursor: pointer;
  background: none; border: 0; padding: 9px 12px; font: inherit;
}
.variant-toggle:hover { background: color-mix(in srgb, var(--text) 5%, transparent); }
.vt-caret { color: var(--muted); flex: none; width: 12px; }
.vt-label { font-weight: 600; flex: none; }
.vt-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.vt-count { color: var(--muted); font-size: 13px; flex: none; font-variant-numeric: tabular-nums; }
.variant-detail { padding: 2px 12px 11px 33px; border-top: 1px solid var(--border); }
.variant-detail p { margin: 8px 0; font-size: 13px; }
.variant-times { list-style: none; margin: 0; padding: 0; }
.variant-times li {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  padding: 4px 0; font-size: 13px; border-bottom: 1px solid var(--border);
}
.variant-times li:last-child { border-bottom: 0; }
.variant-times time { color: var(--muted); font-variant-numeric: tabular-nums; flex: none; }

/* ─────────────────────────────────────────── 商品表單 */
.product-form { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border); }
.grid {
  display: grid; gap: 10px 12px;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  margin-bottom: 14px;
}
.grid label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.grid input { color: var(--text); }
.grid .span2 { grid-column: span 2; }

/* ─────────────────────────────────────────── 歷史 / 表格 */
.history { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border); }
.history h3 { margin: 0 0 8px; font-size: 13px; color: var(--muted); font-weight: 600; }
.history ul { list-style: none; margin: 0; padding: 0; }
.history li {
  display: flex; gap: 10px; align-items: baseline;
  padding: 5px 0; font-size: 13px; border-bottom: 1px solid var(--border);
}
.history li:last-child { border-bottom: 0; }
.history time { color: var(--muted); font-variant-numeric: tabular-nums; flex: none; }
.history .delta { font-variant-numeric: tabular-nums; font-weight: 600; flex: none; }
.history .note { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.delta.plus { color: var(--in); }
.delta.minus { color: var(--out); }

.stats { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); margin-bottom: 16px; }
.stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px;
}
.stat .k { font-size: 12px; color: var(--muted); display: block; margin-bottom: 2px; }
.stat .v { font-size: 22px; font-weight: 650; font-variant-numeric: tabular-nums; }
.stat.alert .v { color: var(--warn-text); }

.list-toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.list-toolbar input[type="search"] { flex: 1 1 220px; }
.checkbox { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; color: var(--muted); white-space: nowrap; }

.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td {
  text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.table th { font-size: 12px; color: var(--muted); font-weight: 600; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table tbody tr { cursor: pointer; }
.table tbody tr:hover { background: color-mix(in srgb, var(--text) 4%, transparent); }
.table .name { white-space: normal; min-width: 180px; }
.table .code { font-variant-numeric: tabular-nums; color: var(--muted); }
.table tr.low td.stock { color: var(--warn-text); font-weight: 650; }
.table tr.zero td.stock { color: var(--danger); font-weight: 650; }
.kind-in { color: var(--in); }
.kind-out { color: var(--out); }

/* ─────────────────────────────────────────── 頁尾聲明 */
.site-footer { max-width: 1040px; margin: 0 auto; padding: 0 16px 28px; }
.site-footer details {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface);
}
.site-footer summary {
  cursor: pointer; padding: 11px 15px; font-size: 13px; color: var(--muted);
  list-style: none;
}
.site-footer summary::-webkit-details-marker { display: none; }
.site-footer summary::before { content: '▸ '; }
.site-footer details[open] summary::before { content: '▾ '; }
.site-footer summary:hover { color: var(--text); }
.notice { padding: 0 15px 13px; font-size: 13px; line-height: 1.65; }
.notice p { margin: 0 0 9px; }
.notice p:last-child { margin-bottom: 0; }

/* ─────────────────────────────────────────── 提示訊息 */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 16px);
  background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 999px; font-size: 14px; font-weight: 550;
  box-shadow: 0 4px 20px rgba(0, 0, 0, .25);
  opacity: 0; pointer-events: none; transition: opacity .18s, transform .18s;
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--danger); color: #fff; }

@media (max-width: 560px) {
  main { padding: 12px; }
  .stock-box { border-left: 0; padding-left: 0; text-align: left; }
  .grid .span2 { grid-column: span 1; }
  .btn-row .btn { flex: 1 1 auto; }
}
