/* Блок фильтров списков (#changelist-filter), подключается глобально. Префикс: afs-
 * Логика панели — static/admin/js/changelist-filters.js. */

/* ---------- Единый вид групп фильтров ---------- */
#changelist-filter details > summary,
#changelist-filter > h3:not(#changelist-filter-clear),
#changelist-filter .afs-body > h3:not(#changelist-filter-clear) {
    margin: 0 0 8px;
    padding: 0 15px;
    color: var(--afs-text, #333);
    font-size: 14px;
    font-weight: 600;
    line-height: 1.35;
    cursor: pointer;
}
#changelist-filter details {
    margin-bottom: 14px;
}
#changelist-filter details > ul {
    margin-top: 0;
}

/* Формы фильтров: «условие + значение» в одну строку, кнопки одинаковой высоты */
#changelist-filter details > form,
#changelist-filter .afs-body > form {
    display: grid;
    grid-template-columns: minmax(64px, 88px) minmax(0, 1fr);
    gap: 8px;
    align-items: end;
    margin: 0;
    padding: 0 15px 14px;
    border-bottom: 1px solid #eaeaea;
}
#changelist-filter form > p {
    margin: 0;
    padding: 0;
    min-width: 0;
}
#changelist-filter form > p label {
    display: block;
    margin: 0 0 3px;
    padding: 0;
    float: none;
    width: auto;
    color: #666;
    font-size: 12px;
}
#changelist-filter form select,
#changelist-filter form input[type="text"],
#changelist-filter form input[type="number"],
#changelist-filter details input.form-control {
    box-sizing: border-box;
    width: 100%;
    max-width: 100%;
    height: 32px;
    min-height: 32px;
    margin: 0;
    padding: 4px 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 13px;
}
#changelist-filter form > p:last-child {
    grid-column: 1 / -1;
    display: flex;
    gap: 8px;
}
#changelist-filter form input[type="submit"],
#changelist-filter form input[type="reset"] {
    flex: 1 1 0;
    min-height: 32px;
    margin: 0;
    padding: 4px 10px;
    border-radius: 4px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}
#changelist-filter form input[type="submit"] {
    border: 1px solid #417690;
    background: #417690;
    color: #fff;
}
#changelist-filter form input[type="reset"] {
    border: 1px solid #ccc;
    background: #fff;
    color: #333;
}
/* Диапазоны «От/До» — две равные колонки */
#changelist-filter form.afs-form--range {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

/* Виджеты внутри группы (диапазон дат, select2) — по ширине колонки */
#changelist-filter details > ul > li.afs-widget {
    padding-right: 0;
}
#changelist-filter details .select2-container {
    box-sizing: border-box;
    width: 100% !important;
    max-width: 100%;
}
/* Исходный <select> под select2 скрыт как .select2-hidden-accessible (absolute, ширина 1px): ширина
   колонки ему не задаётся — иначе он растягивается от #container за правый край и страница становится
   шире экрана (мобильный Chrome уменьшает её целиком). */
#changelist-filter details > .select2-container,
#changelist-filter details > select:not(.select2-hidden-accessible) {
    margin: 4px 15px 0;
    width: calc(100% - 30px) !important;
}
#changelist-filter details > .afs-widget-reset {
    margin: 6px 15px 0;
    font-size: 12px;
}
/* Диапазон дат: пресеты и поля даты — только в мобильной панели, на широком экране — календарь */
#changelist-filter .afs-drp-presets,
#changelist-filter .afs-drp-custom {
    display: none;
}

/* Текущий выбор в заголовке группы показывается только в мобильной панели */
.afs-summary-value {
    display: none;
}

/* ---------- Кнопка и панель (планшет и мобильный) ---------- */
.afs-open-btn,
.afs-header,
.afs-footer,
.afs-backdrop {
    display: none;
}

@media (max-width: 1024px) {
    #changelist-filter {
        display: none !important;
    }
    .afs-open-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        min-height: 44px;
        margin: 0 0 12px;
        padding: 8px 16px;
        border: 1px solid #417690;
        border-radius: 8px;
        background: #fff;
        color: #205067;
        font-size: 15px;
        font-weight: 600;
        cursor: pointer;
    }
    .afs-open-btn::before {
        content: '';
        width: 16px;
        height: 16px;
        background: currentColor;
        -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M3 5h18l-7 8v6l-4 2v-8z'/%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'%3E%3Cpath d='M3 5h18l-7 8v6l-4 2v-8z'/%3E%3C/svg%3E") center / contain no-repeat;
    }
    .afs-count[hidden] {
        display: none;
    }
    .afs-count {
        min-width: 22px;
        height: 22px;
        padding: 0 6px;
        border-radius: 11px;
        background: #417690;
        color: #fff;
        font-size: 12px;
        line-height: 22px;
        text-align: center;
    }

    body.afs-open {
        overflow: hidden;
    }
    body.afs-open .afs-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 9996;
        background: rgba(15, 23, 42, .45);
    }
    body.afs-open #changelist-filter {
        display: flex !important;
        flex-direction: column;
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        z-index: 9997;
        box-sizing: border-box;
        /* !important: страницы задают ширину колонки своим !important (exchange-request.css — 300px) */
        width: min(420px, 100%) !important;
        max-width: 100% !important;
        height: 100vh;
        height: 100dvh;
        max-height: none;
        margin: 0;
        padding: 0;
        border: 0;
        border-radius: 0;
        background: #fff;
        box-shadow: -8px 0 24px rgba(15, 23, 42, .18);
    }
    /* Выпадающие списки виджетов — поверх панели */
    body.afs-open .select2-container--open,
    body.afs-open .daterangepicker,
    body.afs-open .calendarbox,
    body.afs-open .clockbox {
        z-index: 9998 !important;
    }

    .afs-header {
        display: flex;
        flex: 0 0 auto;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: 10px 8px 10px 16px;
        border-bottom: 1px solid #eaeaea;
    }
    .afs-title {
        font-size: 17px;
        font-weight: 700;
    }
    .afs-close {
        width: 44px;
        height: 44px;
        padding: 0;
        border: 0;
        border-radius: 50%;
        background: transparent;
        color: #555;
        font-size: 28px;
        line-height: 1;
        cursor: pointer;
    }
    .afs-body {
        flex: 1 1 auto;
        min-height: 0;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        padding: 12px 0 16px;
    }
    .afs-body > h2 {
        display: none;
    }
    .afs-footer {
        display: flex;
        flex: 0 0 auto;
        gap: 10px;
        padding: 12px 16px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
        border-top: 1px solid #eaeaea;
        background: #fff;
        box-shadow: 0 -4px 12px rgba(15, 23, 42, .06);
    }
    .afs-btn {
        flex: 1 1 0;
        min-height: 48px;
        border-radius: 8px;
        font-size: 16px;
        font-weight: 600;
        cursor: pointer;
    }
    .afs-btn--secondary {
        border: 1px solid #ccc;
        background: #fff;
        color: #333;
    }
    .afs-btn--primary {
        border: 1px solid #417690;
        background: #417690;
        color: #fff;
    }
    .afs-btn--primary.afs-btn--dirty {
        border-color: #205067;
        background: #205067;
        box-shadow: 0 0 0 3px rgba(65, 118, 144, .25);
    }
    .afs-btn:disabled {
        opacity: .6;
    }

    /* ---------- Содержимое панели: группы — карточки, варианты — «таблетки» ---------- */
    body.afs-open .afs-body {
        display: flex;
        flex-direction: column;
        gap: 10px;
        padding: 12px;
        background: #f2f4f7;
    }
    body.afs-open #changelist-filter #changelist-filter-clear,
    body.afs-open #changelist-filter .afs-body > h2 {
        display: none; /* дублирует «Сбросить» внизу панели */
    }
    body.afs-open #changelist-filter .afs-body > details,
    body.afs-open #changelist-filter .afs-body > div > details,
    body.afs-open #changelist-filter .afs-body > h3 {
        margin: 0;
        padding: 12px 14px;
        border-radius: 12px;
        background: #fff;
        box-shadow: 0 1px 2px rgba(16, 24, 40, .06);
    }
    body.afs-open #changelist-filter details > summary {
        display: flex;
        align-items: center;
        gap: 8px;
        margin: 0;
        padding: 0;
        font-size: 15px;
        font-weight: 600;
        list-style: none;
    }
    body.afs-open #changelist-filter details > summary::-webkit-details-marker { display: none; }
    body.afs-open #changelist-filter details > summary::after {
        content: '';
        flex: 0 0 auto;
        width: 8px;
        height: 8px;
        margin-left: 4px;
        border-right: 2px solid #8a94a3;
        border-bottom: 2px solid #8a94a3;
        transform: rotate(45deg) translateY(-2px);
        transition: transform .15s;
    }
    body.afs-open #changelist-filter details:not([open]) > summary::after {
        transform: rotate(-45deg);
    }
    body.afs-open #changelist-filter details[open] > summary {
        margin-bottom: 10px;
    }
    /* Текущий выбор — справа в заголовке (виден и в свёрнутой группе) */
    body.afs-open #changelist-filter .afs-summary-value {
        display: inline;
        margin-left: auto;
        overflow: hidden;
        color: #417690;
        font-size: 13px;
        font-weight: 600;
        text-overflow: ellipsis;
        white-space: nowrap;
        max-width: 45%;
    }
    body.afs-open #changelist-filter details[open] .afs-summary-value { display: none; }
    body.afs-open #changelist-filter details[open] > summary::after { margin-left: auto; }
    body.afs-open #changelist-filter details:not([open]) > summary:not(:has(.afs-summary-value))::after { margin-left: auto; }

    body.afs-open #changelist-filter ul {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        margin: 0;
        padding: 0;
        border: 0;
    }
    body.afs-open #changelist-filter li,
    body.afs-open #changelist-filter li.selected {
        margin: 0;
        padding: 0;
        border: 0;
    }
    body.afs-open #changelist-filter li > a {
        display: inline-flex;
        align-items: center;
        min-height: 40px;
        max-width: 100%;
        padding: 0 14px;
        border: 1px solid #d5dbe1;
        border-radius: 999px;
        background: #fff;
        color: #1f2933;
        font-size: 14px;
        line-height: 1.2;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    body.afs-open #changelist-filter li.selected > a {
        border-color: #417690;
        background: #417690;
        color: #fff;
        font-weight: 600;
    }
    /* Виджеты во всю ширину карточки (диапазон дат) */
    body.afs-open #changelist-filter li.afs-widget { flex: 1 1 100%; }

    /* Диапазон дат: вместо всплывающего календаря — пресеты «таблетками» и свой период двумя полями */
    body.afs-open #changelist-filter .afs-drp-input {
        display: none;
    }
    body.afs-open #changelist-filter ul.afs-drp-presets {
        display: flex;
        margin-bottom: 12px;
    }
    body.afs-open #changelist-filter .afs-drp-custom {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 6px 8px;
    }
    .afs-drp-caption {
        grid-column: 1 / -1;
        color: #666;
        font-size: 12px;
        font-weight: 600;
    }
    #changelist-filter .afs-drp-custom label {
        display: flex;
        flex-direction: column;
        gap: 3px;
        min-width: 0;
        margin: 0;
        padding: 0;
        color: #666;
        font-size: 12px;
    }
    #changelist-filter .afs-drp-custom input[type="date"] {
        display: block;
        box-sizing: border-box;
        width: 100%;
        min-width: 0;
        height: 44px;
        margin: 0;
        padding: 0 10px;
        border: 1px solid #d5dbe1;
        border-radius: 8px;
        background: #fff;
        color: #1f2933;
        font-size: 16px; /* меньше 16px — iOS увеличивает страницу при фокусе */
        -webkit-appearance: none; /* iOS: иначе поле даты не принимает высоту */
    }

    /* Формы: условие + значение в строку; свои «Найти/Очистить» не нужны — всё применяет «Применить» */
    body.afs-open #changelist-filter details > form {
        padding: 0;
        border: 0;
    }
    body.afs-open #changelist-filter form > p:last-child {
        display: none;
    }
    body.afs-open #changelist-filter form select,
    body.afs-open #changelist-filter form input[type="text"],
    body.afs-open #changelist-filter form input[type="number"],
    body.afs-open #changelist-filter details input.form-control {
        height: 40px;
        min-height: 40px;
        border-radius: 8px;
        font-size: 16px; /* меньше 16px — iOS увеличивает страницу при фокусе */
    }
    body.afs-open #changelist-filter details > .select2-container,
    body.afs-open #changelist-filter details > select:not(.select2-hidden-accessible) {
        margin: 0;
        width: 100% !important;
    }
    body.afs-open #changelist-filter .filter-search-input {
        height: 40px;
        font-size: 16px;
    }
    /* select2 — той же высоты, что поля и варианты */
    body.afs-open #changelist-filter .select2-container .select2-selection--single,
    body.afs-open #changelist-filter .select2-container .select2-selection--multiple {
        min-height: 40px;
        border-color: #d5dbe1;
        border-radius: 8px;
    }
    body.afs-open #changelist-filter .select2-container .select2-selection--single .select2-selection__rendered {
        line-height: 38px;
        font-size: 15px;
    }
    body.afs-open #changelist-filter .select2-container .select2-selection--single .select2-selection__arrow {
        height: 38px;
    }
}

@media (max-width: 767px) {
    body.afs-open #changelist-filter {
        width: 100% !important;
        box-shadow: none;
    }
    .afs-open-btn {
        width: 100%;
    }
}

/* ---------- Тёмная тема ---------- */
[data-theme="dark"] #changelist-filter details > summary,
[data-theme="dark"] #changelist-filter > h3:not(#changelist-filter-clear),
[data-theme="dark"] #changelist-filter .afs-body > h3:not(#changelist-filter-clear) {
    color: var(--dt-text-primary);
}
[data-theme="dark"] #changelist-filter details > form,
[data-theme="dark"] #changelist-filter .afs-body > form {
    border-bottom-color: var(--dt-border-light);
}
[data-theme="dark"] #changelist-filter form > p label {
    color: var(--dt-text-secondary);
}
[data-theme="dark"] #changelist-filter form select,
[data-theme="dark"] #changelist-filter form input[type="text"],
[data-theme="dark"] #changelist-filter form input[type="number"],
[data-theme="dark"] #changelist-filter details input.form-control {
    background: var(--dt-bg-input);
    border-color: var(--dt-border);
    color: var(--dt-text-primary);
}
[data-theme="dark"] #changelist-filter form input[type="reset"],
[data-theme="dark"] .afs-btn--secondary {
    background: var(--dt-bg-input);
    border-color: var(--dt-border);
    color: var(--dt-text-primary);
}
[data-theme="dark"] .afs-open-btn {
    background: var(--dt-bg-module);
    border-color: #3d7ea0;
    color: #6db3d4;
}
@media (max-width: 1024px) {
    [data-theme="dark"] body.afs-open #changelist-filter,
    [data-theme="dark"] .afs-footer {
        background: var(--dt-bg-module) !important;
    }
    [data-theme="dark"] .afs-header,
    [data-theme="dark"] .afs-footer {
        border-color: var(--dt-border);
    }
    [data-theme="dark"] .afs-close {
        color: var(--dt-text-secondary);
    }
    [data-theme="dark"] body.afs-open .afs-body {
        background: var(--dt-bg-secondary);
    }
    [data-theme="dark"] body.afs-open #changelist-filter .afs-body > details,
    [data-theme="dark"] body.afs-open #changelist-filter .afs-body > div > details,
    [data-theme="dark"] body.afs-open #changelist-filter .afs-body > h3 {
        background: var(--dt-bg-module);
        box-shadow: none;
    }
    [data-theme="dark"] body.afs-open #changelist-filter li > a {
        border-color: var(--dt-border);
        background: var(--dt-bg-input);
        color: var(--dt-text-primary) !important;
    }
    [data-theme="dark"] body.afs-open #changelist-filter li.selected > a {
        border-color: #3d7ea0;
        background: #3d7ea0;
        color: #fff !important;
    }
    [data-theme="dark"] body.afs-open #changelist-filter .afs-summary-value {
        color: #6db3d4;
    }
    [data-theme="dark"] .afs-drp-caption {
        color: var(--dt-text-secondary);
    }
}
