/*
 * Оформление CRM поверх app.css: переменные палитры переопределены здесь.
 * Тёмная тема в духе Black Dashboard (Creative Tim, MIT) с синим вместо
 * розового, шрифт Inter и плашки KPI как в Soft UI, ненавязчивая анимация.
 */

:root {
  --bg: #1e1e2f;
  --surface: #27293d;
  --text: rgba(255, 255, 255, 0.72);
  --heading: #ffffff;
  --muted: rgba(255, 255, 255, 0.5);
  --border: rgba(255, 255, 255, 0.08);
  --hover-bg: rgba(255, 255, 255, 0.05);
  --accent: #1d8cf8;
  --accent-hover: #3d9ff9;
  --accent-tint: rgba(29, 140, 248, 0.16);
  --success: #00f2c3;
  --success-tint: rgba(0, 242, 195, 0.14);
  --info: #11cdef;
  --info-tint: rgba(17, 205, 239, 0.14);
  --warning: #ff8d72;
  --warning-tint: rgba(255, 141, 114, 0.16);
  --danger: #fd5d93;
  --danger-tint: rgba(253, 93, 147, 0.16);
  --radius: 8px;
  --shadow: 0 1px 20px 0 rgba(0, 0, 0, 0.12);
  --shadow-sm: 0 1px 10px 0 rgba(0, 0, 0, 0.1);
  --blue-gradient: linear-gradient(135deg, #3358f4, #1d8cf8);
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

body {
  font-family: "Inter", -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

h1 {
  font-weight: 700;
  letter-spacing: -0.02em;
}

/* Сайдбар: синяя панель с градиентом на всю высоту экрана */

.layout {
  gap: 0;
}

.sidebar {
  flex-basis: 240px;
  width: 240px;
  margin: 0;
  border: none;
  border-radius: 0;
  background: linear-gradient(0deg, #3358f4, #1d8cf8);
  box-shadow: 4px 0 24px -8px rgba(29, 140, 248, 0.45);
  align-self: flex-start;
  position: sticky;
  top: 0;
  height: 100vh;
  max-height: 100vh;
  overflow-y: auto;
}

.sidebar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #fff;
  border-bottom: 1px solid rgba(255, 255, 255, 0.25);
  margin: 0 6px 12px;
  padding: 4px 4px 16px;
}

.sidebar .brand:hover {
  text-decoration: none;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.85);
  font-size: 14px;
}

.sidebar-nav a,
.sidebar-nav .nav-disabled {
  display: flex;
  align-items: center;
  gap: 12px;
  color: rgba(255, 255, 255, 0.82);
  font-size: 13px;
  font-weight: 500;
  transition: background 0.2s var(--ease), color 0.2s var(--ease), transform 0.2s var(--ease);
}

.sidebar-nav a:hover {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  transform: translateX(3px);
}

.sidebar-nav a.active {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  font-weight: 600;
}

.nav-icon {
  flex: 0 0 18px;
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.9;
}

/* Шапка сливается с фоном, как в Black Dashboard */

.topbar {
  background: transparent;
  border-bottom: none;
}

.user-name {
  color: var(--text);
}

/* Карточки и таблицы */

.card,
.table-wrap,
.detail-card,
.auth-box,
.form-narrow {
  border: 1px solid var(--border);
}

table {
  background: transparent;
}

th {
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
}

tbody tr {
  transition: background 0.15s var(--ease);
}

/* Бейджи: цвета текста под тёмный фон */

.badge-admin { color: #6bb6ff; }
.badge-manager,
.badge-ok,
.flash-success { color: #00f2c3; }
.badge-viewer { color: #11cdef; }
.badge-wait { color: #ff8d72; }
.form-error,
.flash-error { color: #fd5d93; }

/* Поля ввода и кнопки */

input[type="text"],
input[type="password"],
input[type="email"],
input[type="date"],
select,
textarea,
.copy-field {
  background: transparent;
  border-color: #2b3553;
  color: var(--heading);
  transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

input[type="text"]:focus,
input[type="password"]:focus,
input[type="email"]:focus,
input[type="date"]:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(29, 140, 248, 0.18);
}

select option {
  background: var(--surface);
}

input[type="date"] {
  color-scheme: dark;
}

.btn-primary {
  background: var(--blue-gradient);
  background-size: 200% 100%;
  box-shadow: 0 4px 14px -4px rgba(29, 140, 248, 0.6);
  transition: transform 0.2s var(--ease), box-shadow 0.2s var(--ease), background-position 0.3s var(--ease);
}

.btn-primary:hover {
  background: var(--blue-gradient);
  background-position: 100% 0;
  box-shadow: 0 8px 20px -6px rgba(29, 140, 248, 0.75);
}

/* Дашборд: KPI-плашки с иконкой в цветном квадрате (как в Soft UI) */

.kpi {
  position: relative;
  padding: 20px 20px 18px;
  transition: transform 0.25s var(--ease), box-shadow 0.25s var(--ease), border-color 0.25s var(--ease);
}

.kpi:hover {
  transform: translateY(-3px);
  border-color: rgba(29, 140, 248, 0.35);
  box-shadow: 0 12px 28px -12px rgba(0, 0, 0, 0.6);
}

.kpi-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}

.kpi-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.kpi-value {
  font-size: 24px;
  font-weight: 800;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.kpi-icon {
  flex: 0 0 44px;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  color: #fff;
  box-shadow: 0 6px 14px -6px rgba(0, 0, 0, 0.6);
}

.kpi-icon svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.kpi-icon-blue { background: linear-gradient(135deg, #3358f4, #1d8cf8); }
.kpi-icon-orange { background: linear-gradient(135deg, #ea0606, #ff8d72); }
.kpi-icon-dark { background: linear-gradient(135deg, #141727, #3a416f); border: 1px solid rgba(255, 255, 255, 0.1); }
.kpi-icon-green { background: linear-gradient(135deg, #17ad37, #00f2c3); }
.kpi-icon-cyan { background: linear-gradient(135deg, #2152ff, #11cdef); }

.kpi-sub-warn {
  color: #ff8d72;
}

/* Полосы графиков */

.bar-track {
  height: 8px;
  background: rgba(255, 255, 255, 0.06);
}

.bar-fill {
  background: var(--blue-gradient);
  transform-origin: left center;
}

/* Анимация: ненавязчивая, только при загрузке и при наведении */

@keyframes rise-in {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

@keyframes bar-grow {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}

.content > *,
.card-grid > .card,
.dash-stack > *,
.dash-cols > * {
  animation: rise-in 0.5s var(--ease) both;
}

.card-grid > .card:nth-child(2) { animation-delay: 0.04s; }
.card-grid > .card:nth-child(3) { animation-delay: 0.08s; }
.card-grid > .card:nth-child(4) { animation-delay: 0.12s; }
.card-grid > .card:nth-child(5) { animation-delay: 0.16s; }
.card-grid > .card:nth-child(6) { animation-delay: 0.2s; }
.card-grid > .card:nth-child(7) { animation-delay: 0.24s; }
.dash-stack > :nth-child(2) { animation-delay: 0.15s; }
.dash-stack > :nth-child(3) { animation-delay: 0.22s; }
.dash-stack > :nth-child(4) { animation-delay: 0.29s; }
.dash-stack > :nth-child(5) { animation-delay: 0.36s; }

.bar-fill {
  animation: bar-grow 0.9s var(--ease) 0.25s both;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}

/* Узкий экран: сайдбар становится полосой сверху (как в app.css) */

@media (max-width: 992px) {
  .sidebar {
    width: auto;
    margin: 0;
    position: static;
    height: auto;
    max-height: none;
  }

  .sidebar .brand {
    border-bottom: none;
    margin: 0;
    padding: 0;
  }

  .sidebar-nav a:hover {
    transform: none;
  }
}

/* Содержимое на всю ширину и прижато к меню */
.content {
  max-width: none;
  margin: 0;
  padding: 8px 32px 48px;
}

/* Воронка: длинные названия этапов не налезают на полосу */
.bar-row:has(.stage-label) {
  grid-template-columns: minmax(140px, 260px) 1fr 60px;
}

.stage-label {
  white-space: normal;
  line-height: 1.3;
}

/* Автозаполнение браузера не красит поля в белый */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus {
  -webkit-text-fill-color: var(--heading);
  -webkit-box-shadow: 0 0 0 1000px var(--surface) inset;
  caret-color: var(--heading);
  transition: background-color 9999s;
}

/* Второстепенная кнопка (без btn-primary): контурная, в тон тёмной теме */
.btn:not(.btn-primary) {
  background: transparent;
  border-color: #2b3553;
  color: var(--heading);
}

.btn:not(.btn-primary):hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* Кнопка на одной линии с полями фильтра */
.filter-bar .btn {
  margin-top: 0;
}

/* 7 плашек: первые 4 — по 3 колонки из 12, последние 3 — по 4 */
.dash-stack > .card-grid {
  grid-template-columns: repeat(12, minmax(0, 1fr));
}

/* 6 плашек — по 3 в ряд */
.dash-stack > .card-grid > .kpi {
  grid-column: span 4;
}

@media (max-width: 1100px) {
  .dash-stack > .card-grid > .kpi:nth-child(n) {
    grid-column: span 6;
  }
}

@media (max-width: 600px) {
  .dash-stack > .card-grid > .kpi:nth-child(n) {
    grid-column: span 12;
  }
}


/* Контакты по организациям: строка организации — заголовок группы */
.contacts-org td {
  background: rgba(29, 140, 248, 0.07);
  font-weight: 600;
  color: var(--heading);
}

.contacts-org .badge {
  margin-left: 8px;
}

.contacts-manager {
  margin-left: 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
}

.contacts-empty {
  color: var(--warning);
  font-style: italic;
}

/* Карты (Leaflet + OpenStreetMap): тёмная подложка фильтром, как в Black Dashboard */
.geo-map {
  height: 280px;
  margin-top: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: #191b2a;
}

.geo-map-small {
  height: 200px;
}

.map-tiles {
  filter: invert(1) hue-rotate(200deg) brightness(0.85) contrast(0.9) saturate(0.6);
}

.leaflet-container {
  font-family: inherit;
}

.leaflet-control-attribution {
  background: rgba(30, 30, 47, 0.8) !important;
  color: var(--muted) !important;
}

.leaflet-control-attribution a {
  color: var(--accent) !important;
}

.geo-locate {
  margin-top: 10px;
}

/* Страница «Карта» */
.clients-map {
  height: calc(100vh - 260px);
  min-height: 420px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: #191b2a;
}

.map-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  margin-bottom: 12px;
  font-size: 13px;
}

.map-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.map-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1.5px solid #fff;
}

.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.map-popup {
  display: grid;
  gap: 4px;
  font-size: 13px;
}

.map-popup strong {
  color: var(--heading);
  font-size: 14px;
}

.map-popup-tag {
  color: #ff8d72;
  font-size: 12px;
}

.map-popup-link {
  margin-top: 4px;
}

.map-prospect-form {
  display: grid;
  gap: 6px;
  min-width: 220px;
}

.map-prospect-form .btn {
  margin-top: 4px;
}

/* Выбор цвета менеджера в списке пользователей */
.color-input {
  width: 36px;
  height: 26px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
}

/* ======================================================================
 * Телефон и планшет
 * ====================================================================== */

/* На компьютере мобильных элементов нет */
.mobile-bar,
.menu-overlay,
.sidebar-user {
  display: none;
}

@media (max-width: 992px) {
  .layout {
    flex-direction: column;
  }

  /* Узкая верхняя строка: кнопка меню + логотип */
  .mobile-bar {
    position: sticky;
    top: 0;
    z-index: 900;
    display: flex;
    align-items: center;
    gap: 12px;
    height: 56px;
    padding: 0 12px;
    background: linear-gradient(90deg, #3358f4, #1d8cf8);
    box-shadow: 0 4px 16px -6px rgba(29, 140, 248, 0.6);
  }

  .mobile-bar .brand {
    display: flex;
    align-items: center;
    gap: 8px;
    color: #fff;
    font-weight: 700;
    font-size: 18px;
  }

  .mobile-bar .brand:hover {
    text-decoration: none;
  }

  .mobile-bar .brand-mark {
    width: 28px;
    height: 28px;
    font-size: 13px;
  }

  .menu-toggle {
    display: grid;
    align-content: center;
    gap: 5px;
    width: 44px;
    height: 44px;
    padding: 0 10px;
    border: none;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.12);
    cursor: pointer;
  }

  .menu-toggle span {
    display: block;
    height: 2px;
    border-radius: 2px;
    background: #fff;
    transition: transform 0.25s var(--ease), opacity 0.2s var(--ease);
  }

  .menu-open .menu-toggle span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
  .menu-open .menu-toggle span:nth-child(2) { opacity: 0; }
  .menu-open .menu-toggle span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

  /* Меню выезжает слева поверх страницы */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    width: 280px;
    max-width: 85vw;
    height: 100vh;
    max-height: 100vh;
    margin: 0;
    padding: 20px 14px;
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: stretch;
    gap: 4px;
    overflow-y: auto;
    transform: translateX(-105%);
    transition: transform 0.3s var(--ease);
  }

  .menu-open .sidebar {
    transform: none;
  }

  .sidebar .brand {
    margin: 0 6px 12px;
    padding: 4px 4px 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.25);
  }

  .sidebar-nav {
    flex-direction: column;
    flex-wrap: nowrap;
    gap: 2px;
  }

  .sidebar-nav a,
  .sidebar-nav .nav-disabled {
    padding: 12px;
    font-size: 15px;
  }

  .sidebar-nav a:hover {
    transform: none;
  }

  .menu-overlay {
    position: fixed;
    inset: 0;
    z-index: 950;
    display: block;
    background: rgba(10, 12, 24, 0.6);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s var(--ease);
  }

  .menu-open .menu-overlay {
    opacity: 1;
    pointer-events: auto;
  }

  .menu-open {
    overflow: hidden;
  }

  .sidebar-user {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin-top: auto;
    padding: 16px 6px 4px;
    border-top: 1px solid rgba(255, 255, 255, 0.25);
    color: #fff;
    font-size: 14px;
  }

  .sidebar-user .btn-link {
    color: #fff;
    text-decoration: underline;
  }

  /* Шапка с пользователем — только на компьютере */
  .topbar {
    display: none;
  }

  .content {
    padding: 16px 16px 40px;
  }

  /* Плашки дашборда — по две в ряд */
  .dash-stack > .card-grid {
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }

  .dash-stack > .card-grid > .kpi:nth-child(n),
  .dash-stack > .card-grid > .kpi:last-child {
    grid-column: auto;
    min-width: 0;
  }

  .clients-map {
    height: 65vh;
    min-height: 320px;
  }
}

@media (max-width: 768px) {
  h1 {
    font-size: 21px;
  }

  .card {
    padding: 16px;
  }

  /* Поля ввода 16px — иначе iPhone приближает страницу при вводе */
  input[type="text"],
  input[type="password"],
  input[type="email"],
  input[type="date"],
  input[type="number"],
  select,
  textarea {
    font-size: 16px;
    min-height: 44px;
  }

  .form-narrow,
  .detail-card {
    max-width: none;
    padding: 16px;
  }

  .form-narrow .btn,
  .geo-locate {
    width: 100%;
    min-height: 46px;
  }

  .page-head {
    align-items: flex-start;
  }

  .page-head .btn {
    margin-top: 0;
  }

  /* Кнопки в заголовке раздела переносятся, а не уезжают за край */
  .page-head .actions {
    flex-wrap: wrap;
    white-space: normal;
  }

  .filter-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-bar input[type="text"],
  .filter-bar select,
  .filter-bar .btn {
    width: 100%;
    min-width: 0;
  }

  /* Таблицы → карточки: строка — карточка, ячейка — «подпись: значение» */
  .table-wrap {
    overflow: visible;
    background: transparent;
    border: none;
    box-shadow: none;
  }

  .table-wrap table,
  .table-wrap tbody,
  .table-wrap tr,
  .table-wrap td {
    display: block;
    width: 100%;
  }

  .table-wrap thead {
    display: none;
  }

  .table-wrap tbody tr {
    margin-bottom: 12px;
    padding: 8px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow-sm);
  }

  .table-wrap td {
    display: flex;
    gap: 12px;
    justify-content: space-between;
    align-items: center;
    padding: 9px 0;
    border-bottom: 1px solid var(--border);
    white-space: normal;
    text-align: right;
    word-break: break-word;
  }

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

  .table-wrap td[data-label]::before {
    content: attr(data-label);
    flex: 0 0 auto;
    max-width: 45%;
    color: var(--muted);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    text-align: left;
  }

  /* Первая ячейка (обычно название) — заголовок карточки */
  .table-wrap td:first-child {
    justify-content: flex-start;
    text-align: left;
    font-size: 15px;
    font-weight: 600;
    color: var(--heading);
  }

  .table-wrap td:first-child::before {
    display: none;
  }

  .table-wrap td:empty,
  .table-wrap td.actions:empty {
    display: none;
  }

  .table-wrap td.actions {
    justify-content: flex-end;
    flex-wrap: wrap;
  }

  .table-wrap td select,
  .table-wrap td .list-inline-select {
    max-width: 60%;
  }

  /* Быстрая задача в строке организации — на всю ширину карточки */
  .table-wrap td .quick-task-form {
    width: 100%;
  }

  .table-wrap td:has(.quick-task-form) {
    flex-direction: column;
    align-items: stretch;
  }

  .table-wrap td:has(.quick-task-form)::before {
    max-width: none;
  }

  .quick-task-submit {
    min-height: 40px;
  }

  /* Контакты: группа организации — одна карточка */
  .contacts-table tbody.contacts-group {
    margin-bottom: 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    overflow: hidden;
  }

  .contacts-table tbody.contacts-group tr {
    margin: 0;
    border: none;
    border-radius: 0;
    box-shadow: none;
    border-top: 1px solid var(--border);
  }

  .contacts-table tbody.contacts-group tr {
    padding: 2px 14px;
  }

  .contacts-table .contacts-org {
    border-top: none;
    background: rgba(29, 140, 248, 0.1);
  }

  .contacts-table .contacts-org td {
    background: transparent;
    border-bottom: none;
  }

  .contacts-table .contacts-org td.actions {
    padding-top: 0;
  }

  .table-wrap td.contacts-empty {
    font-size: 13px;
    font-weight: 500;
    color: var(--warning);
  }

  .table-wrap td .quick-task-form select {
    max-width: none;
  }

  /* Графики на дашборде */
  .bar-row {
    grid-template-columns: 64px 1fr 76px;
    gap: 8px;
  }

  .bar-row:has(.stage-label) {
    grid-template-columns: 110px 1fr 32px;
  }

  .geo-map {
    height: 260px;
  }
}

@media (max-width: 600px) {
  .content {
    padding: 12px 12px 32px;
  }

  /* Плашки дашборда — по одной в ряд */
  .dash-stack > .card-grid {
    grid-template-columns: 1fr;
  }

  .kpi-value {
    font-size: 22px;
  }
}
