:root {
  --bg: #f6f4f0;
  --panel: #ffffff;
  --ink: #23201c;
  --muted: #7a7268;
  --line: #e6e1d9;
  --accent: #2f5d50;
  --accent-ink: #ffffff;
  --accent-soft: #e4eeea;
  --danger: #b4412f;
  --shadow: 0 1px 2px rgba(35, 32, 28, .06), 0 4px 16px rgba(35, 32, 28, .06);
  --radius: 12px;

  --s-new: #2c5fa8;      --s-new-bg: #e5edf8;
  --s-reuse: #2e7d4f;    --s-reuse-bg: #e3f2e8;
  --s-update: #a8650d;   --s-update-bg: #fbefdc;
  --s-review: #7a4bb0;   --s-review-bg: #efe7f8;
  --s-other: #6b665f;    --s-other-bg: #eeebe6;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 20px;
  padding: 12px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 200px; }
.logo {
  width: 36px; height: 36px; border-radius: 9px;
  display: grid; place-items: center;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 18px;
}
.brand h1 { margin: 0; font-size: 16px; }
.brand p { margin: 0; font-size: 12px; color: var(--muted); }
.search { flex: 1; position: relative; max-width: 560px; }
.search svg {
  position: absolute; left: 12px; top: 50%; width: 16px; height: 16px; transform: translateY(-50%);
  fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round;
}
.search input {
  width: 100%; padding: 9px 12px 9px 36px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: #fff; }
.actions { display: flex; gap: 8px; margin-left: auto; }

.btn {
  border: 1px solid var(--line); background: var(--panel);
  padding: 8px 14px; border-radius: 9px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #cfc8bd; background: #faf9f6; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.link { border: 0; background: none; color: var(--accent); padding: 6px 8px; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: 0; background: none; font-size: 18px; cursor: pointer; color: var(--muted); padding: 6px 10px; border-radius: 8px; }
.icon-btn:hover { background: var(--bg); }

/* ---------- status chips ---------- */
.statusbar { display: flex; flex-wrap: wrap; gap: 8px; padding: 16px 24px 4px; }
.chip {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 7px 12px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--line); background: var(--panel);
}
.chip b { font-weight: 600; }
.chip .n { font-variant-numeric: tabular-nums; color: var(--muted); }
.chip .dot { width: 8px; height: 8px; border-radius: 50%; }
.chip.on { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }

/* ---------- filters ---------- */
.filters {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px;
  padding: 10px 24px 14px;
}
.filter-selects { display: flex; flex-wrap: wrap; gap: 8px; flex: 1; }
.filter-selects select {
  padding: 7px 28px 7px 10px; border-radius: 9px; border: 1px solid var(--line);
  background: var(--panel) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%237a7268' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 10px center;
  appearance: none; cursor: pointer; max-width: 200px;
}
.filter-selects select.set { border-color: var(--accent); background-color: var(--accent-soft); }
.filter-right { display: flex; align-items: center; gap: 12px; }
.count { color: var(--muted); font-variant-numeric: tabular-nums; }
.toggle { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; background: var(--panel); }
.toggle button { border: 0; background: none; padding: 7px 12px; cursor: pointer; }
.toggle button.on { background: var(--ink); color: #fff; }

/* ---------- grid ---------- */
main { padding: 0 24px 48px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.card {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden; cursor: pointer; display: flex; flex-direction: column;
  transition: transform .12s ease, box-shadow .12s ease;
  border: 1px solid transparent;
}
.card:hover { transform: translateY(-2px); box-shadow: 0 6px 22px rgba(35,32,28,.12); }
.card:focus-visible { outline: 2px solid var(--accent); }
.thumb { aspect-ratio: 4 / 3; background: #fbfaf8; display: grid; place-items: center; border-bottom: 1px solid var(--line); }
.thumb img { width: 100%; height: 100%; object-fit: contain; padding: 10px; }
.noimg { color: #c2bbb0; font-size: 12px; display: grid; place-items: center; gap: 6px; text-align: center; }
.noimg svg { width: 34px; height: 34px; fill: none; stroke: currentColor; stroke-width: 1.5; }
.card-body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.card-title { font-weight: 600; line-height: 1.3; }
.card-code { font-size: 12px; color: var(--muted); font-family: ui-monospace, Consolas, monospace; }
.card-meta { font-size: 12px; color: var(--muted); margin-top: auto; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 11.5px; font-weight: 600; white-space: nowrap;
  background: var(--s-other-bg); color: var(--s-other);
}
.badge.new { background: var(--s-new-bg); color: var(--s-new); }
.badge.reuse { background: var(--s-reuse-bg); color: var(--s-reuse); }
.badge.update { background: var(--s-update-bg); color: var(--s-update); }
.badge.review { background: var(--s-review-bg); color: var(--s-review); }
.badge.plain { background: var(--bg); color: var(--muted); font-weight: 500; }

/* ---------- table ---------- */
/* the wrapper scrolls, so the two header rows stay visible while scrolling */
.table-wrap {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: auto; max-height: calc(100vh - 210px); min-height: 320px;
}
table { width: 100%; border-collapse: separate; border-spacing: 0; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
thead { position: sticky; top: 0; z-index: 2; }
th { background: #faf9f6; font-size: 12px; color: var(--muted); font-weight: 600; user-select: none; white-space: nowrap; }
th[data-sort] { cursor: pointer; }
th[data-sort]:hover { color: var(--ink); }
th .arrow { opacity: .6; }
.filter-row th { padding: 0 6px 8px; border-bottom: 1px solid #d9d2c7; }
.filter-row select, .filter-row input {
  width: 100%; min-width: 92px; padding: 5px 22px 5px 8px; font-size: 12.5px; font-weight: 400;
  border: 1px solid var(--line); border-radius: 7px; color: var(--ink);
}
.filter-row input { padding-right: 8px; background: #fff; }
.filter-row select {
  appearance: none; cursor: pointer;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%237a7268' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 7px center;
}
.filter-row .set { border-color: var(--accent); background-color: var(--accent-soft); }
.filter-row input:focus, .filter-row select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.hint { color: var(--muted); font-size: 13px; }

/* in-app confirmation dialog */
dialog.confirm {
  border: 0; border-radius: 14px; padding: 22px 22px 18px; width: min(420px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25); color: var(--ink); background: var(--panel);
}
dialog.confirm::backdrop { background: rgba(20, 18, 15, .4); }
dialog.confirm h3 { margin: 0 0 8px; font-size: 17px; }
dialog.confirm p { margin: 0 0 18px; color: var(--muted); }
.confirm-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.btn.danger-solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger-solid:hover { filter: brightness(1.08); background: var(--danger); }

/* user menu (top right) */
.user-menu { position: relative; }
.user-pop {
  display: none; position: absolute; right: 0; top: calc(100% + 6px); z-index: 30; min-width: 220px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 10px 30px rgba(35, 32, 28, .16); padding: 6px;
}
.user-menu.open .user-pop { display: block; }
.user-pop-head { padding: 8px 10px 10px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.user-pop-head b, .user-pop-head small { display: block; }
.user-pop-head small { color: var(--muted); font-size: 12px; }
.user-pop button, .user-pop a {
  display: block; width: 100%; text-align: left; padding: 8px 10px; border: 0; background: none;
  border-radius: 8px; cursor: pointer; color: var(--ink); text-decoration: none; font-size: 14px;
}
.user-pop button:hover, .user-pop a:hover { background: var(--bg); }
.user-pop [hidden] { display: none; }
dialog.confirm label.f { margin-bottom: 10px; }
.login-error { color: var(--danger); min-height: 18px; font-size: 13px; margin: 2px 0 0; }

/* who-am-I button */
.user-chip { display: inline-flex; align-items: center; gap: 6px; max-width: 200px; }
.user-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-chip svg { width: 16px; height: 16px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; }
.user-chip.unset { color: var(--s-update); border-color: #ecd3a8; background: #fdf6ea; }
.name-input { width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; font-size: 15px; }
.name-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.last-change { margin-top: 4px; font-size: 12px; color: var(--muted); }

/* clear-all-filters button: quiet when nothing is filtered, highlighted when something is */
.clear-btn { display: inline-flex; align-items: center; gap: 6px; }
.clear-btn svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.clear-btn.active { color: var(--danger); border-color: #e5b9b0; background: #fdf3f1; font-weight: 600; }
.clear-btn.active:hover { background: #fbe7e3; border-color: var(--danger); }
.clear-btn:disabled { color: var(--muted); }

/* row number + duplicate button: pinned to the left while scrolling sideways */
th.t-no, td.t-no { position: sticky; left: 0; width: 56px; min-width: 56px; text-align: right; z-index: 1; }
th.t-act, td.t-act { position: sticky; left: 56px; width: 40px; min-width: 40px; padding: 4px; text-align: center; z-index: 1; border-right: 1px solid var(--line); }
td.t-no, td.t-act { background: var(--panel); }
tbody tr:hover td.t-no, tbody tr:hover td.t-act { background: #faf9f6; }
td.t-no { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12.5px; }
.filter-row th.t-no { padding-left: 4px; padding-right: 4px; }
.filter-row th.t-no input { min-width: 0; padding: 5px 6px; text-align: right; }
.dup-btn { padding: 5px; line-height: 0; border-radius: 7px; }
.dup-btn svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.dup-btn:hover { color: var(--accent); background: var(--accent-soft); }
.rowno { font-family: ui-monospace, Consolas, monospace; color: var(--muted); }

/* multi-select filter button + popover */
.ms-btn {
  display: flex; align-items: center; width: 100%; min-width: 92px; max-width: 220px;
  padding: 5px 24px 5px 8px; border: 1px solid var(--line); border-radius: 7px; cursor: pointer;
  font-size: 12.5px; font-weight: 400; color: var(--ink); text-align: left;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%237a7268' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 8px center;
}
.ms-btn span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ms-btn.set { border-color: var(--accent); background-color: var(--accent-soft); }
.ms-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }
.filter-selects .ms-btn { width: auto; padding: 7px 28px 7px 10px; border-radius: 9px; font-size: 14px; }
.ms-pop {
  position: fixed; z-index: 60; display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(35, 32, 28, .18); padding: 8px; font-size: 13px;
}
.ms-pop[hidden] { display: none; }
.ms-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 2px 4px 6px; }
.ms-head .btn.link { padding: 2px 4px; font-size: 12.5px; }
.ms-search { margin: 0 2px 6px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 7px; }
.ms-search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.ms-list { overflow: auto; min-height: 0; flex: 1; }
.ms-opt { display: flex; align-items: center; gap: 8px; padding: 5px 6px; border-radius: 6px; cursor: pointer; }
.ms-opt:hover { background: var(--bg); }
.ms-opt input { accent-color: var(--accent); width: 15px; height: 15px; flex: none; }
.ms-val { flex: 1; }
.ms-n { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }
.ms-empty { color: var(--muted); padding: 8px; }

/* column picker */
.col-picker { position: relative; }
.col-menu {
  display: none; position: absolute; right: 0; top: calc(100% + 6px); z-index: 30;
  width: 300px; max-height: min(70vh, 560px); overflow: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 10px 30px rgba(35, 32, 28, .16); padding: 10px 12px 12px;
}
.col-picker.open .col-menu { display: block; }
.col-menu-head { display: flex; justify-content: space-between; align-items: center; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.col-menu-head .btn.link { padding: 4px 6px; }
.col-group { padding-top: 8px; }
.col-group-title { font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--accent); margin-bottom: 2px; }
.col-opt { display: flex; align-items: center; gap: 8px; padding: 4px 4px; border-radius: 6px; cursor: pointer; }
.col-opt:hover { background: var(--bg); }
.col-opt input { accent-color: var(--accent); width: 15px; height: 15px; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: #faf9f6; }
tbody tr.no-hover { cursor: default; }
tbody tr.no-hover:hover { background: none; }
td.t-img { width: 64px; }
td.t-img img, td.t-img .noimg-sm { width: 52px; height: 42px; object-fit: contain; display: block; border-radius: 6px; background: #fbfaf8; }
td.t-comment { max-width: 340px; color: var(--muted); font-size: 12.5px; }
td.t-comment div { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
td.mono { font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; white-space: nowrap; }

.empty { text-align: center; color: var(--muted); padding: 80px 20px; }

/* ---------- drawer ---------- */
.scrim { position: fixed; inset: 0; background: rgba(20, 18, 15, .32); z-index: 40; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 50;
  width: min(640px, 100vw); background: var(--panel);
  display: flex; flex-direction: column;
  transform: translateX(100%); transition: transform .2s ease;
  box-shadow: -8px 0 30px rgba(0,0,0,.12);
}
.drawer.open { transform: none; }
.drawer-head { display: flex; justify-content: space-between; gap: 12px; padding: 18px 22px 12px; border-bottom: 1px solid var(--line); }
.drawer-head h2 { margin: 0; font-size: 18px; }
.drawer-head p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.drawer-body { flex: 1; overflow: auto; padding: 18px 22px 30px; }
.drawer-foot { display: flex; align-items: center; gap: 8px; padding: 12px 22px; border-top: 1px solid var(--line); background: #faf9f6; }
.drawer-foot .dirty { margin-left: auto; color: var(--s-update); font-size: 12.5px; }

.hero { display: grid; grid-template-columns: 1fr; gap: 10px; margin-bottom: 8px; }
.imgslot { border: 1px dashed var(--line); border-radius: var(--radius); background: #fbfaf8; position: relative; overflow: hidden; }
.imgslot.big { aspect-ratio: 16 / 10; }
.imgslot.small { aspect-ratio: 4 / 3; }
.imgslot img { width: 100%; height: 100%; object-fit: contain; padding: 10px; cursor: zoom-in; }
.imgslot .noimg { height: 100%; }
.imgslot .slot-actions { position: absolute; right: 8px; bottom: 8px; display: flex; gap: 6px; }
.imgslot .slot-actions button { font-size: 12px; padding: 5px 10px; background: rgba(255,255,255,.92); }
.imgslot .label { position: absolute; left: 10px; top: 8px; font-size: 11px; color: var(--muted); background: rgba(255,255,255,.85); padding: 1px 6px; border-radius: 6px; }
.imgslot.drag { border-color: var(--accent); background: var(--accent-soft); }

fieldset { border: 0; padding: 0; margin: 22px 0 0; }
legend { font-weight: 700; font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--accent); margin-bottom: 10px; padding: 0; }
.fgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.fgrid .wide { grid-column: 1 / -1; }
.fgrid .imgs { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
label.f { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
label.f input, label.f textarea, label.f select {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fff; font-size: 14px; color: var(--ink);
}
label.f textarea { min-height: 96px; resize: vertical; }
label.f input:focus, label.f textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label.f.changed input, label.f.changed textarea { border-color: var(--s-update); background: #fffaf2; }

/* ---------- misc ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px;
  opacity: 0; transition: all .2s ease; pointer-events: none; z-index: 80;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--danger); }
.busy { position: fixed; inset: 0; background: rgba(246,244,240,.85); display: grid; place-content: center; justify-items: center; z-index: 90; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.8); display: grid; place-items: center; z-index: 100; cursor: zoom-out; }
.lightbox img { max-width: 92vw; max-height: 92vh; background: #fff; border-radius: 8px; padding: 12px; }

@media (max-width: 900px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; gap: 10px; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .actions #btn-import { display: none; }
  .statusbar, .filters { padding-left: 16px; padding-right: 16px; }
  main { padding: 0 16px 40px; }
  .table-wrap { max-height: 75vh; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
  .fgrid { grid-template-columns: 1fr; }
}
