:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --ink: #17201d;
  --muted: #5d6a65;
  --line: #dde3e0;
  --brand: #1f6f5c;
  --brand-ink: #ffffff;
  --brand-soft: #e3f1ec;
  --received-bg: #fdf0d5; --received-ink: #7a4b00;
  --processed-bg: #dcebfb; --processed-ink: #154a85;
  --shipped-bg: #e1f3e3; --shipped-ink: #1f6430;
  --danger: #b3261e;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111614;
    --surface: #1a211e;
    --ink: #e8eeeb;
    --muted: #9aa8a2;
    --line: #2c3632;
    --brand: #4fb398;
    --brand-ink: #08201a;
    --brand-soft: #1d332c;
    --received-bg: #3a2c10; --received-ink: #f5c873;
    --processed-bg: #142a42; --processed-ink: #9cc6f5;
    --shipped-bg: #16301b; --shipped-ink: #93d6a0;
    --danger: #f2867e;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  padding-bottom: calc(76px + env(safe-area-inset-bottom, 0px));
}
@media (min-width: 800px) { body { padding-bottom: 32px; } }
a { color: var(--brand); }
h1 { font-size: 1.5rem; margin: 0 0 12px; }
h2 { font-size: 1.1rem; margin: 24px 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.warn { color: var(--danger); font-weight: 600; }

/* ---------- top & bottom navigation ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 24px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px;
  background: var(--brand); color: var(--brand-ink);
}
.brand { color: inherit; text-decoration: none; font-weight: 700; font-size: 1.1rem; }
.topnav { display: none; gap: 4px; flex-wrap: wrap; }
.topnav a { color: inherit; text-decoration: none; padding: 6px 12px; border-radius: 8px; opacity: 0.85; }
.topnav a.active, .topnav a:hover { background: rgb(255 255 255 / 0.18); opacity: 1; }
.topnav .nav-icon { display: none; }
@media (min-width: 800px) { .topnav { display: flex; } }

.bottomnav {
  position: fixed; inset: auto 0 0 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.bottomnav a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 10px; font-size: 0.75rem; color: var(--muted); text-decoration: none;
}
.bottomnav a.active { color: var(--brand); font-weight: 600; }
.bottomnav .nav-icon { font-size: 1.35rem; line-height: 1.2; }
.bottomnav .scan-tab .nav-icon {
  background: var(--brand); color: var(--brand-ink); border-radius: 50%;
  width: 44px; height: 44px; display: grid; place-items: center; margin-top: -18px;
  box-shadow: 0 2px 8px rgb(0 0 0 / 0.2);
}
@media (min-width: 800px) { .bottomnav { display: none; } }

.container { max-width: 960px; margin: 0 auto; padding: 16px; }

/* ---------- basic blocks ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin: 12px 0;
}
.card h2:first-child { margin-top: 0; }
.flash {
  background: var(--brand-soft); border: 1px solid var(--brand); color: var(--ink);
  padding: 12px 14px; border-radius: var(--radius); margin-bottom: 16px;
}
.flash-error { border-color: var(--danger); }
.page-head, .section-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.section-head h1, .section-head h2 { margin-bottom: 8px; }
.page-head p { margin: 0; }
.lot-title { font-size: 1.8rem; margin: 0; letter-spacing: 0.01em; }
.notes { white-space: pre-line; background: var(--bg); padding: 10px 12px; border-radius: 8px; margin: 12px 0 0; }

/* ---------- forms ---------- */
.stack { display: flex; flex-direction: column; gap: 14px; }
label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 0.95rem; }
label.check { flex-direction: row; align-items: center; gap: 10px; font-weight: 500; }
label.check input { width: 22px; height: 22px; }
input, select, textarea {
  font: inherit; font-weight: 400; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; padding: 12px; min-height: 48px; width: 100%;
}
input[type="file"] { padding: 10px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 1px; border-color: transparent; }
input[readonly] { background: var(--bg); color: var(--muted); }
fieldset { border: 0; padding: 0; margin: 0; }
legend { font-weight: 600; font-size: 0.95rem; margin-bottom: 6px; padding: 0; }
.input-row { display: flex; gap: 8px; align-items: stretch; }
.input-row > input, .input-row > select { flex: 1; min-width: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 480px) { .grid-2 { grid-template-columns: 1fr; } }

.choice-row { display: flex; gap: 8px; flex-wrap: wrap; }
.choice { flex: 1; min-width: 80px; }
.choice input { position: absolute; opacity: 0; pointer-events: none; }
.choice span {
  display: block; text-align: center; padding: 14px 8px; border: 2px solid var(--line);
  border-radius: 10px; font-weight: 700; background: var(--surface); cursor: pointer;
}
.choice input:checked + span { border-color: var(--brand); background: var(--brand-soft); color: var(--brand); }
.choice input:focus-visible + span { outline: 2px solid var(--brand); outline-offset: 2px; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
  min-height: 48px; padding: 10px 18px; border-radius: 10px; border: 1px solid transparent;
  white-space: nowrap;
}
.btn-primary { background: var(--brand); color: var(--brand-ink); }
.btn-secondary { background: var(--surface); color: var(--brand); border-color: var(--brand); }
.btn-ghost { background: transparent; color: var(--muted); border-color: var(--line); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-big { min-height: 56px; font-size: 1.05rem; }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.quick-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }

/* ---------- collapsible action cards ---------- */
details.action > summary, details.danger > summary {
  cursor: pointer; font-weight: 700; list-style: none; display: flex; justify-content: space-between; align-items: center;
}
details.action > summary::-webkit-details-marker { display: none; }
details.action > summary::after { content: "+"; font-size: 1.4rem; color: var(--muted); }
details.action[open] > summary::after { content: "–"; }
details[open] > summary { margin-bottom: 14px; }
details.danger > summary { color: var(--danger); }

/* ---------- status ---------- */
.badge {
  display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 0.8rem; font-weight: 700;
  background: var(--bg); color: var(--muted); border: 1px solid var(--line); white-space: nowrap;
}
.badge-received { background: var(--received-bg); color: var(--received-ink); border-color: transparent; }
.badge-processed { background: var(--processed-bg); color: var(--processed-ink); border-color: transparent; }
.badge-shipped { background: var(--shipped-bg); color: var(--shipped-ink); border-color: transparent; }

.steps { display: flex; list-style: none; padding: 0; margin: 16px 0; counter-reset: step; }
.steps li {
  flex: 1; text-align: center; font-size: 0.8rem; color: var(--muted); position: relative; padding-top: 28px;
}
.steps li::before {
  counter-increment: step; content: counter(step);
  position: absolute; top: 0; left: 50%; transform: translateX(-50%); z-index: 1;
  width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center;
  background: var(--surface); border: 2px solid var(--line); font-weight: 700; font-size: 0.75rem;
}
.steps li + li::after {
  content: ""; position: absolute; top: 11px; right: 50%; width: 100%; height: 2px; background: var(--line);
}
.steps li.done { color: var(--ink); font-weight: 600; }
.steps li.done::before { content: "✓"; background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.steps li.done + li.done::after { background: var(--brand); }

/* ---------- lists & tiles ---------- */
.lot-list { display: flex; flex-direction: column; gap: 8px; }
.lot-card {
  display: block; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; color: inherit; text-decoration: none;
}
.lot-card:hover { border-color: var(--brand); }
.lot-card.inactive, details.inactive { opacity: 0.6; }
.lot-card-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.lot-no { font-size: 1.05rem; }
.lot-card-meta { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 4px; color: var(--muted); font-size: 0.9rem; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; margin-bottom: 8px; }
.tile {
  display: flex; flex-direction: column; gap: 2px; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 16px; color: inherit; text-decoration: none;
}
a.tile:hover { border-color: var(--brand); }
.tile-label { color: var(--muted); font-size: 0.85rem; font-weight: 600; }
.tile-value { font-size: 1.9rem; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.tile-sub { color: var(--muted); font-size: 0.85rem; }

.two-col { display: grid; gap: 0 24px; }
@media (min-width: 800px) { .two-col { grid-template-columns: 1fr 1fr; } }

.tabs { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 12px; padding-bottom: 2px; }
.tabs a {
  padding: 8px 14px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line);
  color: var(--ink); text-decoration: none; white-space: nowrap; font-size: 0.9rem;
}
.tabs a.active { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); font-weight: 600; }
.filters { display: grid; grid-template-columns: 2fr 1fr 1fr auto; gap: 8px; }
@media (max-width: 640px) { .filters { grid-template-columns: 1fr 1fr; } .filters input[type="search"] { grid-column: 1 / -1; } }

.facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px 16px; margin: 0; }
.facts dt { color: var(--muted); font-size: 0.8rem; font-weight: 600; }
.facts dd { margin: 2px 0 0; font-weight: 600; font-variant-numeric: tabular-nums; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; font-size: 0.92rem; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 0.82rem; }
tbody tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

.rack-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 8px; }
.rack-tile {
  display: flex; flex-direction: column; gap: 4px; min-height: 84px; padding: 12px;
  background: var(--brand-soft); border: 1px solid var(--brand); border-radius: var(--radius);
  color: var(--ink); text-decoration: none; overflow-wrap: anywhere;
}
.rack-tile strong { font-size: 1.2rem; }
.rack-tile.empty { background: var(--surface); border-color: var(--line); color: var(--muted); }

.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; margin-bottom: 12px; }
.photos img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px; display: block; }

.history { list-style: none; padding: 0; margin: 0; }
.history li { display: flex; flex-direction: column; padding: 10px 0; border-bottom: 1px solid var(--line); }

.menu { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.menu a { padding: 16px; color: var(--ink); text-decoration: none; font-weight: 600; border-bottom: 1px solid var(--line); }
.menu a:last-child { border-bottom: 0; }
.menu a::after { content: "›"; float: right; color: var(--muted); }

.auth-box { max-width: 400px; margin: 40px auto; }

/* ---------- camera scanner overlay ---------- */
.scan-overlay {
  position: fixed; inset: 0; z-index: 100; background: rgb(0 0 0 / 0.85);
  display: grid; place-items: center; padding: 16px;
}
.scan-box { width: min(480px, 100%); display: flex; flex-direction: column; gap: 12px; color: #fff; text-align: center; }
.scan-title { font-weight: 700; font-size: 1.1rem; }
#scan-reader { width: 100%; border-radius: var(--radius); overflow: hidden; background: #000; min-height: 240px; }
#scan-reader video { width: 100% !important; display: block; }
.scan-msg { margin: 0; min-height: 1.4em; }

/* ---------- weight balance ---------- */
.balance {
  margin: 0; padding: 10px 12px; border-radius: 10px; font-weight: 600; font-size: 0.92rem;
  background: var(--brand-soft); color: var(--ink); font-variant-numeric: tabular-nums;
}
.balance.balance-bad { background: transparent; border: 2px solid var(--danger); color: var(--danger); }
.table-wrap.flat { border: 0; border-radius: 0; background: transparent; }
.table-wrap.flat td { white-space: normal; }
tfoot td { font-weight: 700; border-top: 2px solid var(--ink); border-bottom: 0; }
.output-rows { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.output-row { display: grid; grid-template-columns: 1fr 7.5rem auto; gap: 8px; }
.output-row .btn { min-width: 48px; padding: 0; font-size: 1.3rem; }
.field-hint { margin: 0 0 8px; }
tr.subtotal td { font-weight: 700; }
.line-rows { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.line-row {
  position: relative; padding: 12px 56px 12px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}
.line-row .row-remove { position: absolute; top: 8px; right: 8px; min-width: 40px; min-height: 40px; padding: 0; font-size: 1.3rem; }
.line-rows:not([data-rows]) .line-row { padding-right: 12px; }
.small-label { font-size: 0.85rem; }
