/* =============================================================================
 * Pllato Suite — UI Components
 * =============================================================================
 *
 * Семантические primitive-компоненты на новых токенах (design-tokens.css).
 * Применяются в новых/редизайн-секциях. Existing legacy-классы (.card-old,
 * .deals-toolbar, и т.п.) пока не трогаем — мигрируем модуль за модулем.
 *
 * Принципы:
 *   - Все размеры через токены (height, padding, radius, font).
 *   - Один паттерн = везде (Card/Badge/Tabs/Field/Modal).
 *   - States: hover, focus-visible, active, disabled, empty.
 *   - Keyboard: tab order + focus indicator (жёлтое glow) везде.
 *   - Long values: ellipsis + tooltip pattern.
 * ========================================================================== */

/* =============================================================================
 * CARD — основной контейнер контента
 * ========================================================================== */
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  padding: 16px 20px;
  position: relative;
}
.card-elevated {
  box-shadow: var(--shadow-card);
}
.card-interactive {
  cursor: pointer;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast), transform var(--transition-fast);
}
.card-interactive:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-card-hover);
}
.card-interactive:active { transform: translateY(0); }
.card-interactive:focus-visible {
  outline: none;
  box-shadow: var(--shadow-focus);
  border-color: var(--brand-primary);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border-default);
  background: var(--bg-surface);
}
.card-title {
  font-family: var(--font-family-heading);
  font-size: var(--font-size-md);
  font-weight: var(--font-weight-semibold);
  color: var(--text-primary);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.card-subtitle {
  font-size: var(--font-size-sm);
  color: var(--text-muted);
}
.card-body { padding: 16px 20px; }
.card-footer {
  display: flex;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid var(--border-default);
  background: var(--bg-base);
  justify-content: flex-end;
}

/* Compact card — для kanban (плотные) */
.card-compact { padding: 12px 14px; }
.card-compact .card-header { padding: 10px 14px; }
.card-compact .card-body { padding: 10px 14px; }

/* =============================================================================
 * BADGE — статусные/информационные чипы
 * ========================================================================== */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 20px;
  padding: 0 8px;
  border-radius: var(--radius-pill);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-medium);
  line-height: 1;
  background: var(--status-neutral-bg);
  color: var(--status-neutral);
  border: 1px solid transparent;
  white-space: nowrap;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.badge.badge-lg {
  height: 24px;
  padding: 0 10px;
  font-size: var(--font-size-sm);
}
.badge.badge-sm {
  height: 16px;
  padding: 0 6px;
  font-size: 10px;
}

/* Status variants */
.badge.success { background: var(--status-success-bg); color: var(--status-success); }
.badge.warning { background: var(--status-warning-bg); color: var(--status-warning); }
.badge.danger  { background: var(--status-danger-bg);  color: var(--status-danger);  }
.badge.info    { background: var(--status-info-bg);    color: var(--status-info);    }
.badge.neutral { background: var(--status-neutral-bg); color: var(--status-neutral); }

/* Solid (более насыщенные — для важных меток) */
.badge.solid.success { background: var(--status-success); color: #fff; }
.badge.solid.warning { background: var(--status-warning); color: #fff; }
.badge.solid.danger  { background: var(--status-danger);  color: #fff; }
.badge.solid.info    { background: var(--status-info);    color: #fff; }
.badge.solid.brand   { background: var(--brand-primary);  color: var(--brand-on-primary); }

/* Outline (тонкие — для меток-категорий) */
.badge.outline {
  background: transparent;
  border-color: var(--border-strong);
  color: var(--text-secondary);
}

/* Counter badge — мини-кружок с числом (для notification bell etc) */
.badge-counter {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: var(--radius-pill);
  background: var(--status-danger);
  color: #fff;
  font-size: 10px;
  font-weight: var(--font-weight-bold);
  line-height: 1;
}

/* =============================================================================
 * TABS — горизонтальные табы (в карточке сделки и т.п.)
 * ========================================================================== */
.tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border-default);
  align-items: stretch;
}
.tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 40px;
  padding: 0 16px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font-family: var(--font-family-body);
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-medium);
  cursor: pointer;
  transition: color var(--transition-fast), border-color var(--transition-fast), background var(--transition-fast);
  position: relative;
  margin-bottom: -1px;     /* перекрыть border-bottom родителя для активного */
}
.tab:hover {
  color: var(--text-primary);
  background: var(--bg-surface-hover);
}
.tab.active {
  color: var(--text-primary);
  border-bottom-color: var(--brand-primary);
}
.tab:focus-visible {
  outline: none;
  background: var(--bg-surface-hover);
  box-shadow: inset 0 0 0 2px var(--brand-primary);
}
.tab .badge-counter,
.tab .badge {
  margin-left: 4px;
}

/* =============================================================================
 * FIELD — labelled form input (label + control + help/error)
 * ========================================================================== */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}
.field-label {
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  color: var(--text-secondary);
  line-height: var(--line-height-sm);
}
.field-label .required {
  color: var(--status-danger);
  margin-left: 2px;
}
.field-control { display: flex; align-items: stretch; gap: 6px; }
.field-help {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  line-height: var(--line-height-xs);
}
.field-error {
  font-size: var(--font-size-xs);
  color: var(--status-danger);
  line-height: var(--line-height-xs);
}
.field.has-error .input,
.field.has-error .input:focus { border-color: var(--status-danger); }
.field.has-error .input:focus { box-shadow: 0 0 0 3px var(--status-danger-bg); }

/* Horizontal field group (label left, control right) */
.field-row {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: 12px;
  align-items: start;
  margin-bottom: 14px;
}
.field-row .field-label { padding-top: 8px; }

/* =============================================================================
 * EMPTY STATE — единый паттерн пустых страниц/таблиц
 * ========================================================================== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  text-align: center;
  color: var(--text-muted);
}
.empty-state-icon {
  font-size: 40px;
  margin-bottom: 12px;
  opacity: .6;
}
.empty-state-title {
  font-family: var(--font-family-heading);
  font-size: var(--font-size-md);
  font-weight: var(--font-weight-semibold);
  color: var(--text-secondary);
  margin-bottom: 4px;
}
.empty-state-desc {
  font-size: var(--font-size-sm);
  max-width: 360px;
  margin-bottom: 16px;
  line-height: var(--line-height-sm);
}

/* =============================================================================
 * STACK utilities — гэп-helpers для разводки контента
 * ========================================================================== */
.stack-v   { display: flex; flex-direction: column; }
.stack-v-4 { display: flex; flex-direction: column; gap: 4px;  }
.stack-v-8 { display: flex; flex-direction: column; gap: 8px;  }
.stack-v-12{ display: flex; flex-direction: column; gap: 12px; }
.stack-v-16{ display: flex; flex-direction: column; gap: 16px; }
.stack-v-20{ display: flex; flex-direction: column; gap: 20px; }
.stack-v-24{ display: flex; flex-direction: column; gap: 24px; }

.stack-h   { display: flex; align-items: center; }
.stack-h-4 { display: flex; align-items: center; gap: 4px;  }
.stack-h-6 { display: flex; align-items: center; gap: 6px;  }
.stack-h-8 { display: flex; align-items: center; gap: 8px;  }
.stack-h-12{ display: flex; align-items: center; gap: 12px; }
.stack-h-16{ display: flex; align-items: center; gap: 16px; }

.stack-grow > :first-child { flex: 1; min-width: 0; }
.spacer { flex: 1; }

/* =============================================================================
 * TYPOGRAPHY utilities
 * ========================================================================== */
.text-xs  { font-size: var(--font-size-xs);   line-height: var(--line-height-xs); }
.text-sm  { font-size: var(--font-size-sm);   line-height: var(--line-height-sm); }
.text-base{ font-size: var(--font-size-base); line-height: var(--line-height-base); }
.text-md  { font-size: var(--font-size-md);   line-height: var(--line-height-md); }
.text-lg  { font-size: var(--font-size-lg);   line-height: var(--line-height-lg); }
.text-xl  { font-size: var(--font-size-xl);   line-height: var(--line-height-xl); }

.font-medium  { font-weight: var(--font-weight-medium); }
.font-semibold{ font-weight: var(--font-weight-semibold); }
.font-bold    { font-weight: var(--font-weight-bold); }

.text-primary   { color: var(--text-primary); }
.text-secondary { color: var(--text-secondary); }
.text-muted     { color: var(--text-muted); }
.text-success   { color: var(--status-success); }
.text-warning   { color: var(--status-warning); }
.text-danger    { color: var(--status-danger); }
.text-info      { color: var(--status-info); }
.text-brand     { color: var(--brand-primary); }

/* Truncate single-line text with ellipsis */
.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

/* =============================================================================
 * PAGE LAYOUT — единая шапка страницы (page-header pattern)
 * ========================================================================== */
.page {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}
.page-header-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 24px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-default);
  flex-shrink: 0;
}
.page-header-bar .page-title-block {
  flex: 1;
  min-width: 0;
}
.page-header-bar h1 {
  font-family: var(--font-family-heading);
  font-size: var(--font-size-xl);
  font-weight: var(--font-weight-bold);
  color: var(--text-primary);
  line-height: 1.2;
  margin: 0;
}
.page-header-bar .page-subtitle {
  font-size: var(--font-size-sm);
  color: var(--text-muted);
  margin-top: 2px;
}
.page-header-bar .page-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.page-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  min-height: 0;
}

/* Toolbar pattern — фильтры + действия под page-header */
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.toolbar .toolbar-search { flex: 1 1 240px; min-width: 200px; }
.toolbar .toolbar-divider {
  width: 1px;
  height: 24px;
  background: var(--border-default);
}

/* =============================================================================
 * SELECT — оформление select под единый стиль с .input
 * ========================================================================== */
.select {
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-family: var(--font-family-body);
  font-size: var(--font-size-base);
  line-height: var(--line-height-base);
  height: var(--size-control-md);
  padding: 0 28px 0 var(--padding-control-md);
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='%235e6470' d='M6 8L0 0h12z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 10px 7px;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
.select:hover { border-color: var(--text-disabled); }
.select:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: var(--shadow-focus);
}
.select.select-sm { height: var(--size-control-sm); font-size: var(--font-size-sm); }
.select.select-lg { height: var(--size-control-lg); font-size: var(--font-size-md); }

/* =============================================================================
 * KBD — клавиатурная подсказка (Tab, Enter, Esc, Cmd+K, и т.д.)
 * ========================================================================== */
.kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  background: var(--bg-base);
  border: 1px solid var(--border-default);
  border-bottom-width: 2px;
  border-radius: var(--radius-xs);
  font-family: var(--font-family-mono);
  font-size: 11px;
  color: var(--text-secondary);
  line-height: 1;
}

/* =============================================================================
 * DIVIDER
 * ========================================================================== */
.divider-h {
  height: 1px;
  background: var(--border-default);
  border: none;
  margin: 16px 0;
}
.divider-v {
  width: 1px;
  background: var(--border-default);
  align-self: stretch;
}

/* =============================================================================
 * AVATAR — единый паттерн (rail-avatar остался legacy)
 * ========================================================================== */
.ui-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-pill);
  background: var(--bg-active);
  color: var(--text-secondary);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
  flex-shrink: 0;
  overflow: hidden;
  text-transform: uppercase;
}
.ui-avatar.sm { width: 22px; height: 22px; font-size: 10px; }
.ui-avatar.lg { width: 36px; height: 36px; font-size: var(--font-size-sm); }
.ui-avatar.xl { width: 48px; height: 48px; font-size: var(--font-size-base); }
.ui-avatar img { width: 100%; height: 100%; object-fit: cover; }
