/* CRM — единая тема. Светлая и тёмная схемы через prefers-color-scheme. */

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --border: #e3e6ea;
  --border-strong: #cbd2d9;
  --text: #1a1d21;
  --text-muted: #6b7480;
  --accent: #2563eb;
  --accent-soft: #eff4ff;

  --ok: #0f7a4d;
  --ok-bg: #e6f5ee;
  --warn: #9a6200;
  --warn-bg: #fdf3e0;
  --danger: #b32020;
  --danger-bg: #fdeaea;
  --info: #1c5fa8;
  --info-bg: #e9f1fb;
  --muted-bg: #eef0f3;

  --radius: 8px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.04);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171b;
    --surface: #1b1f24;
    --surface-2: #21262c;
    --border: #2c333b;
    --border-strong: #3c4550;
    --text: #e6e9ed;
    --text-muted: #98a2b0;
    --accent: #6aa0ff;
    --accent-soft: #1d2a41;

    --ok: #5fd39b;
    --ok-bg: #16302478;
    --warn: #f0b95c;
    --warn-bg: #3a2c1178;
    --danger: #ff8080;
    --danger-bg: #3d1c1c78;
    --info: #7db6f5;
    --info-bg: #16283f78;
    --muted-bg: #272d34;

    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

body.no-scroll {
  overflow: hidden;
}

h1,
h2,
h3 {
  margin: 0;
  font-weight: 600;
}

/* --- Каркас ------------------------------------------------------------ */

.layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: 100vh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px 12px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar__brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px 12px;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.sidebar__logo {
  color: var(--accent);
  font-size: 20px;
}

.sidebar__footer {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.sidebar__user {
  font-size: 12px;
  color: var(--text-muted);
  padding: 0 8px;
  overflow-wrap: anywhere;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav__item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius);
  color: var(--text);
  text-decoration: none;
  transition: background 0.12s;
}

.nav__item:hover {
  background: var(--surface-2);
}

.nav__item--active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.nav__icon {
  font-size: 15px;
  width: 18px;
  text-align: center;
}

.main {
  padding: 20px 24px 48px;
  min-width: 0; /* иначе grid-колонка растягивается под широкую таблицу */
}

/* --- Шапка страницы ---------------------------------------------------- */

.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.page-title {
  font-size: 20px;
  font-weight: 650;
}

.page-subtitle {
  color: var(--text-muted);
  font-size: 13px;
  margin-top: 2px;
}

.breadcrumbs {
  display: flex;
  gap: 6px;
  align-items: center;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 10px;
}

/* --- Вкладки ----------------------------------------------------------- */

.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
  overflow-x: auto;
}

.tab {
  padding: 8px 14px;
  border: none;
  background: none;
  color: var(--text-muted);
  font: inherit;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}

.tab:hover {
  color: var(--text);
}

.tab--active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

/* --- Плитки сводки ----------------------------------------------------- */

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.stat__label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.stat__value {
  font-size: 19px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.stat--danger .stat__value {
  color: var(--danger);
}

/* --- Фильтры ----------------------------------------------------------- */

.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 12px;
}

.filter {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 140px;
}

.filter--toggle {
  flex-direction: row;
  align-items: center;
  gap: 6px;
  min-width: 0;
  padding-bottom: 6px;
  cursor: pointer;
  user-select: none;
}

.filter__label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.input {
  padding: 7px 9px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  min-width: 0;
}

.input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}

select[multiple].input {
  padding: 4px;
}

/* --- Кнопки ------------------------------------------------------------ */

.btn {
  padding: 7px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}

.btn:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: var(--accent);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.btn--primary:hover:not(:disabled) {
  filter: brightness(1.08);
  background: var(--accent);
}

.btn--ghost {
  background: none;
  border-color: transparent;
  color: var(--text-muted);
}

.btn--block {
  width: 100%;
}

.btn--icon {
  padding: 4px 10px;
  line-height: 1.2;
}

.link {
  color: var(--accent);
  text-decoration: none;
}

.link:hover {
  text-decoration: underline;
}

/* --- Таблица ----------------------------------------------------------- */

.table-wrap {
  /* Широкая таблица прокручивается внутри себя — страница вбок не едет. */
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.table {
  width: 100%;
  border-collapse: collapse;
  white-space: nowrap;
}

.table th,
.table td {
  padding: 9px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  user-select: none;
}

.table th.sortable {
  cursor: pointer;
}

.table th.sortable:hover {
  color: var(--text);
}

.table th.sorted {
  color: var(--accent);
}

.sort-arrow {
  margin-left: 4px;
  opacity: 0.6;
  font-size: 10px;
}

.table tbody tr:last-child td {
  border-bottom: none;
}

.table tbody tr[tabindex]:hover {
  background: var(--surface-2);
  cursor: pointer;
}

.table tbody tr:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.table tr.row--danger td:first-child {
  box-shadow: inset 3px 0 0 var(--danger);
}

.table tr.row--highlight {
  background: var(--accent-soft);
}

.align-right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.align-center {
  text-align: center;
}

.mono {
  font-family: var(--mono);
  font-size: 12px;
}

.muted {
  color: var(--text-muted);
}

.nowrap {
  white-space: nowrap;
}

.cell-stack {
  display: flex;
  flex-direction: column;
  line-height: 1.3;
}

.cell-stack__sub {
  font-size: 11px;
  color: var(--text-muted);
}

/* --- Бейджи ------------------------------------------------------------ */

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 100px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.badge--ok {
  background: var(--ok-bg);
  color: var(--ok);
}
.badge--warn {
  background: var(--warn-bg);
  color: var(--warn);
}
.badge--danger {
  background: var(--danger-bg);
  color: var(--danger);
}
.badge--info {
  background: var(--info-bg);
  color: var(--info);
}
.badge--muted {
  background: var(--muted-bg);
  color: var(--text-muted);
}

.warn-icon {
  color: var(--warn);
  margin-left: 4px;
  cursor: help;
}

/* --- Карточки ---------------------------------------------------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 12px;
}

.card__title {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.fields {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 10px 18px;
}

.field__label {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 1px;
}

.field__value {
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.field--strong .field__value {
  font-size: 16px;
  font-weight: 650;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 12px;
  align-items: start;
}

/* Блок просрочки в карточке кредита */
.dpd-hero {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  align-items: center;
  padding: 14px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  margin-bottom: 12px;
}

.dpd-hero--overdue {
  border-color: var(--danger);
  background: var(--danger-bg);
}

.dpd-hero__value {
  font-size: 30px;
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.dpd-hero--overdue .dpd-hero__value {
  color: var(--danger);
}

/* --- Состояния --------------------------------------------------------- */

.state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 32px 16px;
  color: var(--text-muted);
  text-align: center;
  white-space: normal;
}

.state--error .state__title {
  color: var(--danger);
  font-weight: 600;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* --- Пагинация --------------------------------------------------------- */

.paginator {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 2px;
  color: var(--text-muted);
  font-size: 13px;
  flex-wrap: wrap;
}

.paginator__controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.paginator__page {
  font-variant-numeric: tabular-nums;
}

/* --- Боковая панель ---------------------------------------------------- */

.drawer[hidden] {
  display: none;
}

.drawer {
  position: fixed;
  inset: 0;
  z-index: 50;
}

.drawer__overlay {
  position: absolute;
  inset: 0;
  background: rgba(16, 24, 40, 0.45);
}

.drawer__panel {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(720px, 100%);
  background: var(--bg);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  animation: slide-in 0.16s ease-out;
}

@keyframes slide-in {
  from {
    transform: translateX(24px);
    opacity: 0;
  }
}

.drawer__header {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.drawer__title {
  font-size: 16px;
}

.drawer__subtitle {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.drawer__content {
  padding: 14px 16px 32px;
}

/* --- Вход -------------------------------------------------------------- */

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}

.login {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  max-width: 340px;
  padding: 26px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.login__brand {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
  text-align: center;
}

.login__hint {
  margin: -6px 0 0;
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
}

.login__error {
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 13px;
}

/* --- Узкие экраны ------------------------------------------------------ */

@media (max-width: 860px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .sidebar__footer {
    margin: 0 0 0 auto;
    flex-direction: row;
    align-items: center;
    border-top: none;
    padding-top: 0;
  }

  .nav {
    flex-direction: row;
  }

  .main {
    padding: 14px;
  }
}
