:root {
  --bg: #14161a;
  --panel: #1d2026;
  --line: #2c313a;
  --text: #e8eaed;
  --dim: #9aa3ad;
  --accent: #4da3ff;
  --danger: #e5534b;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}

.hidden { display: none !important; }

/* --- Вход --- */
.login {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: var(--bg);
}
.login-card { display: flex; flex-direction: column; gap: 10px; width: min(320px, 90vw); }
.login-card input, .login-card button {
  padding: 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); font-size: 16px;
}

/* --- Шапка --- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.tabs { display: flex; gap: 4px; flex: 1; }
.tab {
  border: none; background: none; color: var(--dim);
  padding: 8px 10px; border-radius: 8px; font-size: 15px;
}
.tab.active { color: var(--text); background: var(--panel); }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%; border: 1px solid var(--line);
  background: none; color: var(--dim); font-size: 18px; flex: none;
}
.plus {
  width: 38px; height: 38px; border-radius: 50%; border: none;
  background: var(--accent); color: #0d1117; font-size: 24px; line-height: 1; flex: none;
}

/* --- Фильтры --- */
.filters { display: flex; gap: 8px; padding: 10px 12px; }
.filters select, .filters input {
  padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel); color: var(--text);
}
.filters select { flex: 1; }
.filters input[type="search"] { flex: 2; min-width: 0; }
.filters .small { flex: none; }

/* --- Чипы-переключатели --- */
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: none; color: var(--dim);
  padding: 6px 12px; border-radius: 999px; font-size: 14px;
}
.chip.active { color: var(--text); background: var(--panel); border-color: var(--accent); }
#archive-switch, #library-switch, #publish-switch { padding: 10px 12px 0; }

/* --- Пагинация референсов --- */
.more { display: block; margin: 0 auto 24px; padding: 10px 18px; }

/* --- Карточки без файла (видео-мем, ссылка) --- */
.no-file {
  aspect-ratio: 1; display: flex; align-items: center; justify-content: center;
  background: var(--bg); font-size: 34px;
}

/* --- Результаты под сюжетом --- */
.results-strip { display: flex; gap: 8px; flex-wrap: wrap; }
.result-cell { position: relative; width: 84px; height: 84px; border-radius: 10px; overflow: hidden; cursor: pointer; }
.result-cell img { width: 100%; height: 100%; object-fit: cover; display: block; }
.result-cell .mini {
  position: absolute; bottom: 4px; width: 24px; height: 24px; border-radius: 50%;
  border: none; font-size: 13px; line-height: 1;
}
.result-cell .mini.ok { left: 4px; background: var(--accent); color: #0d1117; }
.result-cell .mini.no { right: 4px; background: rgba(13, 17, 23, 0.85); color: var(--danger); }

/* --- Очередь публикации --- */
.queue-row {
  grid-column: 1 / -1;
  display: flex; align-items: center; gap: 12px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 12px; cursor: pointer;
}
.queue-thumb {
  width: 52px; height: 52px; border-radius: 8px; overflow: hidden; flex: none;
  background: var(--bg); display: flex; align-items: center; justify-content: center; font-size: 20px;
}
.queue-thumb img { width: 100%; height: 100%; object-fit: cover; }
.queue-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.queue-when { font-size: 15px; }
.queue-caption { color: var(--dim); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.queue-actions { display: flex; gap: 6px; flex: none; }

.badge.meme { left: auto; right: 6px; top: 6px; color: var(--accent); }

#plots-refs img { width: 88px; height: 88px; }

/* --- Постановка в очередь --- */
.sch-time { display: flex; gap: 8px; }
.sch-time input {
  flex: 1; padding: 10px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit;
}
.item-preview a { color: var(--accent); word-break: break-all; }

/* --- Сетка --- */
.grid {
  display: grid; gap: 10px; padding: 4px 12px 24px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  scrollbar-gutter: stable;
}
.card { position: relative; border-radius: 12px; overflow: hidden; background: var(--panel); cursor: pointer; }
.card .thumb { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; }
.card.selected { outline: 2px solid var(--accent); }
.card .check:empty { display: none; }
.card .check {
  position: absolute; top: 6px; right: 6px;
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--accent); color: #0d1117;
  display: flex; align-items: center; justify-content: center; font-size: 15px;
}
.card .text-note {
  aspect-ratio: 1; padding: 12px; overflow: hidden; white-space: pre-wrap;
  color: var(--text); font-size: 14px;
}
.badge {
  position: absolute; top: 6px; left: 6px;
  background: rgba(13, 17, 23, 0.78); color: var(--text);
  border-radius: 6px; padding: 2px 7px; font-size: 12px;
}
.badge.collection { top: auto; bottom: 6px; left: 6px; right: auto; color: var(--dim); }
.badge.count { left: auto; right: 6px; }
.empty { color: var(--dim); text-align: center; padding: 48px 0; }

/* --- Панель выбора --- */
.selection-bar {
  position: fixed; left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom));
  z-index: 8; display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 12px;
}
.selection-bar span:first-child { color: var(--dim); margin-right: auto; }
.sel-collection { display: flex; gap: 6px; }
.sel-collection input {
  padding: 8px 10px; border-radius: 8px; width: 130px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
}

/* --- Модалки --- */
.modal {
  position: fixed; inset: 0; z-index: 10;
  background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: flex-end; justify-content: center;
}
@media (min-width: 600px) { .modal { align-items: center; } }
.modal-card {
  width: min(560px, 100%); max-height: 92vh; overflow-y: auto;
  background: var(--panel); border-radius: 16px 16px 0 0; padding: 14px;
  display: flex; flex-direction: column; gap: 10px;
}
@media (min-width: 600px) { .modal-card { border-radius: 16px; } }

.dropzone {
  border: 1px dashed var(--line); border-radius: 12px;
  padding: 10px; min-height: 60px; cursor: pointer;
  display: flex; flex-direction: column; gap: 8px;
}
.dropzone.drag { border-color: var(--accent); }
.dz-hint { color: var(--dim); text-align: center; padding: 14px 0; }
.dz-previews { display: flex; gap: 6px; flex-wrap: wrap; }
.dz-previews img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; }

textarea, .capture-collection input, #ref-tags, #ref-collection, #set-ollama {
  width: 100%; padding: 10px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
  font: inherit; resize: vertical;
}
.capture-collection input, #ref-tags, #ref-collection, #set-ollama { padding: 10px; }
select { padding: 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; }

.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.modal-actions button {
  padding: 10px 16px; border-radius: 10px; border: none; font-size: 15px;
}
button.small { padding: 7px 12px; border-radius: 8px; font-size: 14px; }
.btn-primary { background: var(--accent); color: #0d1117; }
.btn-ghost { background: none; color: var(--dim); }
.btn-danger { background: none; color: var(--danger); }
.btn-ghost, .btn-danger { border: 1px solid var(--line); }

/* --- Карточка --- */
.item-preview img { width: 100%; border-radius: 12px; display: block; }
.item-picks { display: flex; gap: 6px; flex-wrap: wrap; }
.item-picks img { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; cursor: pointer; opacity: 0.5; }
.item-picks img.active { opacity: 1; outline: 2px solid var(--accent); }
.item-meta { display: flex; gap: 14px; color: var(--dim); font-size: 13px; flex-wrap: wrap; }
.item-meta a { color: var(--accent); text-decoration: none; }

.provenance { display: flex; flex-direction: column; gap: 6px; color: var(--dim); font-size: 13px; }
.provenance a { color: var(--accent); text-decoration: none; }

.ref-description { color: var(--dim); font-size: 14px; }

/* --- Сюжеты и генерация --- */
.plot-card, .modal-card .plot-text { display: flex; flex-direction: column; gap: 8px; }
.plot-card {
  grid-column: 1 / -1;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px;
}
.plot-text { white-space: pre-wrap; }
.gen-plot { white-space: pre-wrap; }
.gen-prompt { display: flex; gap: 8px; align-items: flex-start; }
.gen-prompt > div {
  flex: 1; font-family: ui-monospace, Consolas, monospace; font-size: 13px;
  color: var(--dim); white-space: pre-wrap; word-break: break-word;
}
.plot-card .modal-actions { justify-content: flex-start; }
.strip { display: flex; gap: 6px; flex-wrap: wrap; }
.strip img { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; cursor: pointer; }
.gen-results { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
.gen-results img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 10px; cursor: pointer; }

#set-prompt { min-height: 120px; }

/* --- Тост --- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 10px 16px; z-index: 20; max-width: 90vw;
}
.toast.error { border-color: var(--danger); }
