:root {
  --bg: #ffffff;
  --fg: #0a0a0a;
  --muted: #737373;
  --faint: #a3a3a3;
  --border: #e5e5e5;
  --border-strong: #d4d4d4;
  --surface: #fafafa;
  --surface-hover: #f5f5f5;
  --danger: #dc2626;
  --radius: 6px;
  --shadow-lg: 0 20px 50px rgba(10, 10, 10, 0.18);
}

* {
  box-sizing: border-box;
}

html, body {
  background: var(--bg);
  color: var(--fg);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 14px;
  line-height: 1.5;
  margin: 0;
  padding: 0;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

main {
  flex: 1;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: 1.25rem 1.25rem;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

#form {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* Sidebar */
.sidebar {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: 200px;
  padding: 1rem 0.625rem;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 80;
  transition: width 0.18s ease, padding 0.18s ease;
}

.sidebar .brand {
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--fg);
  padding: 0.35rem 0.75rem 1rem;
  white-space: nowrap;
  overflow: hidden;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius);
  color: var(--muted);
  text-decoration: none;
  font-size: 0.875rem;
  white-space: nowrap;
  overflow: hidden;
  transition: background 0.1s, color 0.1s;
}

.sidebar-nav a svg {
  width: 17px;
  height: 17px;
  flex: 0 0 17px;
}

.sidebar-nav a:hover {
  background: var(--surface-hover);
  color: var(--fg);
}

.sidebar-nav a.active {
  background: var(--fg);
  color: var(--bg);
}

.sidebar-toggle {
  position: absolute;
  top: 0.6rem;
  right: 0.5rem;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--faint);
  cursor: pointer;
  border-radius: 4px;
  padding: 0;
}

.sidebar-toggle:hover {
  background: var(--surface-hover);
  color: var(--fg);
}

.sidebar-toggle svg {
  width: 14px;
  height: 14px;
  transition: transform 0.18s;
}

/* Collapsed sidebar */
.sidebar.collapsed {
  width: 52px;
  padding: 1rem 0.3rem;
}

.sidebar.collapsed .brand {
  visibility: hidden;
  height: 0;
  padding: 0;
  margin-bottom: 2.25rem;
}

.sidebar.collapsed .sidebar-nav a,
.sidebar.collapsed .sidebar-logout button {
  justify-content: center;
  padding-left: 0;
  padding-right: 0;
}

.sidebar.collapsed .sidebar-nav a > span,
.sidebar.collapsed .sidebar-logout button > span,
.sidebar.collapsed .sidebar-user {
  display: none;
}

.sidebar.collapsed .sidebar-toggle {
  right: 50%;
  transform: translateX(50%);
}

.sidebar.collapsed .sidebar-toggle svg {
  transform: rotate(180deg);
}

.sidebar-logout {
  margin-top: auto;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

.sidebar-logout form {
  margin: 0;
}

.sidebar-logout button {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius);
  color: var(--muted);
  background: transparent;
  border: none;
  font: inherit;
  font-size: 0.875rem;
  text-align: left;
  cursor: pointer;
  transition: background 0.1s, color 0.1s;
}

.sidebar-logout button:hover {
  background: var(--surface-hover);
  color: var(--fg);
}

.sidebar-logout svg {
  width: 17px;
  height: 17px;
  flex: 0 0 17px;
}

.sidebar-user {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--faint);
  padding: 0 0.75rem 0.35rem;
}

/* Login */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  min-height: 100vh;
  margin: 0;
  background: var(--surface);
}

.login-main {
  width: 100%;
  max-width: 340px;
}

.login-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem 1.5rem 1.75rem;
  box-shadow: 0 4px 24px rgba(10, 10, 10, 0.06);
}

.login-brand {
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  text-align: center;
  margin-bottom: 1.25rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.login-form label {
  margin-top: 0.5rem;
  margin-bottom: 0.25rem;
}

.login-form input[type=text],
.login-form input[type=password] {
  width: 100%;
  padding: 0.55rem 0.7rem;
  font: inherit;
  font-size: 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
}

.login-form input:focus {
  outline: none;
  border-color: var(--fg);
}

.login-form button.primary {
  margin-top: 1rem;
}

.login-note {
  margin: 0 0 0.75rem;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

.login-error {
  padding: 0.5rem 0.7rem;
  font-size: 0.8rem;
  color: var(--danger);
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius);
  margin-bottom: 0.25rem;
}

/* Accounts */
.accounts-main {
  max-width: 1040px;
  height: 100vh;
}

.accounts-page {
  width: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.account-create {
  display: grid;
  grid-template-columns: minmax(180px, 0.75fr) minmax(0, 1.25fr);
  gap: 1rem;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  margin-bottom: 1rem;
}

.account-create h2 {
  margin: 0 0 0.35rem;
  font-size: 1rem;
  font-weight: 600;
}

.account-create p {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.account-form {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.account-form input[type=email],
.account-form input[type=password] {
  width: 100%;
  height: 2.3rem;
  padding: 0.45rem 0.7rem;
  font: inherit;
  font-size: 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
}

.account-form input:focus {
  outline: none;
  border-color: var(--fg);
}

.account-check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
  text-transform: none;
  letter-spacing: 0;
}

.account-check input {
  margin: 0;
}

.account-success {
  padding: 0.5rem 0.7rem;
  color: #166534;
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: var(--radius);
  font-size: 0.8rem;
}

.account-list-wrap {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.account-table {
  min-width: 720px;
}

/* Stats */
.stats-main {
  max-width: 1120px;
  height: 100vh;
}

.stats-page {
  width: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.stats-card {
  min-height: 5rem;
  padding: 0.8rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.stats-card span,
.stats-panel h2,
.stats-recent h2 {
  margin: 0;
  color: var(--faint);
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.stats-card strong {
  color: var(--fg);
  font-size: 1.3rem;
  font-weight: 600;
  letter-spacing: 0;
}

.stats-panels {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.stats-panel,
.stats-recent {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  overflow: hidden;
}

.stats-panel h2,
.stats-recent h2 {
  padding: 0.75rem 0.75rem 0.6rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.stats-table {
  min-width: 0;
  table-layout: auto;
}

.stats-table th,
.stats-table td {
  white-space: nowrap;
}

.stats-recent {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.stats-table-wrap {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
}

.stats-recent-table {
  min-width: 980px;
}

.status-failed {
  background: #fef2f2;
  border-color: #fecaca;
  color: #991b1b;
}

.status-partial {
  background: #fffbeb;
  border-color: #fde68a;
  color: #92400e;
}

.row + .row {
  margin-top: 0.85rem;
}

#form > .row {
  flex: 0 0 auto;
}

#form > #results-section {
  flex: 1 1 auto;
}

.row-pair {
  display: grid;
  grid-auto-columns: minmax(0, 1fr);
  grid-auto-flow: column;
  gap: 0.75rem;
}

.row-pair .field {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.row-pair .field > :last-child {
  margin-top: auto;
}

label {
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--faint);
  margin-bottom: 0.35rem;
}

label .muted-suffix {
  color: var(--faint);
  text-transform: none;
  letter-spacing: 0;
  font-weight: 400;
}

/* Pose selector button (opens modal) */
.select-button {
  width: 100%;
  height: 2.4rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  padding: 0 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  color: var(--fg);
  text-align: left;
  transition: border-color 0.12s, background 0.12s, box-shadow 0.12s;
}

.select-button:hover {
  border-color: var(--border-strong);
  background: var(--surface-hover);
}

.select-button:focus-visible {
  outline: none;
  border-color: var(--fg);
  box-shadow: 0 0 0 3px rgba(10, 10, 10, 0.06);
}

.select-button .select-button-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.select-button .value {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
}

.select-button .chevron {
  color: var(--faint);
  width: 0.95rem;
  height: 0.95rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: 0 0 auto;
}

/* Drop zone */
#dropzone {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  text-align: center;
  cursor: pointer;
  background: var(--surface);
  transition: border-color 0.12s, background 0.12s;
}

#dropzone:hover {
  border-color: var(--faint);
  background: var(--surface-hover);
}

#dropzone.dragover {
  border-color: var(--fg);
  background: var(--surface-hover);
}

#dropzone p {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}

#preview-wrap {
  display: none;
  align-items: center;
  gap: 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.75rem;
  background: var(--bg);
}

#preview-wrap.visible {
  display: flex;
}

.ref-list {
  flex: 1;
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.5rem;
}

.ref-item {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.ref-item img {
  width: 44px;
  height: 44px;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--bg);
}

.ref-name {
  flex: 1;
  min-width: 0;
  font-size: 0.85rem;
  color: var(--fg);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#preview-wrap .ref-remove {
  width: 1.75rem;
  height: 1.75rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  padding: 0;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--faint);
  cursor: pointer;
  font-size: 0;
  line-height: 1;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}

#preview-wrap .ref-remove svg {
  width: 0.9rem;
  height: 0.9rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#preview-wrap .ref-remove:hover {
  background: var(--bg);
  border-color: var(--border);
  color: var(--danger);
}

#preview-wrap > button {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.3rem 0.7rem;
  font-size: 0.75rem;
  cursor: pointer;
  color: var(--muted);
}

#preview-wrap > button:hover {
  border-color: var(--fg);
  color: var(--fg);
}

textarea {
  width: 100%;
  min-height: 44px;
  height: 44px;
  padding: 0.45rem 0.7rem;
  font: inherit;
  font-size: 0.85rem;
  line-height: 1.45;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  resize: vertical;
  background: var(--bg);
  color: var(--fg);
}

textarea::placeholder {
  color: var(--faint);
}

textarea:focus {
  outline: none;
  border-color: var(--fg);
}

button.primary {
  width: 100%;
  padding: 0.55rem;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  background: var(--fg);
  color: var(--bg);
  border: 1px solid var(--fg);
  border-radius: var(--radius);
  cursor: pointer;
  transition: opacity 0.12s;
}

button.primary:hover {
  opacity: 0.88;
}

button.primary:disabled {
  opacity: 0.4;
  cursor: wait;
}

#status {
  margin-top: 0.6rem;
  min-height: 1.1rem;
  font-size: 0.8rem;
  color: var(--muted);
  text-align: center;
}

#status.error {
  color: var(--danger);
}

/* Results */
#results-section {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#results-section > label {
  flex: 0 0 auto;
}

.results {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-auto-rows: 1fr;
  gap: 0.5rem;
  flex: 1 1 auto;
  min-height: 0;
}

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

.card img {
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
  display: block;
  background: var(--surface);
  object-fit: contain;
}

.card-footer {
  flex: 0 0 auto;
}

.card-placeholder {
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  align-items: center;
  justify-content: center;
  color: var(--faint);
}

.card-placeholder svg {
  width: 2.25rem;
  height: 2.25rem;
  opacity: 0.8;
}

.card-footer {
  padding: 0.5rem 0.7rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.card-footer a {
  color: var(--fg);
  text-decoration: none;
}

.card-footer a:hover {
  text-decoration: underline;
}

.card-err {
  padding: 1.25rem 0.75rem;
  text-align: center;
  color: var(--danger);
  font-size: 0.8rem;
}

#form .results {
  flex: 0 0 auto;
  grid-auto-rows: auto;
  align-items: start;
}

#form .card {
  align-self: start;
}

#form .card img,
#form .card-placeholder {
  aspect-ratio: 3/4;
}

#form .card img {
  flex: 0 0 auto;
  height: auto;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 10, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1.5rem;
  animation: fade 0.12s ease-out;
}

.modal.open {
  display: flex;
}

.modal-dialog {
  background: var(--bg);
  border-radius: 10px;
  width: 100%;
  max-width: 820px;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: pop 0.14s ease-out;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  width: 2rem;
  height: 2rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border-radius: 4px;
}

.modal-close:hover {
  background: var(--surface);
  color: var(--fg);
}

.modal-close svg {
  width: 1rem;
  height: 1rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.modal-body {
  padding: 1.25rem;
}

.modal-footer {
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.modal-footer button {
  padding: 0.5rem 1rem;
  font: inherit;
  font-size: 0.85rem;
  border-radius: var(--radius);
  cursor: pointer;
}

.modal-footer .secondary {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--border);
}

.modal-footer .secondary:hover {
  border-color: var(--fg);
}

.modal-footer .primary {
  width: auto;
  padding: 0.5rem 1.25rem;
}

/* Pose picker (inside modal) */
.poses {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
}

.poses figure {
  margin: 0;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  user-select: none;
  transition: border-color 0.12s, background 0.12s;
}

.poses figure:hover {
  border-color: var(--faint);
}

.poses figure.selected {
  border-color: var(--fg);
}

.poses figure.selected figcaption {
  color: var(--fg);
}

.poses figure:not(.selected) img {
  opacity: 0.35;
}

.poses img {
  width: 100%;
  aspect-ratio: 3/4;
  object-fit: contain;
  display: block;
  transition: opacity 0.12s;
}

.poses figcaption {
  font-size: 0.65rem;
  color: var(--muted);
  margin-top: 0.25rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

@keyframes fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@keyframes pop {
  from {
    opacity: 0;
    transform: translateY(6px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
/* Segmented control (model picker) */
.segment-control {
  display: inline-flex;
  width: 100%;
  height: 2.4rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
  padding: 2px;
  gap: 2px;
}

.segment {
  flex: 1;
  min-width: 0;
  padding: 0 0.5rem;
  font: inherit;
  font-size: 0.85rem;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: calc(var(--radius) - 2px);
  transition: background 0.12s, color 0.12s;
}

.segment:hover {
  color: var(--fg);
}

.segment.active {
  background: var(--fg);
  color: var(--bg);
}

/* Custom select (listbox) */
.custom-select {
  position: relative;
}

.custom-select[data-open=true] > .custom-select-trigger {
  border-color: var(--fg);
  background: var(--bg);
  box-shadow: 0 0 0 3px rgba(10, 10, 10, 0.06);
}

.custom-select[data-open=true] .chevron {
  transform: rotate(180deg);
}

.custom-select-trigger .chevron {
  transition: transform 0.12s;
}

.custom-select-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 50;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(10, 10, 10, 0.12);
  max-height: 260px;
  overflow-y: auto;
  animation: pop 0.1s ease-out;
}

.custom-select-menu[hidden] {
  display: none;
}

.custom-select-option {
  padding: 0.5rem 0.6rem;
  font-size: 0.9rem;
  color: var(--fg);
  border-radius: 4px;
  cursor: pointer;
  user-select: none;
  transition: background 0.08s, color 0.08s;
  outline: none;
}

.custom-select-option:hover,
.custom-select-option:focus {
  background: var(--surface-hover);
}

.custom-select-option[aria-selected=true] {
  background: var(--fg);
  color: var(--bg);
}

.custom-select-option[aria-selected=true]:hover,
.custom-select-option[aria-selected=true]:focus {
  background: var(--fg);
  color: var(--bg);
  opacity: 0.92;
}

/* Collection badge */
.collection-tag {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 3px;
  margin-left: 0.5rem;
  vertical-align: middle;
}

/* Nav */
/* Save panel (index page, after results) */
.save-panel {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px dashed var(--border);
  flex: 0 0 auto;
  transition: opacity 0.12s;
}

.save-panel.disabled {
  opacity: 0.55;
}

.save-panel > label {
  margin-bottom: 0.3rem;
}

.save-row {
  display: flex;
  gap: 0.5rem;
}

.save-row input {
  flex: 1;
  padding: 0.5rem 0.7rem;
  font: inherit;
  font-size: 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
}

.save-row input:focus {
  outline: none;
  border-color: var(--fg);
}

.save-row input:disabled {
  background: var(--surface);
  color: var(--faint);
  cursor: not-allowed;
}

.save-row button.primary {
  width: auto;
  padding: 0.5rem 1.25rem;
}

#save-status {
  margin-top: 0.5rem;
  font-size: 0.8rem;
  color: var(--muted);
  min-height: 1.1rem;
}

#save-status.error {
  color: var(--danger);
}

#save-status a {
  color: var(--fg);
}

/* Library (products list) */
.empty {
  text-align: center;
  color: var(--muted);
  margin-top: 3rem;
  font-size: 0.9rem;
}

.empty a {
  color: var(--fg);
}

.library-main {
  max-width: 1040px;
  height: 100vh;
}

.product-index {
  width: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.product-index-toolbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.product-index-toolbar h1 {
  flex: 0 0 auto;
  margin: 0;
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: 0;
}

.toolbar-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2rem;
  padding: 0 0.75rem;
  background: var(--fg);
  border: 1px solid var(--fg);
  border-radius: var(--radius);
  color: var(--bg);
  font-size: 0.85rem;
  font-weight: 500;
  text-decoration: none;
  flex: 0 0 auto;
}

.toolbar-button:hover {
  opacity: 0.88;
}

.product-search {
  flex: 1 1 420px;
  width: auto;
  max-width: 460px;
  height: 2.25rem;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--muted);
}

.product-search:focus-within {
  background: var(--bg);
  border-color: var(--fg);
  box-shadow: 0 0 0 3px rgba(10, 10, 10, 0.06);
}

.product-search svg {
  width: 1rem;
  height: 1rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: 0 0 auto;
}

.product-search input {
  min-width: 0;
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  color: var(--fg);
  font: inherit;
  font-size: 0.9rem;
}

.product-search input::placeholder {
  color: var(--faint);
}

.product-table-wrap {
  flex: 1 1 auto;
  min-height: 0;
  max-height: calc(100vh - 8.5rem);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}

.product-table {
  width: 100%;
  min-width: 780px;
  border-collapse: collapse;
  table-layout: fixed;
}

.product-table th,
.product-table td {
  padding: 0.625rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
}

.product-table th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface);
  color: var(--faint);
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.product-table td {
  color: var(--muted);
  font-size: 0.85rem;
}

.product-table tbody tr:hover {
  background: var(--surface);
}

.product-table tbody tr:last-child td {
  border-bottom: none;
}

.table-empty {
  height: 8rem;
  text-align: center !important;
  color: var(--muted);
}

.select-col {
  width: 42px;
}

.actions-col {
  width: 52px;
  text-align: right;
}

.actions-col form {
  margin: 0;
  display: flex;
  justify-content: flex-end;
}

.checkbox-box {
  width: 1rem;
  height: 1rem;
  display: inline-block;
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  background: var(--bg);
  vertical-align: middle;
}

.product-cell {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
  color: var(--fg);
  text-decoration: none;
}

.product-cell:hover .product-name {
  text-decoration: underline;
}

.product-thumb-stack {
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  position: relative;
  display: block;
}

.product-thumb-stack img {
  position: absolute;
  width: 36px;
  height: 44px;
  object-fit: cover;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(10, 10, 10, 0.08);
}

.product-thumb-stack img:nth-child(1) {
  left: 0;
  top: 0;
  z-index: 3;
}

.product-thumb-stack img:nth-child(2) {
  left: 5px;
  top: 0;
  z-index: 2;
}

.product-thumb-stack img:nth-child(3) {
  left: 10px;
  top: 0;
  z-index: 1;
}

.product-thumb-stack:empty {
  border: 1px dashed var(--border-strong);
  border-radius: 4px;
  background: var(--surface);
}

.product-cell-copy {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.product-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--fg);
}

.product-subtitle,
.muted-table-text {
  color: var(--faint);
}

.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 1.35rem;
  padding: 0 0.5rem;
  border-radius: 999px;
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: #166534;
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.product-pagination {
  flex: 0 0 auto;
  min-height: 2.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  color: var(--muted);
  font-size: 0.8rem;
}

.pagination-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.pagination-actions a,
.pagination-actions > span:not(:nth-child(2)) {
  width: 1.75rem;
  height: 1.75rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  background: var(--bg);
}

.pagination-actions a:hover {
  color: var(--fg);
  border-color: var(--border-strong);
  background: var(--surface-hover);
}

.pagination-actions > span:not(:nth-child(2)) {
  opacity: 0.45;
}

.pagination-actions svg {
  width: 1rem;
  height: 1rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button {
  width: 2rem;
  height: 2rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--faint);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}

.icon-button svg {
  width: 1rem;
  height: 1rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button:hover {
  background: var(--surface-hover);
  border-color: var(--border);
  color: var(--fg);
}

.icon-button:focus-visible {
  outline: none;
  border-color: var(--fg);
  box-shadow: 0 0 0 3px rgba(10, 10, 10, 0.06);
}

.icon-button.danger-icon:hover,
.icon-button.danger-icon:focus-visible {
  background: #fef2f2;
  border-color: #fecaca;
  color: var(--danger);
}

.product-images {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
}

.product-images figure {
  margin: 0;
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
  background: var(--surface);
}

.product-images figure.ref-thumb {
  border-color: var(--border-strong);
  background: var(--bg);
}

.product-images img {
  width: 100%;
  aspect-ratio: 3/4;
  object-fit: cover;
  display: block;
}

.product-images figcaption {
  padding: 0.25rem 0.5rem;
  font-size: 0.65rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  text-align: center;
}

/* Product detail */
.product-detail-main {
  max-width: 720px;
  height: 100vh;
}

.product-detail {
  width: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.detail-heading {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}

.detail-heading form {
  margin: 0;
  flex: 0 0 auto;
}

.detail-heading-copy {
  min-width: 0;
}

.detail-back {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  margin-bottom: 0.4rem;
  color: var(--muted);
  font-size: 0.78rem;
  text-decoration: none;
}

.detail-back:hover {
  color: var(--fg);
}

.detail-back svg {
  width: 1rem;
  height: 1rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.detail-title {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 600;
  letter-spacing: 0;
  overflow-wrap: anywhere;
}

.detail-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem 0.55rem;
  margin-top: 0.3rem;
  color: var(--faint);
  font-size: 0.78rem;
}

.detail-meta > * + *::before {
  content: "/";
  margin-right: 0.55rem;
  color: var(--border-strong);
}

.detail-section {
  min-height: 0;
  min-width: 0;
}

.detail-section-heading {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 1rem;
  margin-bottom: 0.6rem;
}

.detail-section-heading h2 {
  margin: 0;
  font-size: 0.74rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--faint);
}

.detail-section-heading span {
  color: var(--muted);
  font-size: 0.78rem;
}

.detail-image-grid {
  display: flex;
  gap: 0.75rem;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.05rem 0.05rem 0.55rem;
  scroll-snap-type: x proximity;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
  -webkit-overflow-scrolling: touch;
}

.detail-image-grid::-webkit-scrollbar {
  height: 0.45rem;
}

.detail-image-grid::-webkit-scrollbar-track {
  background: transparent;
}

.detail-image-grid::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 999px;
}

.detail-image-card {
  flex: 0 0 clamp(168px, 23vw, 210px);
  margin: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg);
  min-width: 0;
  display: flex;
  flex-direction: column;
  scroll-snap-align: start;
  transition: border-color 0.12s ease;
}

.detail-image-card:hover {
  border-color: var(--border-strong);
}

.detail-image-card img {
  width: 100%;
  height: auto;
  object-fit: contain;
  display: block;
  background: var(--bg);
}

.detail-image-footer {
  min-height: 2.45rem;
  padding: 0.5rem 0.75rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  color: var(--muted);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.detail-image-footer span,
.detail-image-footer a,
.detail-image-footer {
  min-width: 0;
}

.detail-image-footer span,
.detail-ref .detail-image-footer {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.detail-image-footer a {
  flex: 0 0 auto;
  color: var(--fg);
  text-decoration: none;
}

.detail-image-footer a:hover {
  text-decoration: underline;
}

.detail-empty {
  flex: 1 0 100%;
  margin: 0;
  padding: 1.25rem;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  color: var(--muted);
  background: var(--surface);
  font-size: 0.85rem;
}

/* Hover magnifier */
#magnifier {
  position: fixed;
  top: 50%;
  right: 1.5rem;
  transform: translateY(-50%);
  width: min(36vw, 480px);
  aspect-ratio: 3/4;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface) 0 0/auto no-repeat;
  box-shadow: var(--shadow-lg);
  z-index: 60;
  pointer-events: none;
  animation: fade 0.12s ease-out;
}

#magnifier[hidden] {
  display: none;
}

@media (max-width: 960px) {
  #magnifier {
    display: none !important;
  }
  .stats-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .stats-panels {
    grid-template-columns: 1fr;
  }
}
@media (max-width: 720px) {
  body {
    padding-top: 54px;
  }
  .sidebar,
  .sidebar.collapsed {
    flex-direction: row;
    width: 100%;
    height: 54px;
    bottom: auto;
    padding: 0.5rem 0.75rem;
    align-items: center;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .sidebar .brand,
  .sidebar.collapsed .brand {
    padding: 0;
    margin: 0 0.75rem 0 0;
    visibility: visible;
    height: auto;
  }
  .sidebar-nav,
  .sidebar.collapsed .sidebar-nav {
    flex-direction: row;
    gap: 0.25rem;
    flex: 1;
  }
  .sidebar-nav a,
  .sidebar.collapsed .sidebar-nav a {
    padding: 0.4rem 0.625rem;
    justify-content: flex-start;
  }
  .sidebar-nav a > span,
  .sidebar.collapsed .sidebar-nav a > span,
  .sidebar-logout button > span,
  .sidebar.collapsed .sidebar-logout button > span {
    display: inline;
  }
  .sidebar-toggle {
    display: none;
  }
  main {
    padding: 1rem 1rem;
  }
  .product-index-toolbar {
    flex-wrap: wrap;
    gap: 0.75rem;
  }
  .product-search {
    order: 3;
    flex-basis: 100%;
    max-width: none;
    margin: 0;
  }
  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
@media (max-width: 560px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }
  .results {
    grid-template-columns: 1fr;
  }
  .product-images {
    grid-template-columns: repeat(2, 1fr);
  }
  .detail-image-card {
    flex-basis: min(72vw, 210px);
  }
  .row-pair {
    grid-auto-flow: row;
    grid-auto-columns: auto;
    gap: 1.25rem;
  }
}
