* { box-sizing: border-box; }

:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --ink: #1f2430;
  --muted: #6b7280;
  --line: #e3e6eb;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --danger: #dc2626;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar h1 { margin: 0; font-size: 1.25rem; }
.topbar-right { display: flex; align-items: center; gap: 16px; }
.summary { color: var(--muted); font-size: .85rem; }

main { max-width: 1200px; margin: 0 auto; padding: 20px 24px 60px; }

.toolbar { display: flex; gap: 10px; margin-bottom: 18px; flex-wrap: wrap; }
.toolbar input[type=search] { flex: 1; min-width: 220px; }
input, select, textarea {
  font: inherit;
  color: inherit;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
  gap: 16px;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  cursor: pointer;
  transition: transform .12s ease, box-shadow .12s ease;
}
.card:hover { transform: translateY(-2px); box-shadow: 0 6px 16px rgba(16,24,40,.12); }

.card-img {
  position: relative;
  height: 150px;
  background: #eceef2;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.card-img img { width: 100%; height: 100%; object-fit: cover; }
.card-img .placeholder { font-size: 2.4rem; opacity: .35; }

.card-body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 8px; }
.card-title { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.swatch {
  display: inline-block; vertical-align: -2px;
  width: 16px; height: 16px; border-radius: 50%;
  border: 1px solid rgba(0,0,0,.15); flex: none;
}
.card-sub { color: var(--muted); font-size: .85rem; }

.badges { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  font-size: .72rem; font-weight: 600;
  padding: 2px 8px; border-radius: 999px;
}
.badge-new  { background: #dcfce7; color: #166534; }
.badge-used { background: #fef3c7; color: #92400e; }

.card-stats {
  display: flex; gap: 14px;
  font-size: .82rem; color: var(--muted);
}
.card-stats b { color: var(--ink); font-weight: 600; }

.card-files { display: flex; gap: 8px; font-size: .78rem; color: var(--muted); }

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

/* Grouped spools */
.count-badge {
  position: absolute; top: 8px; right: 8px;
  background: rgba(15, 20, 30, .78); color: #fff;
  font-size: .78rem; font-weight: 700;
  padding: 2px 10px; border-radius: 999px;
}
.backbar { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.backbar[hidden] { display: none; }

/* Detail viewer */
.viewer-wrap { padding: 20px 24px; display: flex; flex-direction: column; gap: 14px; }
.viewer-img {
  width: 100%; max-height: 260px; object-fit: cover;
  border-radius: 10px; cursor: zoom-in; display: block;
}
.viewer-grid {
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 12px 14px; margin: 0;
}
.viewer-grid dt {
  font-size: .72rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: .04em; color: var(--muted); margin-bottom: 2px;
}
.viewer-grid dd { margin: 0; font-weight: 600; }
.viewer-notes {
  background: #f7f8fa; border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; white-space: pre-wrap; font-size: .9rem;
}
.viewer-files { display: flex; gap: 16px; font-size: .88rem; }
.viewer-files a { color: var(--accent); text-decoration: none; }
.viewer-files a:hover { text-decoration: underline; }
.viewer-dates { font-size: .76rem; color: var(--muted); }
@media (max-width: 560px) { .viewer-grid { grid-template-columns: 1fr 1fr; } }

.btn {
  font: inherit;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--card);
  cursor: pointer;
}
.btn:hover { background: #f0f2f5; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: #1d4ed8; }
.btn-danger { background: #fff; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: #fef2f2; }

/* Dialog */
dialog {
  border: none;
  border-radius: 14px;
  padding: 0;
  width: min(680px, calc(100vw - 32px));
  max-height: calc(100vh - 60px);
  box-shadow: 0 20px 50px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(15, 20, 30, .45); }

#filamentForm { padding: 20px 24px; display: flex; flex-direction: column; gap: 16px; }

.dialog-head { display: flex; align-items: center; justify-content: space-between; }
.dialog-head h2 { margin: 0; font-size: 1.15rem; }
.btn-close {
  border: none; background: none; font-size: 1.6rem; line-height: 1;
  cursor: pointer; color: var(--muted); padding: 4px 8px;
}
.btn-close:hover { color: var(--ink); }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid.three { grid-template-columns: 1fr 1fr 1fr; }
label { display: flex; flex-direction: column; gap: 5px; font-size: .85rem; font-weight: 500; }
label input, label textarea { font-weight: 400; }

.color-row { display: flex; gap: 8px; }
.color-row input[type=text] { flex: 1; }
.color-row input[type=color] { padding: 2px; width: 44px; height: 38px; cursor: pointer; }

fieldset {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px 14px;
  margin: 0;
  display: flex; flex-direction: column; gap: 10px;
}
legend { font-size: .85rem; font-weight: 600; padding: 0 6px; }

.weight-row { display: flex; align-items: flex-end; gap: 18px; flex-wrap: wrap; }
.weight-input-label { width: 140px; }
.check { flex-direction: row; align-items: center; gap: 6px; font-weight: 500; padding-bottom: 9px; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.hint { margin: 0; font-size: .78rem; color: var(--muted); }
.hint.calc { color: #92400e; font-weight: 600; }

textarea { resize: vertical; min-height: 110px; }
.full { width: 100%; grid-column: 1 / -1; }

.uploads { display: flex; flex-direction: column; gap: 10px; }
.upload input[type=file] { font-size: .82rem; }
.current { font-size: .76rem; color: var(--muted); }
.current a { color: var(--accent); }

.dialog-actions { display: flex; gap: 10px; align-items: center; }
.spacer { flex: 1; }

/* Lightbox */
#lightbox { background: transparent; box-shadow: none; width: auto; max-width: calc(100vw - 40px); }
#lightbox img { max-width: 100%; max-height: calc(100vh - 80px); border-radius: 10px; display: block; }
.lightbox-close {
  position: absolute; top: 6px; right: 6px;
  background: rgba(0,0,0,.55); color: #fff; border-radius: 50%;
  width: 34px; height: 34px; font-size: 1.2rem;
}

@media (max-width: 560px) {
  .form-grid, .form-grid.three { grid-template-columns: 1fr; }
  .topbar { flex-wrap: wrap; }
}
