:root {
  --bg: #F5F4EF;
  --card: #FFFFFF;
  --ink: #17201B;
  --muted: #4B544E;
  --line: #C9C6BB;
  --line-soft: #E0DDD3;
  --chip: #E6E4DC;
  --green: #1E6B47;
  --green-dark: #14492F;
  --green-soft: #DCEFE4;
  --amber: #A3520B;
  --amber-text: #8A4407;
  --amber-soft: #FBEBD9;
  --red: #B42318;
  --red-soft: #FDE8E6;
  --cam: #1C2420;
  --radius: 14px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); }
body { min-height: 100dvh; overscroll-behavior-y: none; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--green); }
h1, h2, h3 { margin: 0; }
svg { flex: none; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------ layout */
.view {
  padding: calc(16px + var(--safe-t)) 16px calc(var(--tabbar-h) + var(--safe-b) + 24px);
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.view-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.view-head h1 { font-size: 24px; font-weight: 700; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.grow { flex: 1; min-width: 0; }
.section-title {
  font-size: 13px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em; margin: 4px 0 0;
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: var(--card); border-top: 1px solid var(--line-soft);
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr));
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 12px; font-weight: 500; color: var(--muted); text-decoration: none;
}
.tabbar a.active { color: var(--green); font-weight: 600; }
.tabbar svg, .icon {
  width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}

/* ----------------------------------------------------------- buttons */
.btn {
  min-height: 44px; padding: 0 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); font-size: 15px; font-weight: 500;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  text-decoration: none; color: var(--ink);
}
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--green); border-color: var(--green); color: #fff; font-weight: 600; }
.btn-out { background: var(--amber); border-color: var(--amber); color: #fff; font-weight: 600; }
.btn-danger { color: var(--red); border-color: #E7B4AE; }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-block { width: 100%; min-height: 52px; font-size: 16px; }
.btn-icon { width: 44px; padding: 0; }
.btn-small { min-height: 36px; padding: 0 12px; font-size: 14px; }

.segmented {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px;
  background: var(--chip); border-radius: 12px; padding: 4px;
}
.segmented button {
  min-height: 44px; border: 0; border-radius: 9px; background: transparent;
  font-size: 15px; font-weight: 600; color: var(--muted);
}
.segmented button.on-in { background: var(--green); color: #fff; }
.segmented button.on-out { background: var(--amber); color: #fff; }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  min-height: 40px; padding: 0 14px; border-radius: 20px; border: 1px solid var(--line);
  background: var(--card); font-size: 14px;
}
.chip.on { background: var(--ink); border-color: var(--ink); color: #fff; font-weight: 600; }
.chip.green.on { background: var(--green); border-color: var(--green); }

/* ------------------------------------------------------------ forms */
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 500; color: var(--muted); }
.field input, .field select, .input {
  height: 46px; border: 1px solid var(--line); border-radius: 10px; padding: 0 12px;
  font-size: 16px; color: var(--ink); background: var(--card); width: 100%;
}
.field input:focus, .input:focus { outline: 2px solid var(--green); outline-offset: 0; border-color: var(--green); }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.check { display: flex; gap: 10px; align-items: center; font-size: 14px; min-height: 44px; }
.check input { width: 20px; height: 20px; accent-color: var(--green); }
.form-error { background: var(--red-soft); color: var(--red); padding: 10px 12px; border-radius: 10px; margin: 0; font-size: 14px; }
.form-ok { background: var(--green-soft); color: var(--green-dark); padding: 10px 12px; border-radius: 10px; margin: 0; font-size: 14px; }
.hint { font-size: 12px; font-weight: 400; color: var(--muted); }
.hint.warn { color: var(--amber-text); }

.stepper {
  display: inline-flex; align-items: center; background: var(--card);
  border: 1px solid var(--line); border-radius: 12px; padding: 2px;
}
.stepper button { width: 44px; height: 40px; border: 0; background: transparent; font-size: 22px; }
.stepper output { width: 36px; text-align: center; font-size: 17px; font-weight: 700; }

/* ----------------------------------------------------------- camera */
.camera {
  position: relative; border-radius: 20px; overflow: hidden; background: var(--cam);
  aspect-ratio: 4 / 3.6; display: flex; align-items: center; justify-content: center;
}
.camera video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.camera .frame { position: relative; width: 72%; height: 38%; pointer-events: none; }
.camera .frame i { position: absolute; width: 28px; height: 28px; border-color: #fff; border-style: solid; border-width: 0; }
.camera .frame i:nth-child(1) { top: 0; left: 0; border-top-width: 4px; border-left-width: 4px; border-radius: 8px 0 0 0; }
.camera .frame i:nth-child(2) { top: 0; right: 0; border-top-width: 4px; border-right-width: 4px; border-radius: 0 8px 0 0; }
.camera .frame i:nth-child(3) { bottom: 0; left: 0; border-bottom-width: 4px; border-left-width: 4px; border-radius: 0 0 0 8px; }
.camera .frame i:nth-child(4) { bottom: 0; right: 0; border-bottom-width: 4px; border-right-width: 4px; border-radius: 0 0 8px 0; }
.camera .frame .laser {
  position: absolute; left: 10px; right: 10px; top: 50%; height: 3px; border-radius: 2px;
  background: #4ADE80; box-shadow: 0 0 12px #4ADE80; animation: laser 1.8s ease-in-out infinite;
}
.camera.mode-out .frame .laser { background: #F59E0B; box-shadow: 0 0 12px #F59E0B; }
@keyframes laser { 0%, 100% { transform: translateY(-26px); } 50% { transform: translateY(26px); } }
.camera .cam-label {
  position: absolute; bottom: 14px; left: 12px; right: 12px; text-align: center;
  color: #fff; font-size: 14px; text-shadow: 0 1px 3px rgba(0,0,0,.6);
}
.camera .cam-error {
  position: relative; color: #E6EDE8; text-align: center; padding: 24px; display: flex;
  flex-direction: column; gap: 12px; align-items: center; font-size: 14px;
}
.camera.flash::after { content: ""; position: absolute; inset: 0; background: rgba(255,255,255,.35); animation: flash .3s ease-out forwards; }
@keyframes flash { to { opacity: 0; } }

.ocr-cam { position: relative; border-radius: 14px; overflow: hidden; background: var(--cam); aspect-ratio: 16 / 9; }
.ocr-cam video { width: 100%; height: 100%; object-fit: cover; display: block; }
.ocr-cam .ocr-box {
  position: absolute; left: 10%; right: 10%; top: 30%; bottom: 30%;
  border: 3px solid #fff; border-radius: 10px; box-shadow: 0 0 0 999px rgba(0,0,0,.35);
}
.ocr-cam .cam-label { position: absolute; top: 8px; left: 0; right: 0; text-align: center; color: #fff; font-size: 13px; }

/* ------------------------------------------------------------ lists */
.card { background: var(--card); border-radius: var(--radius); }
.list { display: flex; flex-direction: column; gap: 8px; }
.item {
  background: var(--card); border-radius: var(--radius); padding: 10px 12px;
  display: flex; align-items: center; gap: 12px; border: 0; width: 100%; text-align: left;
}
.thumb {
  width: 44px; height: 44px; border-radius: 8px; background: var(--chip); flex: none;
  object-fit: cover; display: flex; align-items: center; justify-content: center; color: var(--muted);
}
.thumb.lg { width: 96px; height: 96px; border-radius: 14px; }
.item .name { font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .meta { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.exp { font-size: 12px; color: var(--muted); }
.exp.soon { color: var(--amber-text); font-weight: 600; }
.exp.over { color: var(--red); font-weight: 600; }
.qty {
  min-width: 40px; height: 32px; padding: 0 8px; border-radius: 8px; background: #EEF5F1; color: var(--green-dark);
  font-size: 15px; font-weight: 700; display: flex; align-items: center; justify-content: center;
}
.delta-in { color: var(--green); font-weight: 700; }
.delta-out { color: var(--amber); font-weight: 700; }
.banner {
  padding: 12px 14px; border-radius: var(--radius); background: var(--amber-soft); color: #6E3705;
  font-size: 14px; display: flex; gap: 10px; align-items: center; border: 0; text-align: left; width: 100%;
}
.empty { text-align: center; color: var(--muted); padding: 32px 12px; font-size: 14px; }
.search {
  display: flex; align-items: center; gap: 8px; height: 46px; background: var(--card);
  border: 1px solid var(--line); border-radius: 12px; padding: 0 12px;
}
.search input { flex: 1; border: 0; outline: 0; background: transparent; font-size: 16px; min-width: 0; }
.batch {
  display: flex; align-items: center; gap: 10px; background: var(--card); border-radius: 12px;
  padding: 8px 8px 8px 14px; border: 1px solid var(--line-soft);
}
.batch-pick {
  min-height: 56px; border-radius: 12px; border: 1px solid var(--line); background: var(--card);
  padding: 8px 14px; display: flex; justify-content: space-between; align-items: center; text-align: left; width: 100%;
}
.batch-pick.on { border: 2px solid var(--green); padding: 7px 13px; }
.badge {
  align-self: flex-start; font-size: 12px; font-weight: 600; padding: 4px 8px; border-radius: 6px;
  background: var(--green-soft); color: var(--green); display: inline-block;
}
.badge.warn { background: var(--amber-soft); color: var(--amber-text); }

/* ----------------------------------------------------------- sheet */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 40; background: rgba(20, 26, 22, .45);
  display: flex; align-items: flex-end; justify-content: center; animation: fade .15s ease-out;
}
.sheet {
  background: var(--bg); width: 100%; max-width: 560px; max-height: calc(100dvh - 24px - var(--safe-t));
  border-radius: 22px 22px 0 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 10px 18px calc(20px + var(--safe-b)); animation: slide .2s ease-out;
  display: flex; flex-direction: column; gap: 14px;
}
.sheet-grip { align-self: center; width: 40px; height: 5px; border-radius: 3px; background: var(--line); }
.sheet-head { display: flex; align-items: center; gap: 8px; }
.sheet-head h2 { font-size: 20px; font-weight: 700; flex: 1; }
.sheet-foot { position: sticky; bottom: calc(-20px - var(--safe-b)); background: var(--bg); padding: 8px 0 calc(4px); display: flex; gap: 10px; }
@keyframes fade { from { opacity: 0; } }
@keyframes slide { from { transform: translateY(40px); opacity: .6; } }

.product-head { display: flex; gap: 14px; align-items: center; }
.product-head .title { font-size: 18px; font-weight: 700; }
.photo-pick { display: flex; gap: 14px; align-items: center; }

/* ------------------------------------------------------------ toast */
#toast-root {
  position: fixed; left: 12px; right: 12px; z-index: 60;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 12px);
  display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none;
}
.sheet-open #toast-root { bottom: auto; top: calc(12px + var(--safe-t)); }
.toast {
  pointer-events: auto; max-width: 520px; width: 100%; background: var(--ink); color: #fff;
  border-radius: 14px; padding: 12px 14px; display: flex; gap: 12px; align-items: center;
  font-size: 14px; box-shadow: 0 6px 24px rgba(0,0,0,.2); animation: slide .2s ease-out;
}
.toast.error { background: var(--red); }
.toast button { background: transparent; border: 0; color: #9FE3BC; font-weight: 700; min-height: 36px; padding: 0 4px; }

.spinner {
  width: 18px; height: 18px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent;
  animation: spin .7s linear infinite; display: inline-block; vertical-align: -3px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------------ login */
.login-page { display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login-card {
  background: var(--card); border-radius: 20px; padding: 28px 22px; width: 100%; max-width: 380px;
  display: flex; flex-direction: column; gap: 16px;
}
.login-card.wide { max-width: 520px; }
.login-card h1 { font-size: 24px; }
.login-logo { border-radius: 14px; }
.checks { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.checks .ok { color: var(--green); }
.checks .warn { color: var(--amber-text); }
.checks .bad { color: var(--red); font-weight: 600; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; }
}
