/* Transition-Kit Inventory — mobile-first, no framework. */

:root {
  color-scheme: light dark;       /* follow the OS light/dark setting */
  /* Neutral slate palette, soft blue accent. Sidebar is its own persistent navy. */
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #e1e4e9;
  --text: #1d242e;
  --muted: #646e7d;
  --primary: #3b6fd4;
  --primary-soft: rgba(59,111,212,.12);
  --primary-ink: #ffffff;
  --green: #1f9d57;
  --amber: #c4791b;
  --red: #d33b3b;
  --green-bg: #def0e4;
  --amber-bg: #f6ecd4;
  --red-bg: #f6dede;
  --track: #e6e9ee;            /* stock-bar track */
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 1px 2px rgba(16,24,40,.03);
  --radius: 5px;
  --radius-sm: 4px;
  --maxw: 1240px;
  /* Sidebar: persistent dark navy in both themes (Click-Maint style) */
  --side-bg: #0e1722;
  --side-bg-2: #16222f;
  --side-text: #c5cedb;
  --side-muted: #7c899b;
  --side-border: #1d2a39;
  --side-active-bg: rgba(91,141,239,.16);
  --side-active-text: #ffffff;
  --side-accent: #5b8def;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151b;
    --surface: #181d25;
    --surface-2: #212833;
    --border: #2a323d;
    --text: #e4e8ee;
    --muted: #8a94a3;
    --primary: #5b8def;
    --primary-soft: rgba(91,141,239,.16);
    --primary-ink: #ffffff;
    --green: #35b06f;
    --amber: #d69a3a;
    --red: #e1605f;
    --green-bg: #14271c;
    --amber-bg: #2a2412;
    --red-bg: #2a1819;
    --track: #2a323d;
    --shadow: 0 1px 2px rgba(0,0,0,.45);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

.muted { color: var(--muted); font-weight: 400; }
.hidden, [hidden] { display: none !important; }

/* ---------- Buttons ---------- */
.btn {
  font: inherit;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: .5rem .8rem;
  border-radius: 6px;
  cursor: pointer;
  line-height: 1;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: transparent; font-weight: 600; }
.btn-primary:hover { filter: brightness(1.07); background: var(--primary); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { color: var(--red); border-color: var(--red); background: transparent; }
.btn-danger:hover { background: var(--red-bg); }
.btn-sm { padding: .3rem .5rem; font-size: .85rem; border-radius: var(--radius-sm); }
.btn-block { display: block; width: 100%; }

/* ---------- Gate ---------- */
.gate {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.25rem;
}
.gate-card {
  width: min(380px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: .85rem;
}
.gate-card h1 { font-size: 1.25rem; margin: 0; }
.gate-card input {
  font: inherit;
  padding: .7rem .8rem;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--text);
}
.gate-msg { color: var(--red); margin: 0; font-size: .9rem; }

/* ---------- Banner ---------- */
.banner {
  padding: .6rem 1rem;
  font-size: .9rem;
  background: var(--amber-bg);
  color: var(--text);
  text-align: center;
  border-bottom: 1px solid var(--border);
}
.banner.info { background: var(--surface-2); }
.banner.error { background: var(--red-bg); }

/* ---------- App layout: sidebar + main ---------- */
.layout { display: flex; align-items: stretch; min-height: 100dvh; }
.save-status { font-size: .78rem; color: var(--side-muted); white-space: nowrap; }
.save-status.saving { color: var(--amber); }
.save-status.saved  { color: var(--green); }
.save-status.error  { color: var(--red); }

/* ---------- Sidebar (persistent navy) ---------- */
.sidebar {
  position: sticky; top: 0; align-self: flex-start;
  height: 100dvh; width: 232px; flex-shrink: 0;
  background: var(--side-bg); color: var(--side-text);
  border-right: 1px solid var(--side-border);
  display: flex; flex-direction: column; gap: .35rem;
  padding: 1rem .7rem .8rem;
}
.side-brand { display: flex; align-items: center; gap: .6rem; padding: .15rem .4rem .9rem; min-width: 0; }
.side-brand .titles { display: flex; flex-direction: column; min-width: 0; }
.side-brand strong { font-size: .95rem; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-brand .muted { color: var(--side-muted); font-size: .74rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-nav { display: flex; flex-direction: column; gap: .15rem; }
.side-link {
  font: inherit; text-align: left; cursor: pointer;
  display: flex; align-items: center; gap: .65rem;
  padding: .55rem .65rem; border-radius: 6px;
  border: none; background: transparent; color: var(--side-text);
}
.side-link:hover { background: var(--side-bg-2); }
.side-link.active { background: var(--side-active-bg); color: var(--side-active-text); font-weight: 600; box-shadow: inset 2px 0 0 var(--side-accent); }
.side-ico { display: inline-flex; width: 18px; height: 18px; color: currentColor; opacity: .85; flex-shrink: 0; }
.side-ico svg { width: 18px; height: 18px; }
.side-link.active .side-ico { opacity: 1; }
.side-label { min-width: 0; }
.side-foot { margin-top: auto; display: flex; flex-direction: column; gap: .4rem; padding: .6rem .35rem 0; border-top: 1px solid var(--side-border); }
.side-foot-actions { display: flex; gap: .3rem; flex-wrap: wrap; }
.side-foot .btn-ghost { color: var(--side-muted); }
.side-foot .btn-ghost:hover { background: var(--side-bg-2); color: var(--side-text); }
.side-nav { flex: 1; }
.side-spacer { flex: 1; min-height: .25rem; }

/* Expandable "Build Tree" group + product sub-links */
.side-group { display: flex; flex-direction: column; }
.side-grouphead { display: flex; align-items: center; gap: .15rem; border-radius: 6px; }
.side-grouphead .side-link-main { flex: 1; }
.side-grouphead.active .side-link-main { color: var(--side-active-text); }
.side-caret {
  border: none; background: transparent; color: var(--side-muted); cursor: pointer;
  font-size: .8rem; padding: .3rem .45rem; border-radius: 6px; transition: transform .12s ease; flex-shrink: 0;
}
.side-caret:hover { background: var(--side-bg-2); color: var(--side-text); }
.side-caret.open { transform: rotate(90deg); }
.side-sub { display: flex; flex-direction: column; gap: .1rem; margin: .1rem 0 .2rem; padding-left: .85rem; }
.side-sublink {
  font: inherit; text-align: left; cursor: pointer; font-size: .86rem;
  color: var(--side-muted); background: transparent; border: none;
  padding: .4rem .55rem; border-radius: 6px; border-left: 2px solid var(--side-border);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.side-sublink:hover { background: var(--side-bg-2); color: var(--side-text); }
.side-sublink.active { color: var(--side-active-text); background: var(--side-active-bg); border-left-color: var(--side-accent); font-weight: 600; }
.side-subempty { font-size: .8rem; color: var(--side-muted); padding: .35rem .55rem; font-style: italic; }

/* ---- Build Tree: products overview cards ---- */
.prod-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1rem; }
.prod-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column; }
.prod-card-img { display: block; width: 100%; aspect-ratio: 16 / 10; border: none; padding: 0; cursor: pointer; background: var(--surface-2); }
.prod-card-body { padding: .8rem .9rem 1rem; display: flex; flex-direction: column; gap: .55rem; }
.prod-card-name { font-weight: 700; font-size: 1.02rem; }
.prod-card-stats { display: flex; gap: 1.3rem; }
.prod-card-stats b { display: block; font-size: 1.4rem; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.prod-card-stats span { font-size: .76rem; color: var(--muted); }
.prod-card-bn { font-size: .85rem; }
.prod-card-foot { display: flex; gap: .4rem; margin-top: .1rem; }

/* ---- Single product page ---- */
.prod-head { display: flex; align-items: flex-start; gap: .8rem; min-width: 0; }
.prod-head h2 { font-size: 1.25rem; }
.add-config { padding: .9rem 1rem; }
.thumb.md { width: 56px; height: 56px; }

/* product type badge */
.tag-product { color: var(--primary); background: var(--primary-soft); border-color: transparent; }
.tag-done { color: var(--green); background: var(--green-bg); border-color: transparent; }

/* sidebar count / warning badge */
.side-badge { margin-left: auto; min-width: 1.2rem; height: 1.2rem; padding: 0 .35rem; border-radius: 999px;
  font-size: .72rem; font-weight: 700; display: inline-flex; align-items: center; justify-content: center;
  background: var(--side-bg-2); color: var(--side-text); }
.side-badge.warn { background: var(--red); color: #fff; }
@media (max-width: 860px) { .side-badge { margin-left: .35rem; } }

/* ---- Production: over-allocation warning ---- */
.warn-panel { border-color: var(--red); }
.warn-panel > strong { color: var(--red); display: block; margin-bottom: .5rem; }
.warn-list { display: flex; flex-direction: column; gap: .25rem; font-size: .88rem; }
.warn-list > div { display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.warn-list .short { color: var(--red); }

/* ---- Production: batch cards ---- */
.batch-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 1rem; }
.batch-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: .9rem 1rem; display: flex; flex-direction: column; gap: .7rem; }
.batch-head { display: flex; align-items: center; gap: .6rem; }
.batch-title { min-width: 0; }
.batch-name { font-weight: 700; }
.batch-sub { font-size: .8rem; }
.batch-progress { display: flex; align-items: center; gap: .6rem; }
.batch-progress .progress { flex: 1; margin: 0; }
.batch-progress > .muted { font-size: .78rem; white-space: nowrap; }
.stages { display: flex; flex-direction: column; gap: .15rem; }
.stage { display: flex; align-items: center; gap: .5rem; padding: .3rem .4rem; border-radius: 6px; cursor: pointer; font-size: .9rem; }
.stage:hover { background: var(--surface-2); }
.stage input { width: auto; }
.stage.done span { text-decoration: line-through; color: var(--muted); }
.batch-foot { display: flex; gap: .4rem; margin-top: .1rem; }
.batch-foot .btn { flex: 1; }

/* ---------- Main column ---------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.view { width: 100%; max-width: var(--maxw); margin: 0 auto; padding: 1.3rem 1.4rem; }
.appfoot { width: 100%; max-width: var(--maxw); margin: 0 auto; padding: 1rem 1.4rem 2.5rem; font-size: .8rem; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin: .25rem 0 1rem; flex-wrap: wrap; }
.section-head h2 { margin: 0; font-size: 1.15rem; }
.row-actions { display: flex; gap: .4rem; flex-wrap: wrap; }

/* ---------- Cards / dashboard ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .75rem; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: .9rem 1rem; box-shadow: var(--shadow);
}
.card .label { font-size: .8rem; color: var(--muted); }
.card .value { font-size: 1.7rem; font-weight: 700; line-height: 1.1; margin-top: .2rem; }
.card .sub { font-size: .8rem; color: var(--muted); margin-top: .2rem; }

.hero {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.1rem 1.2rem; box-shadow: var(--shadow);
  margin-bottom: 1rem;
}
.hero .big { font-size: 2.4rem; font-weight: 800; line-height: 1; }
.progress { height: 12px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin: .6rem 0 .35rem; }
.progress > span { display: block; height: 100%; background: var(--primary); }

.cat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: .75rem; margin-top: 1rem; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
table.grid { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.grid th, table.grid td { padding: .72rem .75rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.grid tbody tr { height: 56px; }
table.grid tbody tr:hover { background: var(--surface-2); }
table.grid th { font-size: .74rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); position: sticky; top: 0; background: var(--surface); z-index: 1; }
table.grid tr:last-child td { border-bottom: none; }
table.grid td.num, table.grid th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.grid tfoot td { font-weight: 700; background: var(--surface-2); }
.item-name { font-weight: 600; }
.item-spec { color: var(--muted); font-size: .82rem; }

/* On-hand direct-edit input (no steppers). direction:rtl puts the native
   number spinners on the LEFT so they don't crowd the right-aligned value. */
.onhand-input {
  width: 4.8rem; font: inherit; direction: rtl; text-align: right; padding: .4rem .5rem;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); color: var(--text);
  font-variant-numeric: tabular-nums;
}
.onhand-input:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.onhand-cell { width: 6rem; }

/* Stock-level bar (replaces status pills). Track is tinted by status so the
   colour reads even at 0% fill; fill shows how stocked vs. needed. */
.stock-cell { width: 130px; }
.bar { display: block; width: 100%; max-width: 120px; height: 8px; border-radius: 999px; background: var(--track); overflow: hidden; }
.bar-fill { display: block; height: 100%; border-radius: 999px; background: var(--muted); transition: width .2s ease; }
.bar-stocked, .bar-ok { background: var(--green-bg); }
.bar-low { background: var(--amber-bg); }
.bar-order { background: var(--red-bg); }
.bar-stocked .bar-fill, .bar-ok .bar-fill { background: var(--green); }
.bar-low .bar-fill { background: var(--amber); }
.bar-order .bar-fill { background: var(--red); }
.bar-untracked { background: var(--track); }
.bar-untracked .bar-fill { background: color-mix(in srgb, var(--muted) 30%, transparent); }

/* Status pills (still used in dashboard/elsewhere) */
.pill { display: inline-block; padding: .15rem .55rem; border-radius: var(--radius-sm); font-size: .78rem; font-weight: 600; white-space: nowrap; }
.pill.done    { background: var(--green-bg); color: var(--green); }
.pill.stocked { background: var(--green-bg); color: var(--green); }
.pill.low     { background: var(--amber-bg); color: var(--amber); }
.pill.order   { background: var(--red-bg); color: var(--red); }

/* ---------- Forms ---------- */
.form-grid { display: grid; gap: .7rem; }
.field { display: flex; flex-direction: column; gap: .25rem; }
.field label { font-size: .82rem; color: var(--muted); }
.field input, .field select, .field textarea {
  font: inherit; padding: .55rem .65rem; border: 1px solid var(--border);
  border-radius: 6px; background: var(--surface-2); color: var(--text);
}
.field textarea { resize: vertical; min-height: 2.4rem; }
.inline-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px,1fr)); gap: .6rem; align-items: end; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: grid; place-items: center; padding: 1rem; z-index: 50;
}
.modal {
  width: min(460px, 100%); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(0,0,0,.3); padding: 1.2rem; max-height: 90dvh; overflow: auto;
}
.modal h3 { margin: 0 0 .8rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; }

/* Settings blocks */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem 1.1rem; margin-bottom: 1rem; }
.panel h2 { margin: 0 0 .8rem; font-size: 1.05rem; }
.cat-line { display: flex; align-items: center; justify-content: space-between; gap: .6rem; padding: .5rem 0; border-bottom: 1px solid var(--border); }
.cat-line:last-child { border-bottom: none; }

/* Responsive: turn data tables into cards on narrow screens */
@media (max-width: 720px) {
  table.grid, table.grid tbody, table.grid tr, table.grid td { display: block; width: 100%; }
  table.grid thead, table.grid tfoot { display: none; }
  table.grid tbody tr { height: auto; }
  table.grid tr { border-bottom: 8px solid var(--bg); padding: .4rem .2rem; }
  table.grid td { border: none; padding: .3rem .6rem; display: flex; justify-content: space-between; gap: 1rem; align-items: center; }
  table.grid td::before {
    content: attr(data-label);
    font-size: .76rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted);
  }
  table.grid td.item-cell { display: block; }
  table.grid td.item-cell::before { display: block; margin-bottom: .15rem; }
  .stock-cell .bar { max-width: 160px; }
}

/* Collapse the sidebar into a top bar on narrow screens */
@media (max-width: 860px) {
  .layout { flex-direction: column; min-height: 0; }
  .sidebar {
    position: sticky; top: 0; z-index: 20;
    height: auto; width: 100%; flex-direction: row; align-items: center; gap: .5rem;
    padding: .5rem .8rem; border-right: none; border-bottom: 1px solid var(--side-border);
    overflow-x: auto;
  }
  .side-brand { padding: 0 .4rem 0 0; flex-shrink: 0; }
  .side-nav { flex-direction: row; gap: .2rem; flex: 1; align-items: center; }
  .side-link { padding: .45rem .6rem; white-space: nowrap; }
  .side-link.active { box-shadow: inset 0 -2px 0 var(--side-accent); }
  .side-group { flex-direction: row; }   /* group head only; sub-links hidden below */
  .side-sub, .side-caret, .side-spacer { display: none; }
  .side-foot { margin: 0; flex-direction: row; align-items: center; padding: 0; border-top: none; flex-shrink: 0; }
}
@media (max-width: 560px) {
  .side-label { display: none; }   /* icon-only nav on phones */
  .side-brand .titles { display: none; }
}

/* ===================== v2: enterprise chrome ===================== */
.brand {
  width: 30px; height: 30px; border-radius: 7px; flex-shrink: 0;
  background: linear-gradient(135deg, var(--primary), #1746a2);
  color: #fff; font-weight: 800; font-size: .8rem; letter-spacing: .02em;
  display: grid; place-items: center;
}
.pill.ok { background: var(--green-bg); color: var(--green); }
.tag { display: inline-block; font-size: .66rem; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); background: var(--surface-2); border: 1px solid var(--border);
  padding: .04rem .4rem; border-radius: 5px; vertical-align: middle; font-weight: 600; }

.search { flex: 1; min-width: 0; max-width: 420px; font: inherit; padding: .55rem .75rem;
  border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); }
.search:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }

th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover { color: var(--text); }

.kpi-row { display: flex; gap: 1.6rem; flex-wrap: wrap; margin: .2rem 0 .7rem; }
.kpi-row .big { font-size: 1.9rem; font-weight: 800; line-height: 1; }

/* ---- Build tree (aligned columns) ---- */
.tree { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.t-row {
  display: grid; align-items: center;
  grid-template-columns: minmax(0, 1fr) 5.5rem 5.5rem 6rem 140px 9rem;
  gap: .5rem; padding: .55rem .85rem; border-bottom: 1px solid var(--border); min-height: 52px;
}
.t-row:last-child { border-bottom: none; }
.t-row:not(.t-head):hover { background: var(--surface-2); }
.t-head { background: var(--surface-2); min-height: 0; padding-top: .5rem; padding-bottom: .5rem; position: sticky; top: 0; z-index: 1; }
.t-head .t-label, .t-head .t-col { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
.t-label { display: flex; align-items: center; gap: .5rem; min-width: 0; }
.t-col { text-align: right; font-variant-numeric: tabular-nums; font-size: .9rem; white-space: nowrap; }
.t-bar-col { text-align: left; }
.caret { border: none; background: transparent; cursor: pointer; color: var(--muted); font-size: .9rem; width: 1.2rem; padding: 0; transition: transform .12s ease; flex-shrink: 0; }
.caret.open { transform: rotate(90deg); }
.caret-spacer { width: 1.2rem; display: inline-block; flex-shrink: 0; }
.t-name { font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.t-actions { display: flex; gap: .3rem; justify-content: flex-end; }

/* ---- BOM editor in the item modal ---- */
.modal.wide { width: min(640px, 100%); }
.form-grid.two { grid-template-columns: 1fr 1fr; }
.field.span2 { grid-column: 1 / -1; }
.bom-editor { margin-top: .8rem; border-top: 1px solid var(--border); padding-top: .7rem; }
.bom-row { display: flex; align-items: center; gap: .5rem; padding: .25rem 0; }
.bom-name { flex: 1; min-width: 0; }
.bom-qty { width: 4.5rem; font: inherit; padding: .35rem; border: 1px solid var(--border); border-radius: 7px; background: var(--surface-2); color: var(--text); text-align: center; }
.bom-add { display: flex; gap: .5rem; align-items: center; margin-top: .55rem; }
.bom-add select { flex: 1; min-width: 0; font: inherit; padding: .4rem; border: 1px solid var(--border); border-radius: 7px; background: var(--surface-2); color: var(--text); }

@media (max-width: 720px) {
  /* Tree: drop To-order + Buildable columns; keep Item | On hand | Stock | actions */
  .t-row { grid-template-columns: minmax(0, 1fr) 3.6rem 110px 5rem; gap: .4rem; padding: .55rem .6rem; }
  .t-row .t-col:nth-child(3), .t-row .t-col:nth-child(4) { display: none; }
}
@media (max-width: 560px) {
  .form-grid.two { grid-template-columns: 1fr; }
  .search { max-width: none; }
}

/* ===================== v3: categories, images, grid, modal redo ===================== */

/* ---- Thumbnails ---- */
.thumb {
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm); overflow: hidden; background: var(--surface-2);
  border: 1px solid var(--border); flex-shrink: 0;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.xs { width: 26px; height: 26px; border-radius: 4px; }
.thumb.sm { width: 40px; height: 40px; }
.thumb.lg { width: 100%; height: 100%; border-radius: 0; border: none; }
.thumb-ph { color: var(--muted); font-weight: 700; font-size: .72rem; letter-spacing: .02em; }
.thumb.lg.thumb-ph { font-size: 1.6rem; }

/* ---- Catalog toolbar (search + controls + New item on one line) ---- */
.cat-bar { display: flex; align-items: center; gap: .55rem; flex-wrap: wrap; margin: 0 0 .8rem; }
.cat-bar .search { flex: 1 1 220px; max-width: none; }
.cat-bar .cat-new { margin-left: auto; }
@media (max-width: 720px) { .cat-bar .cat-new { margin-left: 0; } }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; background: var(--surface); }
.seg-btn { font: inherit; border: none; background: transparent; color: var(--muted); padding: .42rem .72rem; cursor: pointer; }
.seg-btn + .seg-btn { border-left: 1px solid var(--border); }
.seg-btn.on { background: var(--primary); color: var(--primary-ink); font-weight: 600; }
.sort-ctl { display: inline-flex; align-items: center; gap: .35rem; font-size: .85rem; color: var(--muted); }
.sort-ctl select { font: inherit; padding: .4rem .45rem; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); }
.btn-on { background: var(--primary); color: var(--primary-ink); border-color: transparent; font-weight: 600; }
.btn-on:hover { background: var(--primary); filter: brightness(1.07); }

/* ---- Category filter chips ---- */
.chips { display: flex; gap: .4rem; flex-wrap: wrap; margin: 0 0 1rem; }
.chip {
  font: inherit; font-size: .82rem; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: .28rem .6rem; border-radius: 999px; display: inline-flex; align-items: center; gap: .35rem;
}
.chip:hover { background: var(--surface-2); }
.chip.on { background: var(--primary); color: var(--primary-ink); border-color: transparent; font-weight: 600; }
.chip-n { font-size: .72rem; opacity: .7; font-variant-numeric: tabular-nums; }
.cat-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--primary); margin-right: .35rem; vertical-align: middle; }

/* ---- Catalog table: thumb + name cell ---- */
.item-main { display: flex; align-items: center; gap: .6rem; }
.item-text { min-width: 0; }

/* ---- Catalog image grid ---- */
.icards { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: .8rem; }
.icard {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column;
}
.icard-img {
  position: relative; display: block; width: 100%; aspect-ratio: 1 / 1;
  border: none; padding: 0; cursor: pointer; background: var(--surface-2);
}
.icard-body { padding: .6rem .7rem .7rem; display: flex; flex-direction: column; gap: .35rem; }
.icard-name { font-weight: 600; font-size: .9rem; line-height: 1.25; }
.icard-meta { font-size: .78rem; color: var(--muted); }
.icard-bar { padding: .1rem 0 .15rem; }
.icard-foot { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-top: .15rem; }
.onhand-mini { display: inline-flex; align-items: center; gap: .4rem; font-size: .78rem; color: var(--muted); }
.onhand-mini .onhand-input { width: 3.6rem; }

/* ---- Item modal: scrollable body + sticky footer ---- */
.modal.wide { width: min(780px, 100%); }
.modal:has(.modal-scroll) { padding: 0; display: flex; flex-direction: column; }
.modal-head { padding: 1.1rem 1.2rem .4rem; }
.modal-head h3 { margin: 0; }
.modal-scroll { flex: 1 1 auto; overflow: auto; padding: .3rem 1.2rem 1rem; }
.modal:has(.modal-scroll) .modal-actions {
  margin: 0; padding: .8rem 1.2rem; border-top: 1px solid var(--border);
  position: sticky; bottom: 0; background: var(--surface);
}

/* item form: image column + fields */
.item-form { display: grid; grid-template-columns: 160px 1fr; gap: 1rem; align-items: start; }
.img-col { display: flex; flex-direction: column; gap: .5rem; }
.img-drop { width: 160px; height: 160px; border: 1px dashed var(--border); border-radius: 6px;
  background: var(--surface-2); overflow: hidden; cursor: pointer; }
.img-preview { width: 100%; height: 100%; display: grid; place-items: center; }
.img-preview img { width: 100%; height: 100%; object-fit: cover; }
.img-preview.empty { cursor: pointer; }
.img-hint { color: var(--muted); font-size: .85rem; }
.img-actions { display: flex; gap: .4rem; }
.fields-col { display: flex; flex-direction: column; gap: .7rem; min-width: 0; }
.fields-col .field select { font: inherit; padding: .55rem .65rem; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-2); color: var(--text); }
.form-grid.four { grid-template-columns: repeat(4, 1fr); margin-top: .9rem; }
.form-grid .field { min-width: 0; }
.form-grid .field input, .form-grid .field select, .form-grid .field textarea { min-width: 0; width: 100%; max-width: 100%; }
.field.span2 { grid-column: span 2; }
.field.span3 { grid-column: span 3; }
.field.span4 { grid-column: 1 / -1; }
label.check { display: flex; align-items: center; gap: .5rem; flex-direction: row; color: var(--text); }
label.check input { width: auto; }

@media (max-width: 640px) {
  .item-form { grid-template-columns: 1fr; }
  .img-col { align-items: center; }
  .form-grid.four { grid-template-columns: 1fr 1fr; }
  .field.span3, .field.span4 { grid-column: 1 / -1; }
}
