* { 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; }

[hidden] { display: none !important; }

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  overflow-y: hidden;
}
.tab {
  font: inherit;
  font-weight: 600;
  color: var(--muted);
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 10px 16px;
  margin-bottom: -1px;
  cursor: pointer;
  white-space: nowrap;
}
.tab:hover { color: var(--ink); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
@media (max-width: 480px) {
  .tabs { gap: 0; }
  .tab { padding: 10px 7px; font-size: .88rem; }
}

.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; }

/* Rich-text notes editor: contenteditable that preserves pasted formatting. */
.rich-hidden { display: none !important; }
.rich-editor {
  font: inherit;
  color: inherit;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  min-height: 110px;
  max-height: 340px;
  overflow-y: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  cursor: text;
}
.rich-editor:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.rich-editor:empty:before { content: attr(data-placeholder); color: var(--muted); }
.rich-editor p { margin: 0 0 .5em; }
.rich-editor ul, .rich-editor ol { margin: 0 0 .5em; padding-left: 1.4em; }
.rich-editor h1, .rich-editor h2, .rich-editor h3, .rich-editor h4 { margin: .3em 0; font-size: 1.05em; }
.rich-editor a { color: var(--accent); }
.rich-editor blockquote { margin: 0 0 .5em; padding-left: .8em; border-left: 3px solid var(--line); color: var(--muted); }
/* Rendered notes (viewer) keep the same formatting affordances. */
.viewer-notes p { margin: 0 0 .5em; }
.viewer-notes ul, .viewer-notes ol { margin: 0 0 .5em; padding-left: 1.4em; }
.viewer-notes a { color: var(--accent); }
.viewer-notes blockquote { margin: 0 0 .5em; padding-left: .8em; border-left: 3px solid var(--line); color: var(--muted); }
.viewer-notes > :last-child { margin-bottom: 0; }

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

/* Filament grid split into sections (In AMS / Everything Else): the outer
   container stops being a grid so each section can hold its own. */
.grid.sectioned { display: block; }
.fil-group + .fil-group { margin-top: 26px; }
/* Tint the whole In-AMS section so the loaded spools read as a group. */
.fil-group-ams {
  background: #a05cbd;
  border: 1px solid #8d47ab;
  border-radius: var(--radius);
  padding: 16px;
}
/* Header sits on the purple fill, so use light text there. */
.fil-group-ams .group-head { margin-top: 0; color: #fff; }
.fil-group-ams .group-count { background: rgba(255, 255, 255, .25); border-color: transparent; color: #fff; }
/* Click the header to collapse; chevron rotates and the cards hide. */
.ams-head { cursor: pointer; user-select: none; }
.ams-head .chevron { color: #fff; }
.fil-group-ams.collapsed .chevron { transform: rotate(-90deg); }
.fil-group-ams.collapsed > .grid { display: none; }
.fil-group-ams.collapsed { padding-bottom: 16px; }
.fil-group-ams.collapsed .group-head { margin-bottom: 0; }

.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); }

/* Knowledge: collapsible Category → Subcategory sections with drag-to-reorder */
.know-group { margin-bottom: 22px; }
.know-group:last-child { margin-bottom: 0; }
.group-head {
  margin: 0 0 12px; font-size: 1rem; font-weight: 700;
  display: flex; align-items: center; gap: 8px; color: var(--ink);
}
.group-count {
  font-size: .78rem; font-weight: 600; color: var(--muted);
  background: var(--bg); border: 1px solid var(--line);
  border-radius: 999px; padding: 1px 8px;
}
.cat-head, .subgroup-head { cursor: pointer; user-select: none; }
.chevron {
  display: inline-block; font-size: .75em; color: var(--muted);
  transition: transform .15s ease; width: .8em;
}
.know-group.collapsed > .cat-head .chevron,
.know-subgroup.collapsed > .subgroup-head .chevron { transform: rotate(-90deg); }
.know-group.collapsed > .group-body,
.know-subgroup.collapsed > .subgroup-body { display: none; }
.group-body { display: flex; flex-direction: column; gap: 16px; }
.know-subgroup { margin: 0; }
.subgroup-head {
  margin: 0 0 10px; font-size: .9rem; font-weight: 600; color: var(--muted);
  display: flex; align-items: center; gap: 8px;
}
.know-sortable .card, .fil-sortable .card { position: relative; }
.drag-handle {
  position: absolute; top: 6px; left: 6px; z-index: 3;
  width: 28px; height: 28px; padding: 0;
  border: none; border-radius: 7px;
  background: rgba(255, 255, 255, .88); color: #444;
  font-size: 1.05rem; line-height: 1; cursor: grab;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 1px 3px rgba(16, 24, 40, .25);
}
.drag-handle:hover { background: #fff; }
.drag-handle:active { cursor: grabbing; }
.card.dragging { opacity: .45; }
/* Touch: a finger on the handle drags the card instead of scrolling the page,
   and the target is bigger so it's easy to hit. */
.fil-sortable .drag-handle { touch-action: none; }
@media (pointer: coarse) {
  .fil-sortable .drag-handle { width: 36px; height: 36px; font-size: 1.25rem; }
}

/* Admin: manage dropdown options */
.admin-section { margin-bottom: 24px; }
.admin-section:last-child { margin-bottom: 0; }
.admin-field { margin: 0 0 16px; }
.admin-field-label { font-weight: 600; margin-bottom: 6px; }
.admin-sub, .admin-builtin {
  font-size: .7rem; font-weight: 600; color: var(--muted);
  background: var(--bg); border: 1px solid var(--line);
  border-radius: 999px; padding: 1px 8px; margin-left: 4px;
}
.admin-builtin { text-transform: uppercase; letter-spacing: .04em; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--bg); border: 1px solid var(--line);
  border-radius: 999px; padding: 4px 6px 4px 12px; font-size: .88rem;
}
.chip-locked { padding: 4px 12px; opacity: .7; }
.chip-empty { color: var(--muted); font-size: .85rem; }
.chip-x {
  border: none; background: #e5e7eb; color: #444;
  width: 18px; height: 18px; border-radius: 50%;
  cursor: pointer; font-size: .95rem; line-height: 1;
  display: flex; align-items: center; justify-content: center;
}
.chip-x:hover { background: #fca5a5; color: #7f1d1d; }
.admin-add { display: flex; gap: 8px; }
.admin-add input { flex: 1; min-width: 0; }
.admin-fixed .chips { margin-bottom: 0; }

.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; }
.badge-archived { background: #e5e7eb; color: #4b5563; }
.badge-inuse { background: #dbeafe; color: #1e40af; }
.badge-transit { background: #fee2e2; color: #b91c1c; }

/* Ordered-but-not-received spools: reddish border so they stand out at a glance. */
.card-transit {
  border-color: #ef4444;
  box-shadow: 0 0 0 1px #ef4444, var(--shadow);
}

.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); }
.card-buy { font-size: .8rem; }
.card-buy a { color: var(--accent); text-decoration: none; font-weight: 600; }
.card-buy a:hover { text-decoration: underline; }
.btn-link {
  border: none; background: none; padding: 0; cursor: pointer;
  font: inherit; color: var(--accent); font-weight: 600;
}
.btn-link:hover { text-decoration: underline; }

.empty { text-align: center; color: var(--muted); padding: 80px 0; }
.empty-inline { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 60px 0; }

/* Maintenance log */
.maint-list { display: flex; flex-direction: column; gap: 10px; }
.maint-row {
  display: flex;
  gap: 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 16px;
  cursor: pointer;
  transition: box-shadow .12s ease;
}
.maint-row:hover { box-shadow: 0 4px 12px rgba(16, 24, 40, .12); }
.maint-date { flex: none; width: 105px; color: var(--muted); font-size: .85rem; padding-top: 3px; }
.maint-main { min-width: 0; }
.maint-task { font-weight: 600; margin-right: 8px; }
.maint-notes { color: var(--muted); font-size: .85rem; margin-top: 4px; white-space: pre-line; }
@media (max-width: 560px) {
  .maint-row { flex-direction: column; gap: 4px; }
  .maint-date { width: auto; padding-top: 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; }

/* Full-screen pages (detail viewer & editor) */
.page {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: var(--bg);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.page[hidden] { display: none; }
#filamentForm, #accForm, #maintForm, #knowForm, .viewer-wrap {
  max-width: 760px;
  margin: 20px auto 40px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.page .dialog-head {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--card);
  margin: -20px -24px 0;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line);
  border-radius: var(--radius) var(--radius) 0 0;
}
.page .dialog-actions {
  position: sticky;
  bottom: 0;
  z-index: 2;
  background: var(--card);
  margin: 4px -24px -20px;
  padding: 12px 24px;
  border-top: 1px solid var(--line);
  border-radius: 0 0 var(--radius) var(--radius);
}
@media (max-width: 760px) {
  #filamentForm, #accForm, #maintForm, #knowForm, .viewer-wrap {
    max-width: none;
    margin: 0;
    min-height: 100%;
    border: none;
    border-radius: 0;
    box-shadow: none;
  }
  .page .dialog-head, .page .dialog-actions { border-radius: 0; }
  .page .dialog-actions { margin-bottom: 0; padding-bottom: max(12px, env(safe-area-inset-bottom)); }
}

/* 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, #accForm, #maintForm, #knowForm { 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 12px; align-items: center;
  justify-content: space-between; flex-wrap: wrap;
}
.actions-group { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
@media (max-width: 480px) {
  /* stack the two button groups; each fills its row so nothing overflows */
  .actions-group { flex: 1 1 100%; justify-content: flex-end; }
  .actions-group .btn { flex: 1; }
}

/* Full-screen image view */
.photo-page {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(10, 12, 16, .96);
  display: flex; align-items: center; justify-content: center;
  cursor: zoom-out;
}
.photo-page[hidden] { display: none; }
.photo-page img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lightbox-close {
  position: absolute; top: max(10px, env(safe-area-inset-top)); right: 12px;
  background: rgba(255,255,255,.15); color: #fff; border-radius: 50%;
  width: 38px; height: 38px; font-size: 1.3rem;
}

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