/* ================================================================
   Универсальные стили для списочных страниц портала.
   Применяются на: Контрагенты, Номенклатура, Заказы и т.д.
   Цель: единый «идеальный» список — sticky-шапка, поиск, фильтры,
   sticky-thead, кликабельная сортировка по колонкам.
   ================================================================ */

/* Высота sticky-шапки страницы — используется sticky-thead для top-offset.
   Можно переопределить в section CSS, если шапка выше/ниже. */
:root {
  --list-header-h: 64px;
}

/* === Карточка контрагента (dossier-модалка) ============================ */
.cp-row { cursor: pointer; }
.cp-row:hover td { background: rgba(0,122,255,0.03); }

.cp-dossier-card {
  width: 95vw;
  max-width: 1100px;
  padding: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: 90vh;
}
.cp-dossier-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border, rgba(0,0,0,0.08));
  background: #fafbfc;
  flex-shrink: 0;
}
.cp-dossier-title { min-width: 0; }
.cp-dossier-name {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-primary, #1d1d1f);
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cp-dossier-subtitle {
  font-size: 13px;
  color: var(--text-secondary, #6e6e72);
  margin-top: 2px;
}
.cp-dossier-header-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.cp-dossier-body {
  display: grid;
  grid-template-columns: minmax(0, 40fr) minmax(0, 60fr);
  gap: 0;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}
.cp-dossier-left {
  padding: 18px 22px;
  border-right: 1px solid var(--border, rgba(0,0,0,0.06));
  overflow-y: auto;
  background: #fcfcfd;
}
.cp-dossier-right {
  padding: 18px 22px;
  overflow-y: auto;
}
@media (max-width: 800px) {
  .cp-dossier-body { grid-template-columns: 1fr; }
  .cp-dossier-left { border-right: none; border-bottom: 1px solid var(--border, rgba(0,0,0,0.06)); }
}

.cp-dossier-section { margin-bottom: 20px; }
.cp-dossier-section:last-child { margin-bottom: 0; }
.cp-dossier-section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  font-weight: 600;
  color: var(--text-secondary, #6e6e72);
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border, rgba(0,0,0,0.06));
}
.cp-dossier-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 18px;
  padding: 0 6px;
  margin-left: 4px;
  border-radius: 9px;
  background: rgba(0,0,0,0.06);
  color: var(--text-secondary, #6e6e73);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
}
.cp-add-btn {
  border: 1px solid var(--border, rgba(0,0,0,0.12));
  background: transparent;
  color: var(--accent, #007AFF);
  padding: 3px 10px;
  border-radius: 6px;
  font-size: 11.5px;
  font-weight: 500;
  cursor: pointer;
  text-transform: none;
  letter-spacing: 0;
  font-family: inherit;
  transition: all 0.15s;
}
.cp-add-btn:hover {
  background: rgba(0,122,255,0.06);
  border-color: rgba(0,122,255,0.3);
}

/* Список реквизитов слева */
.cp-info-grid {
  display: grid;
  grid-template-columns: minmax(120px, auto) 1fr;
  gap: 6px 14px;
  margin: 0;
  font-size: 13px;
}
.cp-info-grid dt {
  color: var(--text-secondary, #6e6e72);
  font-weight: 500;
}
.cp-info-grid dd {
  color: var(--text-primary, #1d1d1f);
  margin: 0;
  word-break: break-word;
}

/* Задолженность */
.cp-debts {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.cp-debt-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 8px;
  background: rgba(0,0,0,0.03);
  font-size: 13px;
}
.cp-debt-row.is-pos  { background: rgba(52,199,89,0.10);  color: #1c6e36; }
.cp-debt-row.is-neg  { background: rgba(255,59,48,0.10);  color: #a8231b; }
.cp-debt-row .cp-debt-label { color: inherit; font-weight: 500; }
.cp-debt-row .cp-debt-value { font-weight: 700; font-variant-numeric: tabular-nums; }

/* Список (контакты, договора) */
.cp-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.cp-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border: 1px solid var(--border, rgba(0,0,0,0.06));
  border-radius: 8px;
  font-size: 13px;
  background: #fff;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
.cp-list-item:hover {
  background: rgba(0,122,255,0.04);
  border-color: rgba(0,122,255,0.25);
}
.cp-list-item-main {
  display: inline-flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}
.cp-list-item-name {
  font-weight: 600;
  color: var(--text-primary, #1d1d1f);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cp-list-item-sub {
  font-size: 12px;
  color: var(--text-secondary, #6e6e72);
}
.cp-list-item-meta {
  font-size: 12px;
  color: var(--text-secondary, #6e6e72);
  white-space: nowrap;
  flex-shrink: 0;
}
.cp-empty-state {
  font-size: 12.5px;
  color: var(--text-secondary, #6e6e73);
  padding: 8px 0;
  font-style: italic;
}

/* Дерево документов */
.cp-tree {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.cp-tree-group {
  border: 1px solid var(--border, rgba(0,0,0,0.06));
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
}
.cp-tree-group-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: #f3f5f8;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  font-weight: 600;
  color: var(--text-secondary, #6e6e72);
  user-select: none;
}
.cp-tree-group-content { display: block; }

/* Узел документа (заказ или одиночный) */
.cp-tree-node {
  border-top: 1px solid var(--border, rgba(0,0,0,0.04));
  transition: background 0.1s;
}
.cp-tree-node:first-of-type { border-top: none; }
.cp-tree-node-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  font-size: 13px;
}
.cp-tree-node.has-children .cp-tree-node-head {
  cursor: pointer;
  user-select: none;
}
.cp-tree-node.has-children .cp-tree-node-head:hover { background: rgba(0,122,255,0.04); }
.cp-tree-node .chevron {
  display: inline-block;
  width: 12px;
  font-size: 10px;
  color: var(--text-secondary, #6e6e73);
  transition: transform 0.18s;
  flex-shrink: 0;
}
.cp-tree-node.is-expanded .chevron { transform: rotate(90deg); }
.cp-tree-node-spacer { display: inline-block; width: 12px; flex-shrink: 0; }
.cp-tree-node-meta {
  margin-left: auto;
  font-size: 12px;
  color: var(--text-secondary, #6e6e72);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.cp-doc-link {
  color: var(--accent, #007AFF);
  text-decoration: none;
  font-weight: 600;
  cursor: pointer;
}
.cp-doc-link:hover { text-decoration: underline; }
.cp-doc-link-sub { font-weight: 500; }

.cp-tree-children {
  display: none;
  margin: 0 12px 8px 32px;
  padding-left: 10px;
  border-left: 2px solid rgba(0,122,255,0.15);
  flex-direction: column;
  gap: 4px;
}
.cp-tree-node.is-expanded .cp-tree-children { display: flex; }
.cp-tree-child {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  padding: 3px 0;
  color: var(--text-primary, #1d1d1f);
}
.cp-tree-child-meta {
  margin-left: auto;
  color: var(--text-secondary, #6e6e72);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.cp-balance-pos  { color: #1c6e36; font-weight: 600; }
.cp-balance-neg  { color: #a8231b; font-weight: 600; }
.cp-balance-zero { color: #6e6e72; }
.u-c-income  { color: #248a3d; font-weight: 600; }
.u-c-expense { color: #a8231b; font-weight: 600; }

/* === Sticky шапка детализации документа (orders, sales, invoices и т.п.) === */
.detail-page-header {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--bg, #f5f5f7);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 28px 0 12px 0;
  margin: -28px 0 16px 0;
  flex-wrap: wrap;
}
.detail-page-header .detail-title-block {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}
.detail-page-header .detail-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  flex-wrap: wrap;
}

/* === Sticky шапка списка: заголовок | поиск | кнопки === */
.list-page-header {
  position: sticky;
  top: 0;
  z-index: 30;
  /* Grid с тремя колонками: title слева, search по центру (фиксированной ширины),
     actions справа. minmax(0,1fr) предотвращает «расталкивание» от длинного title. */
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(240px, 400px) minmax(0, 1fr);
  align-items: center;
  gap: 16px;
  /* Подложка под фон страницы — без неё под header просвечивает контент */
  background: var(--bg, #f5f5f7);
  /* Внизу padding (а не margin) — он становится частью sticky-зоны.
     Тогда thead начинается СРАЗУ под нижней границей шапки и при scroll=0
     уже находится на финальной sticky-позиции, движения нет. */
  padding: 28px 0 20px 0;
  margin: -28px 0 0 0;
}
/* Вариант шапки без поля поиска — title слева, actions справа */
.list-page-header.list-page-header--no-search {
  grid-template-columns: 1fr auto;
}
@media (max-width: 900px) {
  .list-page-header,
  .list-page-header.list-page-header--no-search {
    grid-template-columns: 1fr;
  }
}

.list-page-title-block { flex-shrink: 0; min-width: 0; }
.list-page-title-block .page-title    { margin: 0; }
.list-page-title-block .page-subtitle { margin: 2px 0 0 0; }

/* Статистические бейджи в шапке (под subtitle) — для разделов с агрегатами */
.list-page-stats {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}
.list-page-stat-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: rgba(0,0,0,0.04);
  border-radius: 8px;
  font-size: 12px;
  white-space: nowrap;
}
.list-page-stat-pill .stat-label {
  color: var(--text-secondary, #6e6e72);
  font-weight: 500;
}
.list-page-stat-pill .stat-value {
  color: var(--text-primary, #1d1d1f);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.list-page-stat-pill--overdue { background: rgba(255,59,48,0.10); }
.list-page-stat-pill--overdue .stat-value { color: #b1271b; }
.list-page-stat-pill--upcoming { background: rgba(0,122,255,0.10); }
.list-page-stat-pill--upcoming .stat-value { color: #0a3d8c; }
.list-page-stat-pill--income { background: rgba(52,199,89,0.10); }
.list-page-stat-pill--income .stat-value { color: #248a3d; }
.list-page-stat-pill--expense { background: rgba(255,149,0,0.12); }
.list-page-stat-pill--expense .stat-value { color: #a16207; }

/* Actions-блок справа: «Фильтр» + «+ Добавить» */
.list-page-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  justify-self: end;
  flex-wrap: wrap;
}

/* Глобальный поиск по центру */
.list-page-search {
  position: relative;
  width: 100%;
  justify-self: center;
}
.list-page-search-input {
  width: 100%;
  height: 36px;
  padding: 8px 36px 8px 38px;
  border: 1px solid var(--border, rgba(0,0,0,0.12));
  border-radius: 10px;
  background: var(--bg-card, #fff);
  font-size: 13.5px;
  color: var(--text-primary, #1d1d1f);
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.list-page-search-input:focus {
  outline: none;
  border-color: var(--accent, #007AFF);
  box-shadow: 0 0 0 3px rgba(0,122,255,0.12);
}
.list-page-search-icon {
  position: absolute;
  left: 12px; top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary, #6e6e73);
  pointer-events: none;
  display: inline-flex;
}
.list-page-search-clear {
  position: absolute;
  right: 8px; top: 50%;
  transform: translateY(-50%);
  width: 22px; height: 22px;
  display: none;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: rgba(0,0,0,0.08);
  color: var(--text-secondary, #6e6e73);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}
.list-page-search.has-value .list-page-search-clear { display: inline-flex; }
.list-page-search-clear:hover {
  background: rgba(0,0,0,0.16);
  color: var(--text-primary, #1d1d1f);
}

/* === Dropdown-меню «Фильтр» (поверх контента, не толкает список) === */
.filter-dropdown-wrap {
  position: relative;
  display: inline-flex;
}
.filter-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.filter-btn-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  margin-left: 2px;
  border-radius: 9px;
  background: var(--accent, #007AFF);
  color: white;
  font-size: 10px;
  font-weight: 700;
}
/* utilities.css (.u-d-none) грузится РАНЬШЕ этой секции → перебивается одиночным
   правилом .filter-btn-badge { display: inline-flex }. Поднимаем специфичность
   до двух классов, чтобы скрытие через u-d-none фактически работало. */
.filter-btn-badge.u-d-none { display: none; }
.filter-btn.is-open {
  background: rgba(0,122,255,0.08);
  border-color: var(--accent, #007AFF);
  color: var(--accent, #007AFF);
}

.filter-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 40;
  width: 520px;
  max-width: calc(100vw - 80px);
  background: var(--bg-card, #fff);
  border: 1px solid var(--border, rgba(0,0,0,0.12));
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.12), 0 2px 6px rgba(0,0,0,0.06);
  padding: 14px;
  display: none;
}
.filter-dropdown.filter-dropdown--wide { width: 680px; }
.filter-dropdown.is-open { display: block; }

.filter-dropdown-section {
  margin-bottom: 12px;
}
.filter-dropdown-section:last-of-type { margin-bottom: 16px; }
.filter-dropdown-label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  color: var(--text-secondary, #6e6e72);
  font-weight: 600;
  margin-bottom: 6px;
}
.filter-dropdown-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  border-top: 1px solid var(--border, rgba(0,0,0,0.06));
  padding-top: 12px;
}

/* Pills для фильтров (multi-select через .active) */
.list-filter-pills {
  display: inline-flex;
  gap: 4px;
  flex-wrap: wrap;
}
.list-filter-pill {
  border: 1px solid var(--border, rgba(0,0,0,0.12));
  background: var(--bg-card, #fff);
  color: var(--text-primary, #1d1d1f);
  font-size: 12.5px;
  font-weight: 500;
  padding: 5px 11px;
  border-radius: 999px;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}
.list-filter-pill:hover {
  background: rgba(0,122,255,0.05);
  border-color: rgba(0,122,255,0.3);
}
.list-filter-pill.active {
  background: var(--accent, #007AFF);
  border-color: var(--accent, #007AFF);
  color: white;
}

/* === Таблица: sticky thead + sortable заголовки === */
.list-table-card {
  background: var(--bg-card, #fff);
  border: 1px solid var(--border, rgba(0,0,0,0.08));
  border-radius: 12px;
  overflow: visible; /* sticky thead не работает с overflow: hidden/auto */
}
.list-table-card .data-table thead th {
  position: sticky;
  /* Заголовки таблицы приклеиваются под sticky list-page-header */
  top: var(--list-header-h, 64px);
  z-index: 5;
  /* macOS-style серый заголовок таблицы (как в Finder / NSTableView).
     Непрозрачный — sticky thead не должен просвечивать tbody при скролле. */
  background: #ececee;
  color: var(--text-secondary, #6e6e72);
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.10);
}

/* Уменьшенное вертикальное расстояние между записями */
.list-table-card .data-table tbody td {
  padding-top: 8px;
  padding-bottom: 8px;
}
.list-table-card .data-table thead th.is-sortable {
  cursor: pointer;
  user-select: none;
  transition: background 0.12s;
}
.list-table-card .data-table thead th.is-sortable:hover {
  background: #e2e2e5;
}
.sort-indicator {
  display: inline-block;
  margin-left: 4px;
  font-size: 10px;
  color: var(--text-secondary, #6e6e73);
  opacity: 0.35;
  transition: opacity 0.12s, color 0.12s;
}
.list-table-card .data-table thead th.is-sorted .sort-indicator {
  opacity: 1;
  color: var(--accent, #007AFF);
}

/* === Компактная иконочная кнопка для шапки (например «Выделить») === */
/* По умолчанию белая (унаследует .btn-outline), при .active — синяя. */
.btn-icon-only {
  padding: 0;
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.btn-icon-only.active {
  background: var(--accent, #007AFF);
  border-color: var(--accent, #007AFF);
  color: #fff;
}
.btn-icon-only.active:hover {
  background: var(--accent-hover, #0062cc);
  border-color: var(--accent-hover, #0062cc);
}

/* === Bulk actions bar (общий для всех списков) === */
/* По умолчанию скрыт; показывается классом .active.
   Не используем u-d-none — он перебивается этой section-CSS из-за порядка загрузки. */
.bulk-actions-bar {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  margin-bottom: 12px;
  background: rgba(0,122,255,0.08);
  border: 1px solid rgba(0,122,255,0.20);
  border-radius: 10px;
}
.bulk-actions-bar.active { display: flex; }
.bulk-actions-info { font-size: 13px; color: var(--text-primary); }
.bulk-actions-info strong {
  color: var(--accent, #007AFF);
  font-weight: 700;
}
.bulk-actions-buttons { display: flex; gap: 8px; }

/* === Колонка чекбоксов в списках с bulk-режимом === */
/* Скрыта по умолчанию, показывается когда у table-card класс .is-selection-mode. */
.bulk-col-sel {
  width: 40px;
  text-align: center;
  padding: 0 6px;
}
.list-table-card:not(.is-selection-mode) .bulk-col-sel { display: none; }
.bulk-col-sel input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
  vertical-align: middle;
}
.list-table-card.is-selection-mode .data-table tbody tr { cursor: pointer; }
.bulk-row-selected td,
.bulk-row-selected:hover td {
  background: rgba(0,122,255,0.09) !important;
}

/* === Подвал модалки: Удалить слева, Отмена/Сохранить справа === */
/* Используется вместо .modal-actions когда нужна кнопка Удалить (existing entity).
   Левая обёртка занимает место даже при скрытой кнопке — Отмена/Сохранить остаются справа. */
.modal-ftr {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 18px;
  margin-top: 4px;
  border-top: 1px solid var(--border, rgba(0,0,0,0.06));
}
.modal-ftr-left  { display: flex; align-items: center; gap: 12px; min-width: 1px; }
.modal-ftr-right { display: flex; align-items: center; gap: 12px; }
