:root {
  --bg: #f6f1e9;
  --card: #fffdf9;
  --ink: #2b2118;
  --muted: #7d6e60;
  --line: #e6dccd;
  --wood: #5c3a21;
  --wood-2: #7a4b2a;
  --gold: #b07d2b;
  --good: #2f7d4f;
  --mid: #b07d2b;
  --low: #b2452f;
  --radius: 14px;
  --tab-h: 64px;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
.sr-only { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); border: 0; }
body { margin: 0; min-height: 100vh; background: var(--bg); }
#app { max-width: 760px; margin: 0 auto; padding: 12px 14px calc(var(--tab-h) + 28px + env(safe-area-inset-bottom)); }
.boot { padding: 80px 0; text-align: center; color: var(--muted); }
h1 { font-size: 22px; margin: 6px 0 14px; color: var(--wood); }
h2 { font-size: 17px; margin: 22px 0 10px; color: var(--wood); }
h3 { font-size: 15px; margin: 14px 0 8px; }
p { line-height: 1.55; }
a { color: var(--wood-2); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.right { text-align: right; }
.hidden { display: none !important; }

/* top bar */
.topbar { display: flex; align-items: center; gap: 10px; margin: 2px 0 12px; }
.topbar h1 { margin: 0; flex: 1; }
.back { font-size: 15px; text-decoration: none; padding: 6px 2px; }

/* buttons */
button, .btn {
  font: inherit; font-size: 15px; border: 1px solid var(--line); background: var(--card); color: var(--ink);
  padding: 10px 14px; border-radius: 10px; cursor: pointer; text-decoration: none; display: inline-flex;
  align-items: center; justify-content: center; gap: 6px; min-height: 42px;
}
button:active, .btn:active { transform: translateY(1px); }
.primary { background: var(--wood); border-color: var(--wood); color: #fff; }
.danger { color: var(--low); border-color: #e8c3b9; }
.ghost { background: transparent; }
.block { width: 100%; }
button:disabled { opacity: .5; }
.chip-btn { padding: 6px 12px; min-height: 34px; font-size: 14px; border-radius: 999px; }
.chip-btn.on { background: var(--wood); color: #fff; border-color: var(--wood); }

/* cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.stat .k { font-size: 13px; color: var(--muted); }
.stat .v { font-size: 22px; font-weight: 700; margin-top: 4px; color: var(--wood); }
.stat .s { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stat.wide { grid-column: 1 / -1; }
h1.brand { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
h1.brand small { font-size: 13px; font-weight: 500; color: var(--gold); letter-spacing: .5px; }
.alerts a { display: flex; justify-content: space-between; padding: 11px 2px; border-bottom: 1px solid var(--line); text-decoration: none; color: var(--ink); }
.alerts a:last-child { border-bottom: 0; }
.alerts b { color: var(--wood); }

/* item grid */
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 560px) { .grid { grid-template-columns: repeat(3, 1fr); } }
.tile { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; text-decoration: none; color: inherit; display: block; position: relative; }
.tile .ph { aspect-ratio: 1; background: #efe6d8; display: flex; align-items: center; justify-content: center; font-size: 34px; color: #c9b79f; }
.tile .ph img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile .body { padding: 8px 10px 10px; }
.tile .code { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.tile .name { font-weight: 600; font-size: 14px; margin: 2px 0 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile .price { font-size: 14px; }
.tile .badges { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 5px; }
.tile.found { outline: 3px solid var(--good); }
.tile .tick { position: absolute; top: 8px; right: 8px; background: var(--good); color: #fff; border-radius: 999px; width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; font-weight: 700; }

.badge { font-size: 11px; padding: 2px 7px; border-radius: 999px; background: #efe6d8; color: var(--wood); white-space: nowrap; }
.badge.m-good { background: #dcefe3; color: var(--good); }
.badge.m-mid { background: #f6ead2; color: #8a5f17; }
.badge.m-low { background: #f6ddd6; color: var(--low); }
.st-sold { background: #e4e4e4; color: #555; }
.st-reserved { background: #e3e9f7; color: #34508f; }
.st-consigned { background: #efe1f3; color: #74378a; }
.st-missing { background: #f6ddd6; color: var(--low); }
.st-personal { background: #e8efe0; color: #4d6a2f; }
.st-gifted { background: #e4e4e4; color: #555; }

/* filters */
.search { width: 100%; }
.filters { display: flex; gap: 6px; overflow-x: auto; padding: 8px 0 10px; -webkit-overflow-scrolling: touch; }
.filters::-webkit-scrollbar { display: none; }
.filters .chip-btn { flex: none; white-space: nowrap; }

/* forms */
label.f { display: block; margin-bottom: 12px; }
label.f > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }
input, select, textarea {
  font: inherit; font-size: 16px; width: 100%; padding: 10px 11px; border: 1px solid var(--line); border-radius: 10px;
  background: #fff; color: var(--ink);
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #d9c09a; border-color: #d9c09a; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.three { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.money { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: #fff; }
.seg button { border: 0; border-radius: 0; min-height: 42px; padding: 8px 11px; font-size: 14px; background: transparent; }
.seg button.on { background: var(--wood); color: #fff; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.fieldset { border-top: 1px dashed var(--line); margin-top: 6px; padding-top: 12px; }

/* margin box */
.calc { background: #fbf5ea; border: 1px solid #ecdcc0; border-radius: var(--radius); padding: 12px 14px; margin: 6px 0 14px; }
.calc .line { display: flex; justify-content: space-between; padding: 3px 0; font-size: 14px; }
.calc .big { display: flex; gap: 10px; margin-top: 8px; }
.calc .big div { flex: 1; background: #fff; border-radius: 10px; padding: 8px; text-align: center; border: 1px solid #ecdcc0; }
.calc .big b { display: block; font-size: 20px; }
.calc .big small { color: var(--muted); font-size: 12px; }
.good { color: var(--good); } .mid { color: #8a5f17; } .low { color: var(--low); }
.target { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-top: 10px; font-size: 13px; }

/* detail */
.gallery { display: flex; gap: 8px; overflow-x: auto; scroll-snap-type: x mandatory; margin: 0 -14px 12px; padding: 0 14px; }
.gallery img { height: 260px; max-width: 86%; border-radius: var(--radius); scroll-snap-align: start; object-fit: cover; background: #efe6d8; }
.gallery .empty { height: 160px; width: 100%; border-radius: var(--radius); background: #efe6d8; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.kv { display: grid; grid-template-columns: 96px 1fr; gap: 7px 10px; font-size: 15px; }
.kv .k { color: var(--muted); font-size: 14px; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }

/* photo editor */
.photos { display: flex; gap: 8px; flex-wrap: wrap; }
.photos .p { position: relative; width: 84px; height: 84px; border-radius: 10px; overflow: hidden; background: #efe6d8; }
.photos .p img { width: 100%; height: 100%; object-fit: cover; }
.photos .p .x, .photos .p .star { position: absolute; top: 3px; width: 26px; height: 26px; min-height: 0; padding: 0; border-radius: 999px; font-size: 13px; background: rgba(255,255,255,.92); border: 0; }
.photos .p .x { right: 3px; }
.photos .p .star { left: 3px; }
.photos .p.main { outline: 3px solid var(--gold); }
.photos .add { width: 84px; height: 84px; border: 2px dashed var(--line); border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 28px; color: var(--muted); cursor: pointer; }

/* stocktake */
.progress { height: 10px; background: #efe6d8; border-radius: 999px; overflow: hidden; margin: 8px 0; }
.progress > div { height: 100%; background: var(--good); transition: width .3s; }
.scanner { position: fixed; inset: 0; background: #000; z-index: 50; display: flex; flex-direction: column; }
.scanner video { flex: 1; width: 100%; object-fit: cover; }
.scanner .frame { position: absolute; left: 50%; top: 42%; width: 64vw; max-width: 300px; aspect-ratio: 1; transform: translate(-50%, -50%); border: 3px solid rgba(255,255,255,.85); border-radius: 18px; box-shadow: 0 0 0 9999px rgba(0,0,0,.35); pointer-events: none; }
.scanner .bar { position: absolute; left: 0; right: 0; bottom: 0; padding: 14px 14px calc(14px + env(safe-area-inset-bottom)); background: rgba(0,0,0,.72); color: #fff; }
.scanner .last { min-height: 44px; font-size: 16px; margin-bottom: 10px; }
.scanner.flash { animation: flash .35s; }
@keyframes flash { from { box-shadow: inset 0 0 0 12px #3fbf72; } to { box-shadow: inset 0 0 0 0 transparent; } }

/* tabs */
.tabs { position: fixed; left: 0; right: 0; bottom: 0; height: calc(var(--tab-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom); background: rgba(255,253,249,.97); border-top: 1px solid var(--line); display: flex; z-index: 20; backdrop-filter: blur(8px); }
.tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 11px; text-decoration: none; color: var(--muted); gap: 2px; }
.tabs a span { font-size: 20px; line-height: 1; }
.tabs a.on { color: var(--wood); font-weight: 700; }
.tabs .tab-add span { background: var(--wood); color: #fff; width: 34px; height: 34px; border-radius: 999px; display: flex; align-items: center; justify-content: center; font-size: 22px; }

/* toast + modal */
.toast { position: fixed; left: 50%; bottom: calc(var(--tab-h) + 18px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: #2b2118; color: #fff; padding: 10px 16px; border-radius: 12px; z-index: 80; max-width: 90vw; font-size: 15px; box-shadow: 0 4px 18px rgba(0,0,0,.2); }
.toast.err { background: var(--low); }
.modal { position: fixed; inset: 0; background: rgba(30,20,10,.45); z-index: 70; display: flex; align-items: flex-end; justify-content: center; }
.modal .sheet { background: var(--bg); width: 100%; max-width: 620px; max-height: 88vh; overflow-y: auto; border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
.modal img.full { width: 100%; border-radius: 12px; }
.viewer { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 90; display: flex; align-items: center; justify-content: center; }
.viewer img { max-width: 100%; max-height: 100%; }

/* login */
.login { max-width: 360px; margin: 14vh auto 0; text-align: center; }
.login .logo { width: 84px; height: 84px; border-radius: 20px; }

/* labels */
.label-list label { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.label-list input[type=checkbox] { width: 22px; height: 22px; flex: none; }
.print-sheet { display: none; }
.lbl { width: var(--lw, 32mm); height: var(--lh, 40mm); border: 0.3mm dashed #999; padding: 2mm; display: flex; flex-direction: column; align-items: center; justify-content: flex-start; overflow: hidden; break-inside: avoid; page-break-inside: avoid; }
.lbl svg { width: calc(var(--lw, 32mm) - 6mm); height: calc(var(--lw, 32mm) - 6mm); }
.lbl .c { font: 700 3.2mm/1.1 -apple-system, "Helvetica Neue", Arial, sans-serif; margin-top: 1mm; letter-spacing: .2mm; }
.lbl .n { font-size: 2.4mm; line-height: 1.15; text-align: center; max-height: 5.6mm; overflow: hidden; }
.lbl .pr { font-size: 2.6mm; font-weight: 600; }
.sheet-grid { display: flex; flex-wrap: wrap; gap: 0; }
.preview-sheet { background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 8px; overflow-x: auto; }

@media print {
  @page { size: A4; margin: 8mm; }
  body { background: #fff; }
  #app, #tabs, #toast, #modal { display: none !important; }
  .print-sheet { display: block; }
}

.help h2 { margin-top: 26px; }
.help li { margin-bottom: 8px; line-height: 1.55; }
.empty-state { text-align: center; padding: 40px 10px; color: var(--muted); }
.empty-state .big { font-size: 46px; }
table.mini { width: 100%; border-collapse: collapse; font-size: 14px; }
table.mini td { padding: 7px 4px; border-bottom: 1px solid var(--line); }
