/* ==========================================================================
   Задачи (/tasks) — what is running right now, and how to stop it.

   Built on the same two-column "list picks, pane explains" shape the rest of the
   panel already uses (see the dialogues page), not as a new pattern. The one thing
   this page does differently: it is grouped by MODULE, not flat. A flat list would
   be modules x accounts — seven times the account count — and the question this page
   answers ("what is my fleet doing") is asked per module, not per pair.
   ========================================================================== */
.tasks-page { display: grid; gap: 14px; }

.tasks-hero {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 1.1rem 1.25rem;
  border-radius: var(--radius-2xl);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
}
.tasks-hero-copy h2 { font-family: var(--font-display); font-size: 1.05rem; font-weight: 600; }
.tasks-hero-copy p { color: var(--color-text-muted); font-size: 0.8rem; margin-top: 0.3rem; max-width: 62ch; }
.tasks-hero-action { flex-shrink: 0; }

.tasks-bar { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }

/* Segmented Активные/История. Anchors, not buttons: each tab is a real URL, so it
   survives a reload and can be linked to — which matters here because "the tasks
   page, history, filtered to Рассылки ЛС" is a thing worth sending to yourself. */
.tasks-seg {
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  border-radius: var(--radius-full);
  background: var(--color-surface-muted);
  border: 1px solid var(--color-border);
}
.tasks-seg-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.85rem;
  border-radius: var(--radius-full);
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--color-text-muted);
  text-decoration: none;
}
.tasks-seg-btn:hover { color: var(--color-text); }
.tasks-seg-btn.is-on { background: var(--color-surface); color: var(--color-text); box-shadow: var(--shadow-sm); }
.tasks-seg-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  min-width: 1.35em;
  height: 1.35em;
  line-height: 1;
  padding: 0 0.3rem;
  border-radius: var(--radius-full);
  background: var(--color-accent);
  color: #fff;
  font-size: 0.68rem;
}

.tasks-chips { display: flex; align-items: center; gap: 0.35rem; flex-wrap: wrap; }
.tasks-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.7rem;
  border-radius: var(--radius-full);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-muted);
  font-size: 0.76rem;
  font-weight: 600;
  text-decoration: none;
}
.tasks-chip:hover { color: var(--color-text); }
.tasks-chip.is-on { background: var(--color-primary-bg); border-color: transparent; color: var(--color-primary-hover); }
.tasks-chip-count { color: var(--color-text-faint); font-size: 0.7rem; }
.tasks-chip.is-on .tasks-chip-count { color: inherit; opacity: 0.7; }

.tasks-split {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(300px, 0.9fr);
  gap: 14px;
  align-items: start;
}
@media (max-width: 1100px) {
  /* minmax(0, 1fr) rather than a bare 1fr — a `1fr` track carries an implicit
     min-width:auto and refuses to shrink below its widest child, which on a phone
     turns the log lines inside the detail pane into horizontal page scroll. Same
     fix, same reason, as the accounts grid. */
  .tasks-split { grid-template-columns: minmax(0, 1fr); }
}

.tasks-list { display: grid; gap: 12px; align-content: start; min-width: 0; }

.tasks-group {
  border-radius: var(--radius-2xl);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
  padding: 0.9rem 1rem 0.75rem;
}
.tasks-group-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  padding-bottom: 0.7rem;
  margin-bottom: 0.5rem;
  border-bottom: 1px solid var(--color-border);
}
.tasks-group-head h3 { font-size: 0.9rem; font-weight: 600; }
.tasks-group-head p { color: var(--color-text-muted); font-size: 0.72rem; margin-top: 0.15rem; }

.tasks-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  border-radius: var(--radius-lg);
  transition: background var(--transition-fast) var(--ease);
}
.tasks-row:hover { background: var(--color-surface-muted); }
/* Selection has to survive the 15s poll swapping this markup out, so it is derived
   from the ?run= in the URL server-side rather than held in JS state. */
.tasks-row.is-selected { background: var(--color-primary-bg); }
.tasks-row-main {
  flex: 1;
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) auto minmax(0, 6rem) auto;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 0.6rem;
  text-decoration: none;
  color: inherit;
}
@media (max-width: 620px) {
  .tasks-row-main { grid-template-columns: minmax(0, 1fr) auto; row-gap: 0.3rem; }
  .tasks-progress { display: none; }
}
.tasks-row-name {
  font-size: 0.82rem;
  font-weight: 600;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tasks-row-sub { display: block; font-weight: 400; font-size: 0.72rem; color: var(--color-text-muted); }
.tasks-row-meta { font-size: 0.72rem; color: var(--color-text-muted); white-space: nowrap; }
.tasks-row-count { font-size: 0.72rem; color: var(--color-text-muted); text-align: right; white-space: nowrap; }
.tasks-row-stop { flex-shrink: 0; padding-right: 0.35rem; }

/* Only drawn for modules that actually have a configured ceiling to count toward
   (Рассылки ЛС today) — the rest run open-endedly, and a bar with an invented
   denominator would be a lie about when they finish. */
.tasks-progress {
  height: 4px;
  border-radius: 99px;
  background: color-mix(in srgb, var(--color-text) 11%, transparent);
  overflow: hidden;
}
.tasks-progress > span { display: block; height: 100%; border-radius: inherit; background: var(--color-accent); }

.tasks-detail {
  border-radius: var(--radius-2xl);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
  padding: 1rem 1.1rem 1.1rem;
  min-width: 0;
  position: sticky;
  top: 0;
}
@media (max-width: 1100px) { .tasks-detail { position: static; } }
.tasks-detail.is-empty { display: flex; align-items: center; justify-content: center; min-height: 14rem; }
.tasks-detail-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--color-border);
}
.tasks-detail-module { font-size: 0.95rem; font-weight: 600; }
.tasks-detail-account { font-size: 0.78rem; color: var(--color-text-muted); margin-top: 0.15rem; }
.tasks-detail-actions { display: flex; align-items: center; gap: 0.4rem; flex-shrink: 0; }

.tasks-facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(7rem, 1fr));
  gap: 0.75rem 1rem;
  margin: 0.9rem 0 1rem;
}
.tasks-facts dt { color: var(--color-text-faint); font-size: 0.6rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.075em; }
.tasks-facts dd { font-size: 0.82rem; font-weight: 600; margin-top: 0.2rem; }
.tasks-fact-wide { grid-column: 1 / -1; }

/* Плотная сетка фактов — для прогонов сбора и поиска. У прогона модуля фактов
   четыре, и он спокойно помещается обычной сеткой; у поиска их под двадцать, и
   обычной сеткой карточка уезжала за экран с двумя прокрутками подряд.
   Экономия набирается из трёх мест, а не из уменьшения кегля:
     - подпись и значение в одну строку, а не одно под другим (было две строки на
       каждый факт);
     - подписи обычным регистром без разрядки: «МИН. КОММЕНТАРИЕВ НА ПОСТ» капслоком
       не помещалось в колонку и переносилось на три строки, из-за чего строка сетки
       становилась втрое выше самой высокой соседки;
     - разделитель — тонкая линия вместо вертикальных отступов. */
.tasks-facts.is-compact {
  grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
  gap: 0 1.4rem;
  margin: 0.6rem 0 0.7rem;
}
.tasks-facts.is-compact > div {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.3rem 0;
  border-bottom: 1px solid var(--color-border);
  min-width: 0;
}
.tasks-facts.is-compact dt {
  font-size: 0.72rem;
  font-weight: 400;
  text-transform: none;
  letter-spacing: normal;
  color: var(--color-text-muted);
  /* Подпись уступает место значению, а не наоборот: длинную подпись читатель
     достроит по смыслу, обрезанное число — нет. */
  min-width: 0;
}
.tasks-facts.is-compact dd {
  margin-top: 0;
  font-size: 0.78rem;
  text-align: right;
  overflow-wrap: anywhere;
}
/* Списки во всю ширину (аккаунты, запросы, ошибки источников) остаются в два уровня:
   значение у них длиннее подписи, и прижимать его вправо не к чему. */
.tasks-facts.is-compact > .tasks-fact-wide {
  display: block;
  padding-top: 0.35rem;
}
.tasks-facts.is-compact > .tasks-fact-wide dd { text-align: left; margin-top: 0.15rem; }

/* Why a run ended. "limit_reached" is the good outcome (the campaign did its job),
   "error"/"account" are the ones worth noticing — so they are the only coloured
   ones, matching the palette's rule that saturation means state. */
.tasks-reason { display: inline-flex; align-items: baseline; gap: 0.4rem; font-size: 0.74rem; font-weight: 600; }
.tasks-reason-user { color: var(--color-text-muted); }
.tasks-reason-limit_reached { color: var(--color-success-text); }
.tasks-reason-error { color: var(--color-danger-text); }
.tasks-reason-account { color: var(--color-warning-text); }
.tasks-reason-detail { font-weight: 400; color: var(--color-text-muted); font-size: 0.72rem; }

.tasks-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 2.5rem 1rem;
  text-align: center;
  color: var(--color-text-muted);
  font-size: 0.85rem;
}
.tasks-placeholder .icon { width: 1.5rem; height: 1.5rem; color: var(--color-text-faint); }
.tasks-placeholder p { font-size: 0.76rem; color: var(--color-text-faint); max-width: 40ch; }

/* ---- История: filters and pager ---- */
.tasks-filters {
  display: flex;
  align-items: flex-end;
  gap: 0.6rem;
  flex-wrap: wrap;
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-2xl);
  background: var(--color-surface);
}
.tasks-filter { display: flex; flex-direction: column; gap: 0.25rem; min-width: 9rem; }
.tasks-filter > span {
  font-size: 0.68rem;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--color-text-faint);
}
.tasks-filter .input { height: 2.1rem; font-size: 0.8rem; }
/* Pushed to the far end so the count and the reset read as the bar's result, not as
   another filter. */
.tasks-filter-summary {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.78rem;
  color: var(--color-text-muted);
}
.tasks-filter-reset { color: var(--color-primary); }
.tasks-filter-reset:hover { text-decoration: underline; }

.tasks-pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-top: 0.75rem;
}
.tasks-pager-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg, 0.6rem);
  background: var(--color-surface);
  font-size: 0.78rem;
  color: var(--color-text);
}
.tasks-pager-btn:hover { background: var(--color-surface-muted); }
/* The ends of the list keep their buttons in place rather than removing them — a pager
   that changes width as you reach the last page makes the target move under the cursor. */
.tasks-pager-btn.is-off { opacity: 0.4; pointer-events: none; }
.tasks-pager-at { font-size: 0.78rem; color: var(--color-text-muted); }

/* Настройки, с которыми прогон начался. Отделены от фактов сверху рамкой, а не просто
   отступом: факты — про сам прогон (когда, сколько, почему кончился), а это про то, с
   чем его запускали, и путать их не надо. Сетка та же, .tasks-facts, поэтому подписи
   выстраиваются в те же колонки, что и выше. */
.tasks-snapshot { border-top: 1px solid var(--color-border); padding-top: 0.85rem; }
.tasks-snapshot h4 {
  font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.075em;
  color: var(--color-text-muted); margin: 0;
}
.tasks-snapshot .tasks-facts { margin-bottom: 0.9rem; }
/* Правило выше идёт позже одноимённого по специфичности и перебило бы отступ плотной
   сетки обратно на просторный — поэтому повторяется здесь, а не там. */
.tasks-snapshot .tasks-facts.is-compact { margin-bottom: 0.5rem; }
/* Текст рассылки и инструкции для ИИ — единственное здесь, что читают строкой, а не
   цифрой: обычный вес, обычный кегль и перенос по словам вместо моноширинного. */
.tasks-snapshot-text {
  font-weight: 400 !important; font-size: 0.76rem !important;
  color: var(--color-text-muted); white-space: pre-wrap; overflow-wrap: anywhere;
}
.tasks-snapshot-empty {
  margin: 0.9rem 0 1rem; padding: 0.7rem 0.8rem;
  border: 1px dashed var(--color-border); border-radius: 12px;
  color: var(--color-text-muted); font-size: 0.75rem;
}

/* ==========================================================================
   Остановка задачи — она уезжает, а не исчезает между кадрами.

   Раньше остановленная строка пропадала вместе с подменой #main-content: клик,
   пустое место, и непонятно, остановилось именно то, на что нажали, или список
   просто перерисовался. Теперь строка сначала гаснет и схлопывается по высоте, и
   только потом приходит ответ сервера (submit придерживается на длительность
   анимации в pages/tasks.js).

   Высоту анимируем через max-height, а не height: реальная высота строки зависит
   от содержимого и на момент старта анимации неизвестна, а max-height с запасом
   даёт тот же схлопывающийся жест без её измерения.
   ========================================================================== */
@keyframes tasksRowLeave {
  0%   { opacity: 1; transform: none; max-height: 8rem; }
  35%  { opacity: 0.55; transform: translateX(10px) scale(0.995); max-height: 8rem; }
  100% { opacity: 0; transform: translateX(28px) scale(0.98); max-height: 0;
         margin-top: 0; margin-bottom: 0; padding-top: 0; padding-bottom: 0; }
}
.tasks-row.is-stopping,
.tasks-group.is-stopping {
  overflow: hidden;
  pointer-events: none;
  animation: tasksRowLeave 420ms var(--ease, cubic-bezier(0.4, 0, 0.2, 1)) forwards;
}
/* Карточка модуля уезжает чуть медленнее строки — она крупнее, и на той же
   скорости жест читается как рывок. */
.tasks-group.is-stopping { animation-duration: 520ms; }
/* Останавливают всё — уходит весь список разом, но с небольшой лесенкой, чтобы
   было видно, что это много задач, а не одна. */
.tasks-list.is-stopping-all .tasks-group { animation: tasksRowLeave 520ms var(--ease, cubic-bezier(0.4, 0, 0.2, 1)) forwards; overflow: hidden; }
.tasks-list.is-stopping-all .tasks-group:nth-child(2) { animation-delay: 60ms; }
.tasks-list.is-stopping-all .tasks-group:nth-child(3) { animation-delay: 120ms; }
.tasks-list.is-stopping-all .tasks-group:nth-child(n+4) { animation-delay: 180ms; }

/* Отключённые анимации — это просьба, а не предпочтение: жест заменяется мгновенным
   исчезновением, но само действие остаётся тем же. */
@media (prefers-reduced-motion: reduce) {
  .tasks-row.is-stopping,
  .tasks-group.is-stopping,
  .tasks-list.is-stopping-all .tasks-group { animation: none; opacity: 0; }
}

/* Точка «идёт прямо сейчас». Пульсирует не сама точка, а её ореол: мигающий текст
   рядом читается как ошибка, а ровная точка с дышащим кольцом — как работа. */
.tasks-live {
  position: relative;
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 0.45rem;
  border-radius: 50%;
  background: var(--color-success-text, var(--color-accent));
  flex: none;
  vertical-align: middle;
}
.tasks-live::after {
  content: '';
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  border: 1px solid var(--color-success-text, var(--color-accent));
  opacity: 0.5;
  animation: tasksLivePulse 2.2s ease-out infinite;
}
@keyframes tasksLivePulse {
  0%   { transform: scale(0.7); opacity: 0.55; }
  70%  { transform: scale(1.5); opacity: 0; }
  100% { transform: scale(1.5); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .tasks-live::after { animation: none; opacity: 0.35; }
}

/* Строка получает видимую цель клика: до этого выделялся только фон, и на светлой
   теме разница между «навёл» и «выбрано» была почти незаметна. */
.tasks-row { position: relative; }
.tasks-row::before {
  content: '';
  position: absolute;
  left: 0;
  top: 6px;
  bottom: 6px;
  width: 2px;
  border-radius: 99px;
  background: var(--color-accent);
  opacity: 0;
  transform: scaleY(0.4);
  transition: opacity var(--transition-fast) var(--ease), transform var(--transition-fast) var(--ease);
}
.tasks-row.is-selected::before { opacity: 1; transform: none; }
.tasks-row:hover::before { opacity: 0.35; transform: none; }
.tasks-row.is-past::before { display: none; }
/* Кнопка остановки проявляется по наведению на строку, но на тач-экранах ждать
   наведения нечего — там она видна всегда. */
@media (hover: hover) {
  .tasks-row-stop { opacity: 0; transition: opacity var(--transition-fast) var(--ease); }
  .tasks-row:hover .tasks-row-stop,
  .tasks-row:focus-within .tasks-row-stop { opacity: 1; }
}

/* ---- Правая колонка, пока ничего не выбрано ---- */
.tasks-empty-detail {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 0.4rem;
  padding: 1.75rem 1.25rem 1.5rem;
}
.tasks-empty-art {
  position: relative;
  display: grid;
  place-items: center;
  width: 3.25rem;
  height: 3.25rem;
  margin-bottom: 0.35rem;
  border-radius: var(--radius-2xl);
  background: var(--color-surface-muted);
  border: 1px solid var(--color-border);
  color: var(--color-text-faint);
}
.tasks-empty-art svg {
  width: 1.4rem;
  height: 1.4rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
/* Точка-курсор у угла карточки: она и есть подсказка «сюда попадёт выбранное». */
.tasks-empty-art i {
  position: absolute;
  right: -3px;
  bottom: -3px;
  width: 0.75rem;
  height: 0.75rem;
  border-radius: 50%;
  background: var(--color-accent);
  box-shadow: 0 0 0 3px var(--color-surface);
}
.tasks-empty-detail strong { font-size: 0.9rem; font-weight: 600; }
.tasks-empty-detail p { font-size: 0.78rem; color: var(--color-text-muted); max-width: 34ch; }
.tasks-empty-list {
  display: grid;
  gap: 0.3rem;
  margin-top: 0.55rem;
  padding: 0;
  list-style: none;
  text-align: left;
}
.tasks-empty-list li {
  position: relative;
  padding-left: 1.1rem;
  font-size: 0.76rem;
  color: var(--color-text-faint);
}
.tasks-empty-list li::before {
  content: '';
  position: absolute;
  left: 0.3rem;
  top: 0.52em;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--color-border-strong, var(--color-border));
}
