:root {
  --bg: #f4f5f2;
  --card: #ffffff;
  --text: #1c1f1a;
  --muted: #646a60;
  --line: #dde0d8;
  --brand: #14532d;
  --brand-2: #1f7a43;
  --brand-soft: #e3f1e7;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --radius: 12px;
  --tabbar-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121411;
    --card: #1c1f1a;
    --text: #e8ebe4;
    --muted: #9aa294;
    --line: #2f342c;
    --brand: #4ade80;
    --brand-2: #22c55e;
    --brand-soft: #173222;
    --warn: #fbbf24;
    --warn-soft: #3a2c0b;
    --bad: #f87171;
    --bad-soft: #3b1515;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
a { color: var(--brand-2); }
h1 { font-size: 1.4rem; margin: 0 0 12px; }
h2 { font-size: 1.1rem; margin: 20px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; }
.pad { padding: 24px 16px; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--brand); color: #fff;
}
@media (prefers-color-scheme: dark) { .topbar { background: #0d2617; } }
.brand { color: #fff; text-decoration: none; font-weight: 700; font-size: 1.1rem; display: flex; align-items: center; gap: 8px; }
.brand img { background: #fff; border-radius: 6px; }
.topnav { display: none; margin-left: auto; gap: 4px; }
.topnav a { color: #fff; text-decoration: none; padding: 6px 12px; border-radius: 8px; opacity: 0.85; }
.topnav a.active, .topnav a:hover { background: rgba(255,255,255,0.15); opacity: 1; }
main { max-width: 1100px; margin: 0 auto; padding: 16px; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 0.75rem;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a.active { color: var(--brand-2); font-weight: 600; }
.tabbar a.scan-tab span.bubble {
  width: 52px; height: 52px; border-radius: 50%; background: var(--brand-2); color: #fff;
  display: flex; align-items: center; justify-content: center; margin-top: -18px; box-shadow: 0 2px 8px rgba(0,0,0,0.25);
}
@media (min-width: 900px) {
  body { padding-bottom: 0; }
  .tabbar { display: none; }
  .topnav { display: flex; }
}

/* ---------- components ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.grow { flex: 1; min-width: 0; }
.grid2 { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 700px) { .grid2 { grid-template-columns: 1fr 1fr; } }

button, .btn {
  font: inherit; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 8px 16px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card); color: var(--text); text-decoration: none;
}
button.primary, .btn.primary { background: var(--brand-2); border-color: var(--brand-2); color: #fff; }
button.danger { color: var(--bad); border-color: var(--bad); }
button.big { min-height: 56px; font-size: 1.1rem; flex: 1; }
button:disabled { opacity: 0.5; cursor: default; }
button.link { background: none; border: none; color: var(--brand-2); padding: 4px; min-height: 0; font-weight: 500; }

label { display: block; font-size: 0.85rem; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  font: inherit; width: 100%; min-height: 44px; padding: 8px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--card); color: var(--text);
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-2); outline-offset: 1px; }
.field { margin-bottom: 12px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 1rem; color: var(--text); font-weight: 500; }
.check input { width: 22px; min-height: 22px; }

.search { position: relative; }
.search input { padding-left: 40px; }
.search svg { position: absolute; left: 12px; top: 12px; width: 20px; height: 20px; color: var(--muted); }

.chips { display: flex; gap: 6px; overflow-x: auto; padding: 8px 0; scrollbar-width: none; }
.chip {
  white-space: nowrap; min-height: 34px; padding: 4px 12px; border-radius: 999px; font-weight: 500; font-size: 0.9rem;
}
.chip.on { background: var(--brand-soft); border-color: var(--brand-2); color: var(--brand-2); }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; background: var(--brand-soft); color: var(--brand-2); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.grey { background: var(--line); color: var(--muted); }

/* part list */
.list { list-style: none; margin: 0; padding: 0; }
.list li { border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: none; }
.list a.item { display: flex; gap: 12px; align-items: center; padding: 12px 4px; color: inherit; text-decoration: none; }
.thumb { width: 52px; height: 52px; border-radius: 8px; background: var(--bg); object-fit: cover; flex: none; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.item .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: 0.85rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qty { font-size: 1.3rem; font-weight: 700; text-align: right; min-width: 48px; }
.qty.low { color: var(--warn); }
.qty.out { color: var(--bad); }

/* part detail */
.hero { display: flex; gap: 16px; align-items: flex-start; }
.hero .photo { width: 120px; height: 120px; border-radius: var(--radius); object-fit: cover; background: var(--bg); flex: none; cursor: zoom-in; }
.hero .photo.empty { display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 0.8rem; text-align: center; cursor: pointer; }
.stock-big { font-size: 2.6rem; font-weight: 800; line-height: 1; }
.actions { display: flex; gap: 8px; margin: 16px 0; }
dl.details { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
dl.details dt { color: var(--muted); font-size: 0.9rem; }
dl.details dd { margin: 0; overflow-wrap: anywhere; }

/* tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { text-align: left; padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 0.8rem; color: var(--muted); font-weight: 600; white-space: nowrap; }
td.num, th.num { text-align: right; }
tr.var-pos td { background: var(--brand-soft); }
tr.var-neg td { background: var(--bad-soft); }

/* stepper */
.stepper { display: flex; align-items: center; gap: 8px; }
.stepper button { width: 56px; height: 56px; font-size: 1.6rem; padding: 0; }
.stepper input { text-align: center; font-size: 1.6rem; font-weight: 700; height: 56px; }

/* dialog */
dialog {
  border: none; border-radius: 16px; padding: 0; width: min(520px, calc(100vw - 24px));
  background: var(--card); color: var(--text); box-shadow: 0 10px 40px rgba(0,0,0,0.3);
}
dialog::backdrop { background: rgba(0,0,0,0.45); }
dialog .dlg { padding: 20px; max-height: 85vh; overflow-y: auto; }
dialog h2 { margin-top: 0; }

/* scanner */
.scanner {
  position: fixed; inset: 0; z-index: 50; background: #000; color: #fff;
  display: flex; flex-direction: column;
}
.scanner .scan-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; padding-top: calc(12px + env(safe-area-inset-top)); }
.scanner .scan-head button { background: rgba(255,255,255,0.15); color: #fff; border: none; }
.scanner #reader { flex: 1; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.scanner #reader video { width: 100% !important; height: auto !important; max-height: 100%; object-fit: cover; }
.scanner .scan-foot { padding: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
.scanner .scan-foot input { background: rgba(255,255,255,0.1); color: #fff; border-color: rgba(255,255,255,0.3); }

/* toast */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 100; background: #222; color: #fff; padding: 10px 18px; border-radius: 10px; font-weight: 500;
  opacity: 0; pointer-events: none; transition: opacity 0.2s; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; }
#toast.error { background: var(--bad); }
@media (min-width: 900px) { #toast { bottom: 24px; } }

.login-box { max-width: 380px; margin: 40px auto; }
.menu a { display: flex; justify-content: space-between; padding: 14px 4px; border-bottom: 1px solid var(--line); color: inherit; text-decoration: none; font-weight: 500; }
.menu a:last-child { border-bottom: none; }

/* ---------- printable labels ---------- */
.labels-sheet { display: grid; gap: 0; }
.label {
  display: flex; align-items: center; gap: 2mm; padding: 2mm; overflow: hidden;
  border: 1px dashed #bbb; background: #fff; color: #000; break-inside: avoid;
}
.label svg { flex: none; }
.label .ltext { min-width: 0; font-family: Arial, sans-serif; }
.label .lcode { font-weight: 700; font-size: 11pt; }
.label .lname { font-size: 8pt; line-height: 1.15; max-height: 3.5em; overflow: hidden; }
.label .lloc { font-size: 7pt; color: #444; }
@media print {
  body { background: #fff; padding: 0; }
  .topbar, .tabbar, .no-print, #toast { display: none !important; }
  main { padding: 0; max-width: none; }
  .label { border-color: transparent; }
  .label.outline { border: 0.2mm solid #ccc; }
}

button svg, .btn svg { width: 24px; height: 24px; flex: none; }
@media (max-width: 600px) {
  .hero .photo { width: 88px; height: 88px; }
  .hero h1 { font-size: 1.2rem; }
  .hide-sm { display: none; }
}
