:root {
  color-scheme: light;
  --ink: #18201d;
  --muted: #66706b;
  --line: #dce2df;
  --surface: #ffffff;
  --surface-alt: #f4f7f5;
  --green: #146c43;
  --green-dark: #0d5232;
  --blue: #245ea8;
  --amber: #a45c0a;
  --danger: #a52a2a;
  --shadow: 0 12px 30px rgba(24, 32, 29, 0.1);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  background: var(--surface-alt);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.5;
}

button, input, select { font: inherit; letter-spacing: 0; }
button, label { -webkit-tap-highlight-color: transparent; }
button:focus-visible, input:focus-visible, select:focus-visible, label:focus-within {
  outline: 3px solid rgba(36, 94, 168, 0.28);
  outline-offset: 2px;
}

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.header-inner, main {
  width: min(1440px, calc(100% - 40px));
  margin: 0 auto;
}

.header-inner {
  min-height: 88px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
}

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 24px; line-height: 1.2; letter-spacing: 0; }
h2 { margin-bottom: 4px; font-size: 20px; line-height: 1.3; letter-spacing: 0; }
.eyebrow { margin-bottom: 4px; color: var(--green); font-size: 12px; font-weight: 750; text-transform: uppercase; }
.muted { color: var(--muted); }

main { padding: 36px 0 72px; }

.upload-view {
  min-height: calc(100vh - 196px);
  display: grid;
  place-items: center;
}

.upload-panel {
  width: min(560px, 100%);
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.drop-zone {
  min-height: 190px;
  margin: 24px 0 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 2px dashed #aeb8b3;
  border-radius: 6px;
  background: #fafcfb;
  cursor: pointer;
  transition: border-color 140ms ease, background 140ms ease;
}

.drop-zone:hover, .drop-zone.is-dragging { border-color: var(--green); background: #f0f8f3; }
.drop-title { max-width: 90%; overflow-wrap: anywhere; color: var(--blue); font-weight: 750; }
.drop-meta { color: var(--muted); font-size: 14px; }

.button, .icon-button {
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 700;
}

.button { padding: 9px 16px; }
.button:disabled { cursor: not-allowed; opacity: 0.48; }
.button-primary { background: var(--green); color: #fff; }
.button-primary:hover:not(:disabled) { background: var(--green-dark); }
.button-secondary { border-color: #b8c2bd; background: var(--surface); color: var(--ink); }
.button-secondary:hover { border-color: #7f8c85; background: #f8faf9; }
.button-wide { width: 100%; }
.icon-button { width: 42px; padding: 0; background: transparent; color: var(--muted); font-size: 24px; }
.icon-button:hover { background: #edf1ef; color: var(--ink); }

.header-actions, .group-actions, .selection-toolbar { display: flex; align-items: center; gap: 10px; }

.status-view, .error-view {
  max-width: 760px;
  margin: 14vh auto 0;
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.status-line, .review-summary, .group-header, .dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
}

#progressValue { color: var(--green); font-size: 24px; }
.progress-track { height: 8px; margin: 20px 0 12px; overflow: hidden; background: #e2e8e5; border-radius: 4px; }
.progress-bar { width: 0; height: 100%; background: var(--green); transition: width 240ms ease; }

.review-summary { padding-bottom: 24px; border-bottom: 1px solid var(--line); }
.summary-stats { display: flex; gap: 28px; margin: 0; }
.summary-stats div { min-width: 68px; }
.summary-stats dt { color: var(--muted); font-size: 12px; text-transform: uppercase; }
.summary-stats dd { margin: 0; font-size: 22px; font-weight: 750; }

.group-actions { min-height: 72px; justify-content: space-between; }
.save-state { color: var(--muted); font-size: 14px; }

.selection-toolbar {
  position: sticky;
  z-index: 5;
  top: 12px;
  width: fit-content;
  margin: 16px auto 0;
  padding: 8px 10px 8px 16px;
  background: var(--ink);
  color: #fff;
  border-radius: 8px;
  box-shadow: var(--shadow);
}
.selection-toolbar select { min-width: 180px; height: 40px; padding: 0 32px 0 10px; border: 0; border-radius: 4px; }
.selection-toolbar .icon-button { color: #fff; }

.product-group { padding: 24px 0 30px; border-top: 1px solid var(--line); }
.product-group:first-child { border-top: 0; }
.group-title-wrap { display: flex; align-items: center; gap: 10px; min-width: 0; }
.group-name { min-width: 170px; max-width: 360px; height: 42px; padding: 8px 10px; border: 1px solid transparent; border-radius: 5px; background: transparent; color: var(--ink); font-size: 18px; font-weight: 750; }
.group-name:hover, .group-name:focus { border-color: #b8c2bd; background: var(--surface); }
.group-count { color: var(--muted); font-size: 13px; white-space: nowrap; }
.remove-group { flex: 0 0 auto; }

.image-grid {
  margin-top: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(172px, 1fr));
  gap: 12px;
}

.image-card {
  position: relative;
  min-width: 0;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 7px;
}
.image-card.is-selected { border-color: var(--blue); box-shadow: 0 0 0 2px rgba(36, 94, 168, 0.18); }
.image-card.is-tag { border-color: #cf9b55; }
.image-select { position: absolute; z-index: 2; top: 8px; left: 8px; width: 20px; height: 20px; accent-color: var(--blue); }
.image-preview-button { width: 100%; padding: 0; display: block; border: 0; background: #eef1ef; cursor: zoom-in; }
.image-preview-button img { width: 100%; aspect-ratio: 1 / 1; display: block; object-fit: contain; }
.image-meta { padding: 10px; }
.image-name { margin: 0 0 7px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 700; }
.badges { min-height: 22px; display: flex; flex-wrap: wrap; gap: 5px; }
.badge { padding: 2px 6px; border-radius: 4px; background: #e8f2ec; color: var(--green-dark); font-size: 11px; font-weight: 700; }
.badge-low { background: #fff0dc; color: var(--amber); }
.badge-fallback { background: #fce5e5; color: var(--danger); }
.badge-tag { background: #fff0dc; color: #784307; }
.empty-group { margin: 16px 0 0; padding: 24px; border: 1px dashed #b8c2bd; color: var(--muted); text-align: center; }

.preview-dialog { width: min(1120px, calc(100% - 32px)); max-height: calc(100vh - 32px); padding: 0; border: 0; border-radius: 8px; box-shadow: 0 22px 70px rgba(0, 0, 0, 0.25); }
.preview-dialog::backdrop { background: rgba(15, 22, 19, 0.64); }
.dialog-header { padding: 18px 20px; border-bottom: 1px solid var(--line); }
.preview-grid { padding: 20px; display: grid; grid-template-columns: 1fr 1fr; gap: 20px; background: var(--surface-alt); }
.preview-grid figure { min-width: 0; margin: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 7px; overflow: hidden; }
.preview-grid img { width: 100%; height: min(64vh, 620px); display: block; object-fit: contain; }
.preview-grid figcaption { padding: 10px 12px; border-top: 1px solid var(--line); color: var(--muted); font-size: 13px; font-weight: 700; }

.error-view { border-top: 4px solid var(--danger); }
.error-view .button { margin-top: 10px; }
.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; }
[hidden] { display: none !important; }

@media (max-width: 720px) {
  .header-inner, main { width: min(100% - 24px, 1440px); }
  .header-inner { min-height: 76px; }
  .header-actions .button-secondary { display: none; }
  main { padding-top: 20px; }
  .upload-panel, .status-view, .error-view { padding: 20px; }
  .review-summary, .group-header { align-items: flex-start; }
  .review-summary { flex-direction: column; }
  .summary-stats { width: 100%; justify-content: space-between; gap: 12px; }
  .selection-toolbar { width: 100%; flex-wrap: wrap; }
  .selection-toolbar select { min-width: 130px; max-width: 45vw; }
  .image-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .group-name { width: 100%; min-width: 0; }
  .preview-grid { grid-template-columns: 1fr; }
  .preview-grid img { height: 42vh; }
}
