/* Клиенты и Абонементы: переключатель «люди / оплаты», сворачиваемый срез с графиками,
   яркая сводка, полоска остатка занятий и анимации строк. */

body.crm-ui {
    --cp-a: #2563eb;
    --cp-b: #7c3aed;
    --cp-sales-a: #f97316;
    --cp-sales-b: #e11d48;
    --cp-ease: cubic-bezier(0.22, 1, 0.36, 1);
    --cp-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* --- Переключатель --- */
.crm-switch {
    position: relative;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    margin: 0 0 6px;
    padding: 6px;
    border-radius: 20px;
    background: var(--surface, #fff);
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 10px 26px -16px rgba(15, 23, 42, 0.25);
    animation: cp-rise 0.45s var(--cp-ease) both;
}

.crm-switch__tab {
    position: relative;
    z-index: 1;
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
    padding: 10px 14px;
    border-radius: 15px;
    color: var(--crm-ink, #111);
    text-decoration: none;
    transition: color 0.25s ease, background-color 0.25s ease, transform 0.25s var(--cp-ease);
}

.crm-switch__tab:hover {
    text-decoration: none;
    background: rgba(15, 23, 42, 0.035);
}

.crm-switch__tab.is-on {
    color: #fff;
    background: transparent;
}

.crm-switch__glider {
    position: absolute;
    top: 6px;
    bottom: 6px;
    left: 6px;
    width: calc(50% - 9px);
    border-radius: 15px;
    background: linear-gradient(135deg, var(--cp-a), var(--cp-b));
    box-shadow: 0 12px 26px -12px rgba(91, 33, 182, 0.85);
    transition: transform 0.45s var(--cp-spring), background 0.3s ease, box-shadow 0.3s ease;
    animation: cp-glider-in 0.6s var(--cp-spring) both;
}

.crm-switch:has(.is-sales.is-on) .crm-switch__glider {
    transform: translateX(calc(100% + 6px));
    background: linear-gradient(135deg, var(--cp-sales-a), var(--cp-sales-b));
    box-shadow: 0 12px 26px -12px rgba(225, 29, 72, 0.8);
}

.crm-switch__icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    border-radius: 12px;
    transition: background-color 0.25s ease, color 0.25s ease, transform 0.4s var(--cp-spring);
}

.crm-switch__icon svg {
    width: 22px;
    height: 22px;
}

.crm-switch__tab.is-clients .crm-switch__icon {
    background: rgba(37, 99, 235, 0.1);
    color: var(--cp-a);
}

.crm-switch__tab.is-sales .crm-switch__icon {
    background: rgba(249, 115, 22, 0.12);
    color: #ea580c;
}

.crm-switch__tab.is-on .crm-switch__icon {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}

.crm-switch__tab:hover .crm-switch__icon {
    transform: rotate(-8deg) scale(1.08);
}

.crm-switch__text {
    min-width: 0;
}

.crm-switch__text strong {
    display: block;
    font-size: 16px;
    font-weight: 800;
    letter-spacing: -0.01em;
}

.crm-switch__text small {
    display: block;
    margin-top: 1px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 12.5px;
    opacity: 0.72;
}

.crm-switch__lead {
    margin: 0 4px 14px;
    color: var(--crm-muted, rgba(0, 0, 0, 0.5));
    font-size: 13px;
    line-height: 1.45;
    animation: cp-rise 0.45s 0.08s var(--cp-ease) both;
}

@keyframes cp-glider-in {
    from { opacity: 0; transform: scale(0.9); }
}

.crm-switch:has(.is-sales.is-on) .crm-switch__glider {
    animation-name: cp-glider-in-right;
}

@keyframes cp-glider-in-right {
    from { opacity: 0; transform: translateX(calc(100% + 6px)) scale(0.9); }
    to { transform: translateX(calc(100% + 6px)); }
}

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

/* --- Срез и графики: свёрнуты, пока не нужен фильтр --- */
.clients-insight[data-insight] {
    padding: 0;
}

.clients-insight__toggle {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 12px 16px;
    border: 0;
    background: transparent;
    color: inherit;
    font: inherit;
    text-align: left;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.clients-insight__toggle:hover {
    background: rgba(37, 99, 235, 0.04);
}

.clients-insight__toggle-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    width: 36px;
    height: 36px;
    border-radius: 11px;
    background: linear-gradient(135deg, var(--cp-a), var(--cp-b));
    color: #fff;
    box-shadow: 0 8px 18px -8px rgba(91, 33, 182, 0.8);
}

.clients-insight__toggle-icon svg {
    width: 18px;
    height: 18px;
}

.clients-insight__toggle-icon path {
    transform-origin: 50% 100%;
    transform-box: fill-box;
    animation: cp-bars 2.6s ease-in-out infinite;
}

.clients-insight__toggle-icon path {
    animation-play-state: paused;
}

.clients-insight__toggle:hover .clients-insight__toggle-icon path {
    animation-play-state: running;
}

@keyframes cp-bars {
    0%, 100% { transform: scaleY(1); }
    50% { transform: scaleY(0.6); }
}

.clients-insight__toggle-text {
    flex: 1 1 auto;
    min-width: 0;
}

.clients-insight__toggle-text strong {
    display: block;
    font-size: 14px;
    font-weight: 800;
}

.clients-insight__toggle-text small {
    display: block;
    color: var(--crm-muted, rgba(0, 0, 0, 0.5));
    font-size: 12.5px;
}

.clients-insight__toggle-money {
    flex: 0 0 auto;
    padding: 6px 12px;
    border-radius: 999px;
    background: rgba(22, 163, 74, 0.1);
    color: #15803d;
    font-size: 13px;
    font-weight: 700;
    white-space: nowrap;
}

.clients-insight__toggle-chev {
    flex: 0 0 auto;
    width: 9px;
    height: 9px;
    margin: 0 4px 4px 4px;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    color: rgba(0, 0, 0, 0.4);
    transform: rotate(45deg);
    transition: transform 0.3s var(--cp-ease), margin 0.3s ease;
}

.clients-insight:not(.is-collapsed) .clients-insight__toggle-chev {
    margin-top: 6px;
    margin-bottom: 0;
    transform: rotate(-135deg);
}

.clients-insight__body {
    display: grid;
    grid-template-rows: 1fr;
    transition: grid-template-rows 0.45s var(--cp-ease);
}

.clients-insight.is-collapsed .clients-insight__body {
    grid-template-rows: 0fr;
}

.clients-insight__inner {
    min-height: 0;
    overflow: hidden;
    padding: 0 14px 16px;
    transition: opacity 0.3s ease, padding 0.3s ease;
}

.clients-insight.is-collapsed .clients-insight__inner {
    padding-bottom: 0;
    opacity: 0;
}

.clients-insight:not(.is-collapsed) .clients-insight__card {
    animation: cp-rise 0.5s var(--cp-ease) both;
}

.clients-insight:not(.is-collapsed) .clients-insight__card + .clients-insight__card {
    animation-delay: 0.08s;
}

/* --- Сводка абонементов: цветные карточки --- */
.crm-board__stats {
    gap: 8px;
    padding: 12px;
}

.crm-board__stat {
    height: auto;
    min-height: 34px;
    padding: 6px 12px;
    border: 0;
    border-radius: 12px;
    background: #f5f7fb;
    font-size: 12.5px;
    animation: cp-rise 0.45s var(--cp-ease) both;
    transition: transform 0.25s var(--cp-spring), box-shadow 0.25s ease, background-color 0.2s ease;
}

.crm-board__stat:nth-child(2) { animation-delay: 0.03s; }
.crm-board__stat:nth-child(3) { animation-delay: 0.06s; }
.crm-board__stat:nth-child(4) { animation-delay: 0.09s; }
.crm-board__stat:nth-child(5) { animation-delay: 0.12s; }
.crm-board__stat:nth-child(6) { animation-delay: 0.15s; }
.crm-board__stat:nth-child(7) { animation-delay: 0.18s; }

.crm-board__stat b {
    font-size: 15px;
    font-weight: 800;
}

.crm-board__stat.is-money {
    background: linear-gradient(135deg, rgba(34, 197, 94, 0.14), rgba(16, 185, 129, 0.08));
    color: #166534;
}

.crm-board__stat.is-money b { color: #15803d; }

.crm-board__stat.is-debt {
    background: rgba(239, 68, 68, 0.1);
    color: #991b1b;
}

.crm-board__stat.is-renew,
.crm-board__stat.is-ending {
    background: rgba(249, 115, 22, 0.1);
    color: #9a3412;
}

.crm-board__stat.is-pending {
    background: rgba(234, 179, 8, 0.14);
    color: #854d0e;
}

.crm-board__stat.is-link:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 18px -12px rgba(15, 23, 42, 0.45);
}

/* --- Полоска остатка занятий --- */
.crm-left-bar {
    display: block;
    width: 46px;
    height: 5px;
    margin: 5px 0 0 auto;
    border-radius: 999px;
    background: #e8ecf3;
    overflow: hidden;
}

.crm-left-bar b {
    display: block;
    width: var(--w, 0%);
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, #22c55e, #16a34a);
    transform-origin: 0 50%;
    animation: cp-bar-grow 0.8s 0.2s var(--cp-ease) both;
}

.crm-left-bar.is-low b {
    background: linear-gradient(90deg, #fb923c, #f97316);
}

.crm-left-bar.is-out b {
    background: #ef4444;
}

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

/* --- Фильтры-вкладки и строки таблиц --- */
.clients-sheet .clients-filter {
    transition: transform 0.25s var(--cp-spring), background-color 0.2s ease, color 0.2s ease, box-shadow 0.25s ease;
}

.clients-sheet .clients-filter:hover {
    transform: translateY(-1px);
}

body.crm-ui .clients-sheet .clients-filter.is-active {
    border-color: transparent;
    background: linear-gradient(135deg, var(--cp-a), var(--cp-b));
    box-shadow: 0 8px 18px -10px rgba(91, 33, 182, 0.8);
}

.crm-board .clients-filter.is-active {
    background: linear-gradient(135deg, var(--cp-sales-a), var(--cp-sales-b)) !important;
    box-shadow: 0 8px 18px -10px rgba(225, 29, 72, 0.7) !important;
}

.clients-sheet .clients-table tbody tr:not([hidden]) {
    animation: cp-row-in 0.45s var(--cp-ease) both;
}

.clients-sheet .clients-table tbody tr:nth-child(1) { animation-delay: 0.04s; }
.clients-sheet .clients-table tbody tr:nth-child(2) { animation-delay: 0.07s; }
.clients-sheet .clients-table tbody tr:nth-child(3) { animation-delay: 0.1s; }
.clients-sheet .clients-table tbody tr:nth-child(4) { animation-delay: 0.13s; }
.clients-sheet .clients-table tbody tr:nth-child(5) { animation-delay: 0.16s; }
.clients-sheet .clients-table tbody tr:nth-child(6) { animation-delay: 0.19s; }
.clients-sheet .clients-table tbody tr:nth-child(7) { animation-delay: 0.22s; }
.clients-sheet .clients-table tbody tr:nth-child(8) { animation-delay: 0.25s; }
.clients-sheet .clients-table tbody tr:nth-child(n+9) { animation-delay: 0.28s; }

@keyframes cp-row-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}

.clients-sheet .clients-table tbody tr {
    transition: background-color 0.2s ease, box-shadow 0.2s ease;
}

.clients-sheet .clients-table tbody tr:hover {
    box-shadow: inset 3px 0 0 var(--cp-a);
}

.crm-board .clients-table tbody tr:hover {
    box-shadow: inset 3px 0 0 var(--cp-sales-a);
}

.clients-sheet .clients-table__name {
    transition: color 0.2s ease;
}

/* --- Тёмная тема --- */
html[data-theme="dark"] .crm-switch__tab:hover {
    background: rgba(255, 255, 255, 0.05);
}

html[data-theme="dark"] .crm-board__stat {
    background: #1a2230;
}

html[data-theme="dark"] .crm-board__stat.is-money { background: rgba(34, 197, 94, 0.14); color: #86efac; }
html[data-theme="dark"] .crm-board__stat.is-money b { color: #4ade80; }
html[data-theme="dark"] .crm-board__stat.is-debt { background: rgba(239, 68, 68, 0.16); color: #fca5a5; }
html[data-theme="dark"] .crm-board__stat.is-renew,
html[data-theme="dark"] .crm-board__stat.is-ending { background: rgba(249, 115, 22, 0.16); color: #fdba74; }

html[data-theme="dark"] .crm-left-bar {
    background: #243044;
}

html[data-theme="dark"] .clients-insight__toggle-money {
    background: rgba(34, 197, 94, 0.14);
    color: #4ade80;
}

html[data-theme="dark"] .clients-insight__toggle-chev {
    color: rgba(255, 255, 255, 0.45);
}

/* --- Телефон --- */
@media (max-width: 720px) {
    .crm-switch {
        gap: 4px;
        padding: 4px;
        border-radius: 16px;
    }

    .crm-switch__glider {
        top: 4px;
        bottom: 4px;
        left: 4px;
        width: calc(50% - 6px);
        border-radius: 12px;
    }

    .crm-switch:has(.is-sales.is-on) .crm-switch__glider {
        transform: translateX(calc(100% + 4px));
    }

    .crm-switch__tab {
        gap: 8px;
        padding: 8px 10px;
        border-radius: 12px;
    }

    .crm-switch__icon {
        width: 32px;
        height: 32px;
        border-radius: 10px;
    }

    .crm-switch__text strong {
        font-size: 14px;
    }

    .crm-switch__text small {
        display: none;
    }

    .clients-insight__toggle-money {
        display: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    .crm-switch,
    .crm-switch__lead,
    .crm-switch__glider,
    .crm-board__stat,
    .crm-left-bar b,
    .clients-sheet .clients-table tbody tr,
    .clients-insight__card {
        animation: none !important;
    }

    .clients-insight__body,
    .crm-switch__glider {
        transition: none !important;
    }
}

/* ── Поиск клиентов: фильтры-селекты в ряд с полем, лупа внутри поля ── */
.clients-sheet .clients-search {
    display: flex;
    align-items: center;
    gap: 6px;
    width: auto;
    min-width: 0;
    flex-wrap: wrap;
    justify-content: flex-end;
}
.clients-search__field {
    position: relative;
    display: block;
    flex: 1 1 200px;
    min-width: 180px;
}
.clients-sheet .clients-search .clients-dict-filter {
    height: 32px;
    padding: 0 10px;
    border-radius: 999px;
    font-size: 13px;
    flex: 0 0 auto;
}
.clients-sheet > .crm-switch { margin: 8px 8px 0; }
.clients-sheet > .crm-switch__lead { margin: 10px 16px 6px; }
.clients-insight:not(.is-collapsed) .clients-insight__toggle-money { display: none; }
@media (max-width: 640px) {
    .clients-sheet .clients-search { width: 100%; justify-content: stretch; }
    .clients-sheet .clients-search .clients-dict-filter { flex: 1 1 0; max-width: none; }
    .clients-search__field { flex-basis: 100%; }
}
@media (max-width: 640px) {
    .clients-insight__periods { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; max-width: 100%; }
    .clients-insight__periods::-webkit-scrollbar { display: none; }
    .clients-insight__periods > * { flex: 0 0 auto; }
}
