:root {
  --bg: #f2f3f5;
  --card: #ffffff;
  --text: #1c1d1f;
  --muted: #868b93;
  --border: #e6e7eb;
  --accent: #00ae42;
  --accent-ink: #ffffff;
  --accent-soft: #e3f6ea;
  --track: #e8e9ed;
  --danger: #e5484d;
  --warn: #e69500;
  --field: #f4f5f7;
  --flange: #3b3e43;
  --core: #c9ccd1;
  --box: #e7e1d6;
  --box-line: rgba(0, 0, 0, .14);
  --plank: #d3cbbd;
  --plank-edge: #b9b0a0;
  --fil-edge: rgba(0, 0, 0, .18);
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 2px 10px rgba(0, 0, 0, .04);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101113;
    --card: #1b1d20;
    --text: #f1f2f3;
    --muted: #8d929a;
    --border: #2a2d32;
    --accent: #00c04b;
    --accent-ink: #04130a;
    --accent-soft: #0f2a19;
    --track: #2d3036;
    --field: #24272b;
    --flange: #5b5f66;
    --core: #3a3d43;
    --box: #cfc8bb;
    --box-line: rgba(0, 0, 0, .16);
    --plank: #6e6250;
    --plank-edge: #463d31;
    --fil-edge: rgba(255, 255, 255, .28);
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
svg { display: block; }
[hidden] { display: none !important; }

/* --- header ----------------------------------------------------------- */

.top {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding: calc(12px + env(safe-area-inset-top)) 16px 8px;
}
.top-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
h1 { margin: 0; font-size: 28px; letter-spacing: -.02em; }
.top-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.top-actions { display: flex; align-items: center; gap: 6px; padding-top: 4px; }

.pill {
  border: 0; border-radius: 999px; padding: 5px 10px 5px 8px;
  font-size: 12px; font-weight: 600; background: var(--card); box-shadow: var(--shadow);
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill.ok::before { background: var(--accent); }
.pill.wait::before { background: var(--warn); }
.pill.bad { color: var(--danger); }
.pill.bad::before { background: var(--danger); }

.icon-btn {
  width: 36px; height: 36px; border: 0; border-radius: 50%;
  background: var(--card); box-shadow: var(--shadow);
  display: inline-grid; place-items: center; flex: none;
}
.icon-btn svg { width: 20px; height: 20px; fill: currentColor; }
.icon-btn.danger { color: var(--danger); background: transparent; box-shadow: none; }

.tool-row { display: flex; gap: 8px; margin-top: 12px; }
.search {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px;
  background: var(--card); border-radius: 12px; padding: 0 12px; box-shadow: var(--shadow);
}
.search svg { width: 18px; height: 18px; fill: var(--muted); flex: none; }
.search input { flex: 1; min-width: 0; border: 0; background: none; outline: none; padding: 10px 0; font-size: 16px; }

.seg {
  display: flex; background: var(--track); border-radius: 10px; padding: 2px; gap: 2px;
}
.seg button {
  flex: 1; border: 0; background: none; border-radius: 8px; padding: 6px 8px;
  font-size: 13px; font-weight: 600; color: var(--muted); white-space: nowrap;
}
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.view-toggle { flex: none; }
.view-toggle button { padding: 0 12px; }

.chips {
  display: flex; gap: 6px; overflow-x: auto; margin: 10px -16px 0; padding: 0 16px 2px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chips { -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 28px), transparent); mask-image: linear-gradient(90deg, #000 calc(100% - 28px), transparent); }
.top-row, .tool-row, .legend-row { max-width: 1068px; margin-left: auto; margin-right: auto; }
@media (min-width: 1100px) {
  .chips { max-width: 1068px; margin: 10px auto 0; padding: 0; -webkit-mask-image: none; mask-image: none; }
}

.legend-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 10px; }
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); padding: 2px 4px 10px; }
.sort-toggle button { padding: 5px 10px; font-size: 12.5px; }

.switch {
  display: inline-flex; align-items: center; gap: 8px; flex: none; cursor: pointer;
  font-size: 12.5px; font-weight: 600; color: var(--muted); white-space: nowrap;
}
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .track {
  position: relative; width: 38px; height: 22px; border-radius: 999px; flex: none;
  background: var(--track); transition: background .2s;
}
.switch .track::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .3); transition: transform .2s;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(16px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }
.legend > span { display: inline-flex; align-items: center; gap: 6px; }
.legend svg { width: 30px; height: auto; }
.legend .badge.static { position: static; box-shadow: none; min-width: 0; padding: 0 5px; font-size: 10px; line-height: 14px; font-style: normal; }
.chip {
  border: 0; border-radius: 999px; padding: 6px 14px; font-size: 13px; font-weight: 600;
  background: var(--card); color: var(--text); white-space: nowrap; box-shadow: var(--shadow);
}
.chip.on { background: var(--accent); color: var(--accent-ink); }

main { padding: 4px 16px 0; max-width: 1100px; margin: 0 auto; }
.foot { text-align: center; color: var(--muted); font-size: 12px; margin: 24px 16px; }

/* --- shelf view --------------------------------------------------------- */

.shelf {
  background: var(--card); border-radius: 16px; box-shadow: var(--shadow);
  padding: 12px 12px 14px; margin-bottom: 12px;
}
.shelf-head { display: flex; align-items: baseline; gap: 8px; padding: 0 4px 6px; flex-wrap: wrap; }
.group-title { font-weight: 700; font-size: 17px; }
.group-meta { color: var(--muted); font-size: 12.5px; }

.shelf-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
  column-gap: 0; row-gap: 14px;
  align-items: end;
}
.tower {
  border: 0; background: none; padding: 0; min-width: 0;
  display: flex; flex-direction: column; align-items: stretch; text-align: center;
}
.stack {
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  gap: 2px; padding: 10px 6px 0; min-height: 34px;
  transition: transform .15s;
}
.tower:active .stack { transform: translateY(-3px); }
.pile {
  position: relative; width: 100%; max-width: 60px;
  display: flex; flex-direction: column; gap: 1px;
}
.pile svg { width: 100%; height: auto; }
.pile.cartons .badge { top: auto; bottom: 4px; }
.badge {
  position: absolute; top: -7px; right: -5px; z-index: 1;
  min-width: 20px; padding: 1px 6px; border-radius: 999px;
  font-size: 11px; font-weight: 700; line-height: 16px;
  background: var(--accent); color: var(--accent-ink);
  box-shadow: 0 0 0 2px var(--card);
}
.badge.part { background: var(--text); color: var(--card); }
.tower.low .badge { background: var(--warn); color: #fff; }
.plank {
  height: 6px; background: var(--plank);
  border-bottom: 2px solid var(--plank-edge);
}
.tower-name, .tower-sub {
  display: block; padding: 0 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tower-label { display: block; height: 56px; padding-top: 6px; } /* 2-line name + type */
.tower-name {
  font-size: 12px; font-weight: 600; line-height: 1.25;
  white-space: normal; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2;
  overflow-wrap: anywhere;
}
.tower-sub { font-size: 10.5px; color: var(--muted); min-height: 1.35em; }
.tower.out .tower-name, .tower.out .tower-sub { color: var(--muted); }

.side-flange { fill: var(--flange); }
.side-core { fill: var(--core); }
.side-fil { stroke: var(--fil-edge); stroke-width: .8; }
.box { fill: var(--box); }
.box-edge { stroke: rgba(0, 0, 0, .18); stroke-width: .8; }
.box-window { stroke: rgba(0, 0, 0, .25); stroke-width: .8; }
.box-hub { fill: var(--box); }
.box-line { fill: var(--box-line); }
.ghost rect { fill: none; stroke: var(--muted); stroke-width: 1.2; stroke-dasharray: 3 3; opacity: .6; }
.ghost text { fill: var(--muted); font-size: 8.5px; font-weight: 600; font-family: inherit; }

/* --- list view ---------------------------------------------------------- */

.group {
  background: var(--card); border-radius: 16px; box-shadow: var(--shadow);
  margin-bottom: 12px; overflow: hidden;
}
.group-head {
  width: 100%; border: 0; background: none; text-align: left;
  display: flex; align-items: center; gap: 10px; padding: 14px 14px;
}
.group-head .group-meta { margin-left: auto; text-align: right; }
.group-dots { display: flex; }
.group-dots i {
  width: 14px; height: 14px; border-radius: 50%; margin-left: -4px;
  border: 2px solid var(--card); box-shadow: 0 0 0 .5px rgba(0, 0, 0, .2);
}
.group-dots i:first-child { margin-left: 0; }
.chev { width: 22px; height: 22px; fill: var(--muted); flex: none; transition: transform .2s; }
.group.open > .group-head .chev, .fil.expanded > .fil-head .chev { transform: rotate(180deg); }
.group-body { border-top: 1px solid var(--border); }

.fil + .fil { border-top: 1px solid var(--border); }
.fil-head {
  width: 100%; border: 0; background: none; text-align: left;
  display: flex; align-items: center; gap: 12px; padding: 10px 12px 10px 14px;
}
.fil-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.fil-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fil-sub { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fil-stock { display: flex; flex-direction: column; align-items: flex-end; flex: none; }
.fil-count { font-weight: 700; font-size: 13.5px; color: var(--accent); }
.fil-kg { color: var(--muted); font-size: 12px; }
.fil.low .fil-count { color: var(--warn); }
.fil.out .fil-count { color: var(--muted); font-weight: 600; }
.fil.out .ring { opacity: .45; }
.fil-body { padding: 2px 14px 14px; }

.ring { flex: none; }
.ring-rim { fill: var(--card); stroke: rgba(127, 127, 127, .35); stroke-width: 1; }
.ring-track { stroke: var(--track); }
.ring-hub { fill: var(--card); stroke: rgba(127, 127, 127, .35); stroke-width: 1; }

/* --- filament body (list + detail sheet) ------------------------------- */

.stock-row, .spool-row { display: flex; align-items: center; gap: 10px; padding: 6px 0; }
.stock-icon { width: 48px; flex: none; }
.stock-icon svg { width: 100%; height: auto; }
.stock-text { flex: 1; }
.stock-text b { font-size: 18px; }
.stock-text small, .sub-head small { color: var(--muted); }
.sub-head { font-weight: 700; font-size: 13px; margin: 10px 0 2px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.spool-row .seg { flex: 1; min-width: 0; }
.spool-row .seg button { padding: 7px 2px; }
.empty-note { color: var(--muted); font-size: 13px; margin: 4px 0; }
.spool-chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.spool-chip {
  display: inline-flex; align-items: center; gap: 6px; font-size: 13px;
  background: var(--field); border-radius: 999px; padding: 3px 10px 3px 4px;
}
.facts { color: var(--muted); font-size: 12.5px; margin: 10px 0 0; }
.notes { font-size: 13.5px; margin: 6px 0 0; white-space: pre-wrap; }
.fil-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

.btn {
  border: 0; border-radius: 12px; padding: 11px 16px; font-weight: 600;
  background: var(--field); color: var(--text);
}
.btn.sm { padding: 8px 12px; font-size: 13.5px; border-radius: 10px; }
.btn.wide { width: 100%; margin: 2px 0 4px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .45; }

.stepper {
  display: flex; align-items: center; background: var(--field); border-radius: 12px; overflow: hidden; flex: none;
}
.stepper button {
  width: 40px; height: 40px; border: 0; background: none; font-size: 22px; line-height: 1; color: var(--accent);
}
.stepper button:disabled { color: var(--muted); opacity: .5; }
.stepper input {
  width: 64px; text-align: center; border: 0; background: none; font-weight: 700; font-size: 16px;
  padding: 0; -moz-appearance: textfield; appearance: textfield;
}
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper.sm button { width: 34px; height: 34px; font-size: 20px; }
.stepper.sm input { width: 56px; font-size: 15px; }

/* --- FAB ------------------------------------------------------------------ */

.fab {
  position: fixed; right: 18px; bottom: calc(22px + env(safe-area-inset-bottom)); z-index: 6;
  width: 58px; height: 58px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-ink);
  box-shadow: 0 6px 18px rgba(0, 174, 66, .38);
  display: grid; place-items: center;
}
.fab svg { width: 28px; height: 28px; fill: currentColor; }
.fab:active { transform: scale(.95); }

/* --- bottom sheets ----------------------------------------------------- */

.sheet {
  border: 0; padding: 0 16px calc(16px + env(safe-area-inset-bottom)); margin: auto auto 0;
  width: 100%; max-width: 560px; max-height: 92vh; max-height: 92dvh; overflow-y: auto;
  border-radius: 20px 20px 0 0; background: var(--card); color: var(--text);
  overscroll-behavior: contain;
}
.sheet[open] { animation: up .22s ease-out; }
.sheet::backdrop { background: rgba(0, 0, 0, .42); }
@keyframes up { from { transform: translateY(40px); opacity: .6; } }
@media (min-width: 700px) {
  .sheet { margin: auto; border-radius: 20px; }
}
.sheet-head {
  position: sticky; top: 0; z-index: 3; background: var(--card);
  margin: 0 -16px; padding: 0 16px 2px;
}
.grab { width: 38px; height: 5px; border-radius: 3px; background: var(--border); }
.sheet-head { padding-top: 18px; }
.sheet h2 { margin: 6px 0 8px; font-size: 20px; }
.sheet-head .grab { position: absolute; top: 8px; left: 50%; margin-left: -19px; }
.sheet label, .sheet .label {
  display: block; font-size: 12.5px; font-weight: 600; color: var(--muted); margin: 12px 0 0;
}
.sheet .label .hint { font-weight: 400; }
.sheet input:not([type=color]):not([type=file]):not(.num), .sheet select, .sheet textarea {
  display: block; width: 100%; margin-top: 5px; border: 1px solid var(--border); background: var(--field);
  border-radius: 10px; padding: 10px 12px; font-size: 16px; outline: none; color: var(--text);
}
.sheet input:focus, .sheet select:focus, .sheet textarea:focus { border-color: var(--accent); }
.sheet textarea { resize: vertical; }
.sheet code { background: var(--field); padding: 0 4px; border-radius: 4px; }
.sheet .hint { color: var(--muted); font-size: 13px; margin: 0 0 8px; }
#import-form textarea { font: 13px/1.4 ui-monospace, Menlo, Consolas, monospace; }
#import-file { margin-top: 10px; font-size: 13px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid2 label { margin-top: 12px; }

.swatches { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 10px; }
.swatches .sw {
  width: 32px; height: 32px; border-radius: 50%; border: 0;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .18);
}
.swatches .sw.on { box-shadow: 0 0 0 2px var(--card), 0 0 0 4px var(--accent); }
.swatches.named {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 6px;
}
.sw-named {
  display: flex; align-items: center; gap: 7px; text-align: left; min-width: 0;
  border: 1px solid var(--border); background: var(--field); border-radius: 10px; padding: 6px 7px;
  font-size: 12.5px; position: relative;
}
.sw-named i {
  position: relative; width: 22px; height: 22px; border-radius: 50%; flex: none;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .2);
}
.sw-named i b {
  position: absolute; right: -6px; bottom: -5px; min-width: 15px; padding: 0 3px; border-radius: 999px;
  font-size: 9.5px; font-weight: 700; line-height: 14px; text-align: center; font-style: normal;
  background: var(--accent); color: var(--accent-ink); box-shadow: 0 0 0 2px var(--field);
}
.sw-named span {
  min-width: 0; line-height: 1.2; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden;
}
.sw-named.on { border-color: var(--accent); background: var(--accent-soft); font-weight: 600; }
.sw-named.on i b { box-shadow: 0 0 0 2px var(--accent-soft); }

.color-row { display: flex; gap: 10px; align-items: stretch; }
.color-row input[name=color_name] { flex: 1; margin-top: 0 !important; }
.color-row input[type=color] {
  -webkit-appearance: none; appearance: none; width: 44px; height: 44px; flex: none; align-self: center;
  border: 0; border-radius: 50%; padding: 0; background: none; cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .2), 0 0 0 2px var(--card), 0 0 0 3px var(--border);
}
.color-row input[type=color]::-webkit-color-swatch-wrapper { padding: 0; }
.color-row input[type=color]::-webkit-color-swatch { border: 0; border-radius: 50%; }
.color-row input[type=color]::-moz-color-swatch { border: 0; border-radius: 50%; }

.stepper-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 14px; }
.stepper-row small { color: var(--muted); font-size: 12px; }
.seg-wide { margin-top: 10px; align-items: center; }
.seg-wide > span { font-size: 12.5px; color: var(--muted); padding: 0 8px; white-space: nowrap; }

.more-fields { margin-top: 14px; }
.more-fields summary { color: var(--accent); font-weight: 600; font-size: 14px; cursor: pointer; }

.sheet-actions {
  display: flex; gap: 10px; position: sticky; bottom: 0; z-index: 3; background: var(--card);
  margin: 18px -16px calc(-16px - env(safe-area-inset-bottom));
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
}
.sheet-actions .btn { flex: 1; }

.detail-head { display: flex; align-items: center; gap: 14px; margin: 6px 0 8px; }
.detail-head h2 { margin: 0; }
.detail-stock { font-weight: 700; color: var(--accent); margin-top: 2px; font-size: 14px; }
.detail-stock.low { color: var(--warn); }
.detail-stock.out { color: var(--muted); }

.menu { display: flex; flex-direction: column; gap: 8px; padding: 6px 0; }
.menu button {
  border: 0; background: var(--field); border-radius: 12px; padding: 14px; font-weight: 600; text-align: left;
}
.menu button[data-close] { text-align: center; color: var(--muted); }

/* --- empty + toast ------------------------------------------------------ */

.empty { text-align: center; color: var(--muted); padding: 48px 20px; }
.empty-stack { width: 84px; margin: 0 auto 16px; display: flex; flex-direction: column; gap: 1px; }
.empty-stack svg { width: 100%; height: auto; }

.toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); z-index: 50;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #1f2124; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  max-width: calc(100vw - 32px); transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: var(--danger); }
