:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-2: #f0f0f3;
  --text: #1c1c22;
  --muted: #6b6b76;
  --border: #e2e2e8;
  --accent: #6d5dfc;
  --accent-text: #ffffff;
  --accent-soft: #ecebff;
  --ok: #1f9d55;
  --warn: #c98a00;
  --error: #d64545;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131317;
    --surface: #1c1c22;
    --surface-2: #25252d;
    --text: #ececf1;
    --muted: #9a9aa6;
    --border: #2f2f38;
    --accent: #8b7eff;
    --accent-text: #0f0f14;
    --accent-soft: #2a2745;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

[hidden] { display: none !important; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 20px; }
h3 { font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.strong { color: var(--text); font-weight: 600; }
.pad { padding: 16px; }
.error { color: var(--error); margin: 8px 0 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; padding: 8px 14px; font: inherit; cursor: pointer; text-decoration: none;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.wide { width: calc(100% - 24px); margin: 8px 12px 16px; }

.chip {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 999px; padding: 5px 12px; font: inherit; cursor: pointer;
}
.chip.small { padding: 2px 8px; font-size: 11px; }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.badge {
  display: inline-block; margin-left: 6px; padding: 1px 7px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); font-size: 11px;
}
.badge.ok { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }

input, textarea, select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
}
textarea { width: 100%; resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }

.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; }

/* вход */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; display: grid; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px;
}
.login-card p { margin: 0; }

/* каркас */
.app {
  display: grid; grid-template-columns: 320px 1fr; grid-template-rows: 56px 1fr;
  height: 100vh;
}
.topbar {
  grid-column: 1 / -1; display: flex; align-items: center; gap: 16px; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 16px; }
.brand span { color: var(--accent); }
.shop-status { margin-left: auto; display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }

.sidebar { border-right: 1px solid var(--border); background: var(--surface); overflow-y: auto; }
.search { position: sticky; top: 0; padding: 12px; background: var(--surface); border-bottom: 1px solid var(--border); }
.search input { width: 100%; }
.products { list-style: none; margin: 0; padding: 0; }
.product {
  display: flex; gap: 10px; align-items: center; padding: 8px 12px; cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.product:hover { background: var(--surface-2); }
.product.active { background: var(--accent-soft); }
.product img, .product .noimg {
  width: 48px; height: 48px; border-radius: 6px; object-fit: cover; flex: none; background: var(--surface-2);
}
.product-info { min-width: 0; }
.product-title { overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }

.main { overflow-y: auto; padding: 20px; display: grid; gap: 16px; align-content: start; }
.empty { max-width: 520px; margin: 80px auto; text-align: center; }

.product-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.product-head h2 { margin-bottom: 4px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.card-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }

/* галерея */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.tile { position: relative; margin: 0; border: 2px solid transparent; border-radius: var(--radius); padding: 4px; }
.tile.selected { border-color: var(--accent); background: var(--accent-soft); }
.tile.reference { border-color: var(--warn); border-style: dashed; }
.tile.disabled { opacity: .5; }
.tile-img { display: block; width: 100%; padding: 0; border: 0; background: var(--surface-2); border-radius: 8px; cursor: pointer; overflow: hidden; }
.tile-img img, .tile-img .noimg { display: block; width: 100%; aspect-ratio: 1; object-fit: contain; }
.tile figcaption { display: flex; justify-content: space-between; align-items: center; gap: 4px; padding: 6px 2px 0; }
.variants { color: var(--muted); padding: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.check {
  position: absolute; top: 10px; left: 10px; width: 24px; height: 24px; border-radius: 50%;
  background: var(--accent); color: var(--accent-text); display: grid; place-items: center; font-weight: 700;
}

/* редактор */
.editor { display: grid; gap: 12px; }
.editor .card-head { margin-bottom: 0; }
.presets { display: grid; gap: 8px; }
.preset-group { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.preset-group > .muted { width: 64px; }
.check-row { display: flex; align-items: center; gap: 8px; color: var(--muted); }

/* вкладки */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin: -4px 0 12px; }
.tab { border: 0; background: none; color: var(--muted); font: inherit; padding: 8px 12px; cursor: pointer; border-bottom: 2px solid transparent; }
.tab.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

/* задачи */
.jobs { display: grid; gap: 16px; }
.job { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.job-head { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.status { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); }
.status.done { color: var(--ok); }
.status.failed { color: var(--error); }
.status.running, .status.queued { color: var(--warn); }
.job-prompt { margin: 8px 0; white-space: pre-wrap; }
.job-images { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.out { margin: 0; }
.out img { display: block; width: 100%; aspect-ratio: 1; object-fit: contain; background: var(--surface-2); border-radius: 8px; }
.out-img { display: block; width: 100%; padding: 0; border: 0; background: none; cursor: zoom-in; }
.out figcaption { padding-top: 6px; display: grid; gap: 6px; }
.out-actions { display: flex; flex-wrap: wrap; gap: 4px; }
.out.source img { opacity: .9; }
.out.placeholder { aspect-ratio: 1; display: grid; place-items: center; background: var(--surface-2); border-radius: 8px; }
.spinner {
  width: 32px; height: 32px; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--accent);
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* история */
.journal { display: grid; gap: 10px; }
.entry { display: flex; gap: 12px; border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; }
.entry-thumb img, .entry-thumb.noimg { width: 96px; height: 96px; object-fit: contain; background: var(--surface-2); border-radius: 6px; display: block; }
.entry-body { display: grid; gap: 4px; align-content: start; }
.entry-body p { margin: 0; }

/* диалоги */
dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text); padding: 0; }
dialog::backdrop { background: rgb(0 0 0 / .55); }
.confirm form { padding: 20px; max-width: 440px; display: grid; gap: 16px; }
.confirm p { margin: 0; }
.viewer { width: min(96vw, 1100px); }
.viewer-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.compare { position: relative; height: min(80vh, 900px); background: var(--surface-2); user-select: none; }
.compare img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.compare-after { position: absolute; inset: 0; }
.compare input[type=range] {
  position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: ew-resize;
}
.compare-label {
  position: absolute; top: 12px; padding: 2px 8px; border-radius: 6px;
  background: rgb(0 0 0 / .55); color: #fff; font-size: 12px; pointer-events: none;
}
.compare-label.left { left: 12px; }
.compare-label.right { right: 12px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 10;
  max-width: min(92vw, 560px); padding: 10px 16px; border-radius: 10px;
  background: var(--text); color: var(--bg); box-shadow: 0 8px 24px rgb(0 0 0 / .2);
}
.toast.error { background: var(--error); color: #fff; }
.toast.ok { background: var(--ok); color: #fff; }

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 56px auto 1fr; height: auto; }
  .sidebar { max-height: 40vh; border-right: 0; border-bottom: 1px solid var(--border); }
  .main { padding: 16px; }
}
