:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --line: #e2e5ea;
  --text: #15181d;
  --muted: #6b7380;
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --accent-soft: #e8efff;
  --danger: #d33a3a;
  --ok: #1f9d55;
  --ai: #fff6d6;
  --ai-line: #e8c547;
  --input: #ffffff;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .05);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --card: #171b21;
    --line: #2a3039;
    --text: #e8ebef;
    --muted: #8b93a0;
    --accent: #5b8cff;
    --accent-ink: #0b0e12;
    --accent-soft: #1d2840;
    --danger: #ff6b6b;
    --ok: #3ccf7e;
    --ai: #3a3216;
    --ai-line: #a88a25;
    --input: #0f1216;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.spacer { flex: 1; }
kbd {
  font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 4px;
}

/* ---------- buttons & inputs ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--card); color: var(--text);
  font: inherit; font-weight: 500; text-decoration: none; white-space: nowrap;
  cursor: pointer; user-select: none;
  transition: background .12s, border-color .12s, opacity .12s;
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { border-color: var(--line); }
.btn.small { min-height: 30px; padding: 0 10px; font-size: 13px; }
.btn.icon { width: 34px; min-height: 34px; padding: 0; }

input[type=text], input[type=password], input[type=search], input[type=date], textarea, select {
  width: 100%; min-height: 40px; padding: 8px 10px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--input); color: var(--text);
  font: inherit;
}
textarea { resize: vertical; min-height: 64px; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

/* ---------- top bar ---------- */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 17px; }
.product-picker { display: flex; gap: 8px; align-items: center; flex: 1; min-width: 260px; }
.product-picker select { max-width: 320px; }
.topbar-right { display: flex; gap: 6px; align-items: center; }

main { max-width: 1400px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }

.empty { max-width: 560px; margin: 40px auto; text-align: center; padding: 32px; }
.empty p { color: var(--muted); margin: 10px 0 20px; }

/* ---------- capture + form ---------- */

#workspace { display: grid; gap: 16px; }
.grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; align-items: start; }

.cam-wrap {
  position: relative;
  aspect-ratio: 4 / 3;
  background: #0b0d10;
  border-radius: 8px;
  overflow: hidden;
  display: flex; align-items: center; justify-content: center;
}
.cam-wrap video, .cam-wrap img { width: 100%; height: 100%; object-fit: contain; }
.cam-placeholder { color: #9aa3ae; text-align: center; display: grid; gap: 4px; }
.cam-placeholder small { color: #6b7380; }

.ai-overlay {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center; gap: 10px;
  background: rgba(0, 0, 0, .55); color: #fff; font-weight: 500;
}
.spinner {
  width: 20px; height: 20px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .3); border-top-color: #fff;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.cam-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
#btnUpload { position: relative; }

.ai-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.toggle { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.ai-status {
  margin-top: 10px; padding: 10px 12px; border-radius: 8px;
  background: var(--accent-soft); font-size: 14px;
}
.ai-status.err { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.hint-line { margin: 10px 0 0; }

.form-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.fg { display: grid; gap: 4px; }
.fg > label { font-size: 13px; font-weight: 500; color: var(--muted); }
.fg[data-type=textarea] { grid-column: 1 / -1; }
.fg-check label { display: flex; align-items: center; gap: 8px; min-height: 40px; cursor: pointer; font-size: 15px; color: var(--text); }
.fg.ai-filled input, .fg.ai-filled textarea, .fg.ai-filled select { background: var(--ai); border-color: var(--ai-line); }
.fg-check.ai-filled label { background: var(--ai); border-radius: 8px; padding: 0 8px; }

.form-actions { display: flex; gap: 8px; margin-top: 16px; }
.form-actions .primary { flex: 1; min-height: 44px; font-size: 16px; }

/* ---------- list ---------- */

.list-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.list-head h2 { margin-right: auto; }
.list-head input[type=search] { width: 240px; min-height: 34px; }
.list-export { display: flex; gap: 6px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; white-space: nowrap; }
td { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td[data-type=number] { font-variant-numeric: tabular-nums; }
tbody tr:hover { background: color-mix(in srgb, var(--accent) 5%, transparent); }
tr.flash { animation: flash 1.4s ease-out; }
@keyframes flash { from { background: var(--ai); } to { background: transparent; } }

.col-photo { width: 64px; }
.col-date { white-space: nowrap; }
.col-actions { text-align: right; white-space: nowrap; width: 1%; }
.thumb { width: 48px; height: 48px; object-fit: cover; border-radius: 6px; display: block; cursor: zoom-in; background: var(--line); }
.thumb.none { display: flex; align-items: center; justify-content: center; color: var(--muted); cursor: default; }

/* ---------- dialogs ---------- */

dialog {
  width: min(720px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); color: var(--text);
  padding: 20px;
}
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog h2 { margin-bottom: 14px; }

.lbl { display: grid; gap: 4px; margin-bottom: 12px; font-size: 13px; font-weight: 500; }
.fields-editor-head { display: flex; justify-content: space-between; align-items: center; margin: 6px 0 8px; }
.field-rows { display: grid; gap: 8px; }
.field-row { display: grid; gap: 6px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; }
.f-main { display: grid; grid-template-columns: 1fr 140px auto; gap: 6px; }
.f-btns { display: flex; gap: 4px; }
.dialog-actions { display: flex; gap: 8px; margin-top: 18px; align-items: center; }

.image-dialog { width: auto; max-width: calc(100vw - 24px); padding: 12px; text-align: center; }
.image-dialog img { max-width: 100%; max-height: calc(100vh - 120px); display: block; margin: 0 auto 10px; border-radius: 8px; }

dialog.narrow { width: min(420px, calc(100vw - 24px)); }

/* ---------- start from photos ---------- */

.photo-start {
  margin-bottom: 16px; padding: 14px;
  border: 1px dashed var(--line); border-radius: 10px;
  background: color-mix(in srgb, var(--accent) 4%, transparent);
}
.ps-head { display: grid; gap: 2px; margin-bottom: 10px; }
.ps-cam { position: relative; margin-bottom: 10px; border-radius: 8px; overflow: hidden; background: #0b0d10; aspect-ratio: 4 / 3; max-height: 320px; }
.ps-cam video { width: 100%; height: 100%; object-fit: contain; display: block; }
.ps-cam .btn { position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%); }
.ps-thumbs { display: flex; flex-wrap: wrap; gap: 8px; }
.ps-thumbs:not(:empty) { margin-bottom: 10px; }
.ps-thumb { position: relative; width: 72px; height: 72px; }
.ps-thumb img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; display: block; }
.ps-del {
  position: absolute; top: -6px; right: -6px; width: 22px; height: 22px;
  border: 0; border-radius: 50%; background: #1d232b; color: #fff; font-size: 11px; cursor: pointer;
}
.ps-actions, .ps-suggest { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.ps-suggest { margin-top: 10px; }
.ps-suggest input { flex: 1; min-width: 200px; }
#psAddItemsRow { margin-top: 10px; }
.field-row.ai-filled { border-color: var(--ai-line); background: color-mix(in srgb, var(--ai) 50%, transparent); }
.spinner.dark { display: inline-block; width: 14px; height: 14px; vertical-align: -2px; border-color: color-mix(in srgb, var(--text) 20%, transparent); border-top-color: var(--text); }
.dialog-title-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.dialog-title-row h2 { margin: 0; }

/* ---------- accounts ---------- */

.user-menu { position: relative; }
.user-menu > summary { list-style: none; }
.user-menu > summary::-webkit-details-marker { display: none; }
.user-menu .menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20;
  min-width: 180px; padding: 6px;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .15);
}
.user-menu form { margin: 0; }
.menu-item {
  display: block; width: 100%; text-align: left;
  padding: 8px 10px; border: 0; border-radius: 6px;
  background: transparent; color: var(--text); font: inherit; cursor: pointer;
}
.menu-item:hover { background: var(--accent-soft); }

.by { display: block; font-size: 12px; }
.tag {
  display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); font-size: 11px; font-weight: 600; vertical-align: middle;
}
.users-table tr.inactive td { opacity: .55; }
.users-table td { max-width: none; }
.user-form { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }
.user-form h3 { margin-bottom: 10px; }
.user-form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; align-items: end; }
.user-active { margin-bottom: 12px; min-height: 40px; }
.login-card .lbl { margin-bottom: 0; }

@media (max-width: 640px) {
  .user-form-grid { grid-template-columns: 1fr; }
  .users-table th:nth-child(4), .users-table td:nth-child(4) { display: none; }
}

/* ---------- toast ---------- */

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  z-index: 100; max-width: calc(100vw - 32px);
  padding: 10px 16px; border-radius: 8px;
  background: #1d232b; color: #fff; font-weight: 500;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
}
.toast.ok { background: var(--ok); }
.toast.err { background: var(--danger); }

/* ---------- login ---------- */

.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: min(340px, 100%); display: grid; gap: 12px; }
.error { color: var(--danger); margin: 0; }

/* ---------- mobile ---------- */

@media (max-width: 900px) {
  .grid { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  main { padding: 10px; gap: 10px; }
  .topbar { padding: 8px 10px; gap: 8px; }
  .product-picker { min-width: 0; width: 100%; order: 3; }
  .product-picker select { max-width: none; flex: 1; min-width: 0; }
  .topbar-right { margin-left: auto; }
  .topbar-right .muted { display: none; }
  .card { padding: 12px; }
  .fields { grid-template-columns: 1fr; }
  .f-main { grid-template-columns: 1fr 110px; }
  .f-btns { grid-column: 1 / -1; justify-content: flex-end; }
  .list-head input[type=search] { width: 100%; order: 3; }

  /* table becomes cards */
  #itemsTable thead { display: none; }
  #itemsTable, #itemsTable tbody, #itemsTable tr, #itemsTable td { display: block; width: 100%; }
  #itemsTable tr {
    display: grid; grid-template-columns: 56px 1fr; gap: 2px 12px;
    padding: 10px 0; border-bottom: 1px solid var(--line);
  }
  #itemsTable td { border: 0; padding: 0; max-width: none; white-space: normal; }
  #itemsTable td.col-photo { grid-row: span 20; }
  #itemsTable td[data-label]:not(:empty)::before { content: attr(data-label) ": "; color: var(--muted); font-size: 12px; }
  #itemsTable td[data-label]:empty { display: none; }
  #itemsTable td.col-actions { text-align: left; margin-top: 6px; }
}
