/* Боковое меню CRM: градиентный активный пункт, анимации, компактная «рейка» с иконками.
   Подключается после crm-theme.css. Рейка: html.sb-rail (кнопка) или автоматически на 901–1199px. */

body.crm-ui {
    --sidebar-w: 236px;
    --sb-rail-w: 72px;
    --sb-open-w: 248px;
    --sb-a: #2563eb;
    --sb-b: #7c3aed;
    --sb-grad: linear-gradient(135deg, var(--sb-a) 0%, var(--sb-b) 100%);
    --sb-ease: cubic-bezier(0.22, 1, 0.36, 1);
    --sb-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
    --sb-tile: rgba(37, 99, 235, 0.08);
}

body.crm-ui .sidebar {
    padding: 12px 10px;
    overflow: hidden;
    background:
        radial-gradient(120% 40% at 0% 0%, rgba(124, 58, 237, 0.07), transparent 60%),
        radial-gradient(120% 40% at 100% 100%, rgba(37, 99, 235, 0.07), transparent 60%),
        var(--crm-sidebar);
    transition: width 0.32s var(--sb-ease), box-shadow 0.32s ease, transform 0.36s var(--sb-ease);
}

/* --- Шапка --- */
body.crm-ui .sidebar-brand {
    position: relative;
    gap: 10px;
    padding: 6px 8px 12px;
    margin-bottom: 8px;
}

body.crm-ui .sidebar-brand > div {
    min-width: 0;
    white-space: nowrap;
}

body.crm-ui .sidebar-brand .qozgal-logo--sidebar,
body.crm-ui .sidebar-brand .brand-logo {
    transition: transform 0.4s var(--sb-spring);
}

body.crm-ui .sidebar-brand:hover .qozgal-logo--sidebar,
body.crm-ui .sidebar-brand:hover .brand-logo {
    transform: rotate(-6deg) scale(1.06);
}

.sb-pin {
    position: absolute;
    right: 2px;
    top: 50%;
    width: 28px;
    height: 28px;
    margin-top: -18px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 0;
    border-radius: 9px;
    background: transparent;
    color: var(--crm-muted);
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s ease, background-color 0.2s ease, color 0.2s ease, transform 0.3s var(--sb-ease);
}

.sb-pin svg {
    width: 16px;
    height: 16px;
}

body.crm-ui .sidebar:hover .sb-pin,
.sb-pin:focus-visible {
    opacity: 1;
}

.sb-pin:hover {
    background: var(--sb-tile);
    color: var(--sb-a);
}

html.sb-rail .sb-pin {
    transform: rotate(180deg);
}

/* --- Карточка клуба --- */
body.crm-ui .sidebar-studio {
    margin: 0 0 10px;
    padding: 10px 12px 12px;
    border: 0;
    border-radius: 14px;
    background: var(--surface, #fff);
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 6px 16px -10px rgba(15, 23, 42, 0.18);
}

body.crm-ui .sidebar-studio__name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

body.crm-ui .sidebar-studio__plan {
    margin-top: 6px;
    padding: 3px 9px;
    border-radius: 999px;
    background: rgba(124, 58, 237, 0.1);
    color: #6d28d9;
    transition: background-color 0.2s ease;
}

body.crm-ui .sidebar-studio__plan:hover {
    background: rgba(124, 58, 237, 0.18);
}

body.crm-ui .branch-switch {
    margin-top: 8px;
}

body.crm-ui .branch-switch__select {
    padding: 7px 10px;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

/* --- Пункты меню --- */
body.crm-ui .sidebar-nav {
    gap: 2px;
    margin: 0 -4px;
    padding: 0 4px 6px;
    scrollbar-width: thin;
}

body.crm-ui .nav-item,
body.crm-ui .nav-group-toggle {
    position: relative;
    gap: 10px;
    min-height: 38px;
    padding: 5px 10px 5px 6px;
    border-radius: 12px;
    white-space: nowrap;
    transition: background-color 0.2s ease, color 0.2s ease, transform 0.25s var(--sb-ease), box-shadow 0.25s ease;
}

body.crm-ui .nav-item > span,
body.crm-ui .nav-group-title {
    overflow: hidden;
    text-overflow: ellipsis;
    transition: opacity 0.2s ease;
}

body.crm-ui .nav-item .nav-icon-svg,
body.crm-ui .nav-group-toggle .nav-icon-svg {
    box-sizing: content-box;
    width: 16px;
    height: 16px;
    padding: 6px;
    border-radius: 9px;
    background: var(--sb-tile);
    color: var(--sb-a);
    transition: background-color 0.2s ease, color 0.2s ease, transform 0.35s var(--sb-spring);
}

body.crm-ui .nav-item:hover,
body.crm-ui .nav-group-toggle:hover {
    background: rgba(37, 99, 235, 0.07);
    color: var(--sb-a);
    transform: translateX(2px);
}

body.crm-ui .nav-item:hover .nav-icon-svg,
body.crm-ui .nav-group-toggle:hover .nav-icon-svg {
    transform: scale(1.1) rotate(-6deg);
    background: rgba(37, 99, 235, 0.14);
}

body.crm-ui .nav-item:active {
    transform: scale(0.98);
}

body.crm-ui .nav-item.active {
    background: var(--sb-grad);
    color: #fff;
    box-shadow: 0 10px 22px -12px rgba(91, 33, 182, 0.9);
    transform: none;
}

body.crm-ui .nav-item.active .nav-icon-svg {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}

body.crm-ui .nav-item.active::before {
    content: "";
    position: absolute;
    left: -4px;
    top: 50%;
    width: 3px;
    height: 20px;
    margin-top: -10px;
    border-radius: 0 4px 4px 0;
    background: var(--sb-grad);
    animation: sb-bar-in 0.5s var(--sb-spring) both;
}

body.crm-ui .nav-item.active::after {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background: linear-gradient(110deg, transparent 30%, rgba(255, 255, 255, 0.28) 50%, transparent 70%);
    transform: translateX(-110%);
    animation: sb-shine 1.2s 0.25s var(--sb-ease) both;
    pointer-events: none;
}

@keyframes sb-bar-in {
    from { transform: scaleY(0); }
    to { transform: scaleY(1); }
}

@keyframes sb-shine {
    to { transform: translateX(110%); }
}

body.crm-ui .crm-nav-divider {
    height: 1px;
    margin: 8px 10px;
    background: linear-gradient(90deg, transparent, var(--crm-line), transparent);
}

/* Группы */
body.crm-ui .nav-group {
    margin-top: 2px;
}

body.crm-ui .nav-group.has-active > .nav-group-toggle {
    color: var(--sb-b);
}

body.crm-ui .nav-chevron {
    border-radius: 7px;
    transition: background-color 0.2s ease;
}

body.crm-ui .nav-submenu {
    margin: 0 4px;
}

body.crm-ui .nav-group.is-open .nav-submenu {
    animation: sb-sub-in 0.32s var(--sb-ease);
}

@keyframes sb-sub-in {
    from { opacity: 0; transform: translateY(-6px); clip-path: inset(0 0 100% 0); }
    to { opacity: 1; transform: none; clip-path: inset(0 0 0 0); }
}

body.crm-ui .nav-submenu-inner {
    gap: 1px;
    padding: 4px 0 6px 12px;
    margin-left: 18px;
    border-left: 2px solid rgba(124, 58, 237, 0.14);
}

body.crm-ui .nav-subitem {
    position: relative;
    padding: 6px 10px;
    border-radius: 9px;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: background-color 0.2s ease, color 0.2s ease, transform 0.25s var(--sb-ease);
}

body.crm-ui .nav-subitem:hover {
    transform: translateX(2px);
}

body.crm-ui .nav-subitem.active {
    background: rgba(124, 58, 237, 0.12);
    color: #6d28d9;
    font-weight: 700;
}

body.crm-ui .nav-subitem.active::before {
    content: "";
    position: absolute;
    left: -17px;
    top: 50%;
    width: 8px;
    height: 8px;
    margin-top: -4px;
    border-radius: 50%;
    background: var(--sb-grad);
    box-shadow: 0 0 0 3px var(--crm-sidebar);
}

/* --- Низ --- */
body.crm-ui .sidebar-platform {
    border: 0;
    border-radius: 12px;
    white-space: nowrap;
    overflow: hidden;
}

body.crm-ui .sidebar-footer {
    padding-top: 10px;
}

body.crm-ui .user-info {
    margin-bottom: 8px;
    white-space: nowrap;
}

body.crm-ui .user-avatar {
    flex: 0 0 auto;
    background: var(--sb-grad);
    box-shadow: 0 6px 14px -6px rgba(91, 33, 182, 0.8);
}

body.crm-ui .user-info > div:last-child {
    min-width: 0;
    overflow: hidden;
}

body.crm-ui .user-name {
    overflow: hidden;
    text-overflow: ellipsis;
}

body.crm-ui .logout-btn {
    border-radius: 12px;
    transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}

body.crm-ui .logout-btn:hover {
    background: rgba(239, 68, 68, 0.08);
    border-color: rgba(239, 68, 68, 0.3);
    color: #dc2626;
}

/* --- Появление пунктов (один раз за сессию) --- */
.sidebar.sb-intro .sidebar-brand,
.sidebar.sb-intro .sidebar-studio,
.sidebar.sb-intro .sidebar-nav > *,
.sidebar.sb-intro .sidebar-footer,
.sidebar.is-open .sidebar-nav > * {
    animation: sb-item-in 0.45s var(--sb-ease) both;
    animation-delay: calc(var(--sb-i, 0) * 22ms);
}

@keyframes sb-item-in {
    from { opacity: 0; transform: translateX(-12px); }
    to { opacity: 1; transform: none; }
}

/* --- Низкие экраны: плотнее --- */
@media (min-width: 901px) and (max-height: 820px) {
    body.crm-ui .sidebar-brand {
        padding-bottom: 8px;
        margin-bottom: 6px;
    }

    body.crm-ui .sidebar-studio {
        padding: 8px 10px;
        margin-bottom: 6px;
    }

    body.crm-ui .sidebar-studio__plan {
        margin-top: 4px;
    }

    body.crm-ui .branch-switch {
        margin-top: 6px;
    }

    body.crm-ui .branch-switch__label {
        display: none;
    }

    body.crm-ui .branch-switch__select {
        padding: 5px 8px;
    }

    body.crm-ui .wake-toggle {
        margin-top: 6px;
    }

    body.crm-ui .nav-item,
    body.crm-ui .nav-group-toggle {
        min-height: 32px;
        padding-top: 3px;
        padding-bottom: 3px;
        font-size: 13.5px;
    }

    body.crm-ui .nav-item .nav-icon-svg,
    body.crm-ui .nav-group-toggle .nav-icon-svg {
        padding: 5px;
    }

    body.crm-ui .crm-nav-divider {
        margin: 5px 10px;
    }

    body.crm-ui .sidebar-platform {
        display: none;
    }

    body.crm-ui .user-info {
        margin-bottom: 6px;
    }
}

/* --- Рейка с иконками: по кнопке на больших экранах и всегда на 901–1199px --- */
@media (min-width: 901px) {
    html.sb-rail body.crm-ui {
        --sidebar-w: var(--sb-rail-w);
    }

    html.sb-rail body.crm-ui .main-content {
        transition: margin-left 0.32s var(--sb-ease);
    }
}

@media (min-width: 901px) and (max-width: 1199px) {
    body.crm-ui {
        --sidebar-w: var(--sb-rail-w);
    }

    .sb-pin {
        display: none;
    }
}

@media (min-width: 901px) {
    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .sidebar-studio,
    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .sidebar-platform,
    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .sidebar-brand > div,
    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .nav-submenu,
    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .nav-chevron,
    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .user-info > div:last-child,
    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .logout-btn,
    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .sb-pin,
    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .menu-order-toggle {
        display: none;
    }

    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .nav-item > span,
    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .nav-group-title {
        opacity: 0;
    }

    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .sidebar-brand {
        justify-content: center;
        padding-left: 0;
        padding-right: 0;
    }

    html.sb-rail body.crm-ui .sidebar:not(:hover):not(:focus-within) .user-info {
        justify-content: center;
        padding: 0;
    }

    html.sb-rail body.crm-ui .sidebar:hover,
    html.sb-rail body.crm-ui .sidebar:focus-within {
        width: var(--sb-open-w);
        z-index: 40;
        box-shadow: 24px 0 48px -16px rgba(15, 23, 42, 0.28);
    }
}

@media (min-width: 901px) and (max-width: 1199px) {
    body.crm-ui .sidebar:not(:hover):not(:focus-within) .sidebar-studio,
    body.crm-ui .sidebar:not(:hover):not(:focus-within) .sidebar-platform,
    body.crm-ui .sidebar:not(:hover):not(:focus-within) .sidebar-brand > div,
    body.crm-ui .sidebar:not(:hover):not(:focus-within) .nav-submenu,
    body.crm-ui .sidebar:not(:hover):not(:focus-within) .nav-chevron,
    body.crm-ui .sidebar:not(:hover):not(:focus-within) .user-info > div:last-child,
    body.crm-ui .sidebar:not(:hover):not(:focus-within) .logout-btn,
    body.crm-ui .sidebar:not(:hover):not(:focus-within) .menu-order-toggle {
        display: none;
    }

    body.crm-ui .sidebar:not(:hover):not(:focus-within) .nav-item > span,
    body.crm-ui .sidebar:not(:hover):not(:focus-within) .nav-group-title {
        opacity: 0;
    }

    body.crm-ui .sidebar:not(:hover):not(:focus-within) .sidebar-brand {
        justify-content: center;
        padding-left: 0;
        padding-right: 0;
    }

    body.crm-ui .sidebar:not(:hover):not(:focus-within) .user-info {
        justify-content: center;
        padding: 0;
    }

    body.crm-ui .sidebar:hover,
    body.crm-ui .sidebar:focus-within {
        width: var(--sb-open-w);
        z-index: 40;
        box-shadow: 24px 0 48px -16px rgba(15, 23, 42, 0.28);
    }
}

/* --- Телефон и планшет: выезжающая шторка --- */
@media (max-width: 900px) {
    .sb-pin {
        display: none;
    }

    body.crm-ui .sidebar {
        width: min(296px, 86vw);
        border-radius: 0 22px 22px 0;
        transition: transform 0.42s var(--sb-ease);
    }

    body.crm-ui .sidebar-backdrop {
        backdrop-filter: blur(4px);
        animation: sb-fade 0.3s ease both;
    }

    body.crm-ui .nav-item,
    body.crm-ui .nav-group-toggle {
        min-height: 44px;
    }

    body.crm-ui .sidebar-toggle {
        transition: transform 0.25s var(--sb-spring), border-color 0.2s ease;
    }

    body.crm-ui .sidebar-toggle:active {
        transform: scale(0.92);
    }

    body.crm-ui .sidebar-toggle span {
        transition: transform 0.3s var(--sb-ease), width 0.3s var(--sb-ease);
    }

    body.crm-ui .sidebar-toggle span:nth-child(2) {
        width: 11px;
        align-self: center;
        margin-left: -5px;
    }

    body.crm-ui .sidebar-toggle:hover span:nth-child(2) {
        width: 16px;
        margin-left: 0;
    }
}

@keyframes sb-fade {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* --- Тёмная тема --- */
html[data-theme="dark"] body.crm-ui {
    --sb-tile: rgba(96, 165, 250, 0.12);
}

html[data-theme="dark"] body.crm-ui .sidebar {
    background:
        radial-gradient(120% 40% at 0% 0%, rgba(124, 58, 237, 0.14), transparent 60%),
        radial-gradient(120% 40% at 100% 100%, rgba(37, 99, 235, 0.12), transparent 60%),
        var(--crm-sidebar);
}

html[data-theme="dark"] body.crm-ui .nav-item .nav-icon-svg,
html[data-theme="dark"] body.crm-ui .nav-group-toggle .nav-icon-svg {
    color: #93c5fd;
}

html[data-theme="dark"] body.crm-ui .nav-item.active .nav-icon-svg {
    color: #fff;
}

html[data-theme="dark"] body.crm-ui .nav-item:hover,
html[data-theme="dark"] body.crm-ui .nav-group-toggle:hover {
    color: #bfdbfe;
}

html[data-theme="dark"] body.crm-ui .nav-subitem.active,
html[data-theme="dark"] body.crm-ui .sidebar-studio__plan {
    background: rgba(167, 139, 250, 0.16);
    color: #c4b5fd;
}

html[data-theme="dark"] body.crm-ui .nav-group.has-active > .nav-group-toggle {
    color: #c4b5fd;
}

@media (prefers-reduced-motion: reduce) {
    body.crm-ui .sidebar,
    body.crm-ui .nav-item.active::before,
    body.crm-ui .nav-item.active::after,
    body.crm-ui .nav-group.is-open .nav-submenu,
    .sidebar.sb-intro *,
    .sidebar.is-open .sidebar-nav > * {
        animation: none !important;
        transition: none !important;
    }
}

/* ============================================================
   Всё меню прокручивается целиком: карточка клуба и низ не стоят на месте.
   Сверху липкий только логотип, снизу — компактная строка «аватар + выход».
   ============================================================ */
body.crm-ui .sidebar {
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    padding-top: 0;
    padding-bottom: 0;
}

body.crm-ui .sidebar::-webkit-scrollbar {
    width: 6px;
}

body.crm-ui .sidebar::-webkit-scrollbar-thumb {
    border-radius: 999px;
    background: rgba(15, 23, 42, 0.14);
}

body.crm-ui .sidebar-brand {
    position: sticky;
    top: 0;
    z-index: 3;
    flex: 0 0 auto;
    margin: 0 -10px 8px;
    padding: 12px 18px 10px;
    border-bottom: 0;
    background: var(--crm-sidebar);
    transition: box-shadow 0.2s ease;
}

body.crm-ui .sidebar.is-scrolled .sidebar-brand {
    box-shadow: 0 10px 18px -14px rgba(15, 23, 42, 0.35);
}

body.crm-ui .sidebar-studio {
    flex: 0 0 auto;
}

body.crm-ui .sidebar-nav {
    flex: 1 0 auto;
    overflow: visible;
}

body.crm-ui .sidebar-platform {
    flex: 0 0 auto;
    margin: 8px 8px 0;
    padding: 4px 6px;
    background: transparent;
    font-size: 11px;
    opacity: 0.75;
}

body.crm-ui .sidebar-footer {
    position: sticky;
    bottom: 0;
    z-index: 3;
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 6px -10px 0;
    padding: 8px 12px 10px 14px;
    border-top: 0;
    background: var(--crm-sidebar);
    box-shadow: 0 -10px 18px -14px rgba(15, 23, 42, 0.35);
}

body.crm-ui .sidebar-footer .user-info {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
    padding: 0;
}

body.crm-ui .sidebar-footer .user-avatar {
    width: 32px;
    height: 32px;
    font-size: 0.8rem;
}

body.crm-ui .sidebar-footer .user-name {
    font-size: 0.8rem;
}

body.crm-ui .sidebar-footer .user-role {
    font-size: 0.7rem;
}

body.crm-ui .sidebar-footer .logout-btn {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border-radius: 11px;
}

body.crm-ui .sidebar-footer .logout-btn span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

body.crm-ui .sidebar-footer .logout-btn::before {
    content: "";
    width: 18px;
    height: 18px;
    background: currentColor;
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23000' stroke-width='1.9' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M14 4h3a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2h-3'/%3E%3Cpath d='M10 16.5 5.5 12 10 7.5M5.5 12H15'/%3E%3C/svg%3E") center / contain no-repeat;
    mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23000' stroke-width='1.9' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M14 4h3a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2h-3'/%3E%3Cpath d='M10 16.5 5.5 12 10 7.5M5.5 12H15'/%3E%3C/svg%3E") center / contain no-repeat;
}

/* Кнопка демо «Оставить заявку» в низу остаётся полной ширины */
body.crm-ui .sidebar-footer .btn-block {
    width: 100%;
}

html[data-theme="dark"] body.crm-ui .sidebar-brand,
html[data-theme="dark"] body.crm-ui .sidebar-footer {
    background: var(--crm-sidebar);
}

html[data-theme="dark"] body.crm-ui .sidebar::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.16);
}

@media (max-width: 900px) {
    body.crm-ui .sidebar-footer {
        padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    }

    body.crm-ui .sidebar-footer .logout-btn {
        width: 42px;
        height: 42px;
    }
}
