/* Основные стили проекта CRM TechBar.
   Объединённый CSS (таблицы, мобильная версия, комментарии, поле СБ). */

/* Кнопка выхода в навбаре — стиль как у nav-link */
.navbar .nav-item form button.nav-link {
    color: rgba(255, 255, 255, 0.75);
}
.navbar .nav-item form button.nav-link:hover {
    color: #fff;
}

/* Редактируемые поля — визуальные подсказки */
.editable-date,
.editable-comment,
.editable-internal-comment,
.sb-date-editable {
    cursor: pointer;
    transition: background-color 0.2s, box-shadow 0.2s;
}

.editable-date {
    border-bottom: 1px dashed #007bff;
    padding: 2px 4px;
    border-radius: 3px;
}

.editable-date:hover,
.editable-date:focus-visible {
    background-color: #f0f8ff;
    box-shadow: 0 0 0 1px rgba(0, 123, 255, 0.2);
}

.editable-comment:hover,
.editable-comment:focus-visible,
.editable-internal-comment:hover,
.editable-internal-comment:focus-visible {
    box-shadow: 0 0 0 1px rgba(0, 123, 255, 0.2);
}

.sb-date-editable:hover,
.sb-date-editable:focus-visible {
    box-shadow: 0 0 0 1px rgba(0, 123, 255, 0.2);
}

.table td {
    vertical-align: middle;
}

.table {
    width: 100%;
    margin-bottom: 1rem;
    background-color: transparent;
}

.nav-tabs .nav-link {
    font-weight: 700 !important;
    white-space: nowrap;
}

/* Панель фильтров: подписи внутри полей, сетка на десктопе */
.filter-fields-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    align-items: stretch;
}

.filter-fields-row .input-group {
    flex: 1 1 auto;
    min-width: 120px;
}

.filter-fields-row .input-group-text {
    white-space: nowrap;
}

/* Панель фильтров: 3 колонки × 2 строки */
.filter-panel-grid {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
    padding: 0.4rem 0.5rem;
}
.filter-grid-cell {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}
.filter-grid-cell .input-group {
    width: 100%;
}
.filter-grid-actions {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 0.35rem;
}
@media screen and (min-width: 576px) {
    .filter-panel-grid {
        grid-template-columns: 1fr 1fr 1fr;
        grid-template-rows: auto auto;
    }
    .filter-grid-actions {
        grid-column: 1 / -1;
    }
}

/* Компактная панель поиска и сортировки на десктопе */
@media screen and (min-width: 769px) {
    .filter-panel-grid {
        padding: 0.4rem 0.5rem;
        gap: 0.25rem;
    }
    .filter-grid-cell {
        gap: 0.25rem;
    }
    .filter-panel-grid .input-group .form-control,
    .filter-panel-grid .input-group .form-select,
    .filter-panel-grid .input-group .input-group-text {
        padding: 0.25rem 0.5rem;
        font-size: 0.875rem;
    }
    .filter-panel-grid .btn {
        padding: 0.25rem 0.5rem;
        font-size: 0.875rem;
    }
}

.table td:first-child,
.table td[data-label="Гос. номер"] {
    font-weight: 700 !important;
}

.table td.cell-copy-gov {
    cursor: pointer;
}
.table td.cell-copy-gov:hover {
    text-decoration: underline;
}

/* ===== Панель управления: читаемость ===== */
#myTab {
    gap: 0.25rem;
    border-bottom: 1px solid #dee2e6;
}

#myTab .nav-link {
    border-radius: 8px 8px 0 0;
    color: #495057;
    padding: 0.55rem 0.9rem;
}

#myTab .nav-link.active {
    background-color: #f8f9fa;
    border-color: #dee2e6 #dee2e6 #f8f9fa;
    color: #0d6efd;
}

[id^="filterPanel-"] {
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 10px;
    padding: 0.65rem 0.5rem;
}

[id^="filterPanel-"] .input-group-text {
    font-weight: 600;
    background: #fff;
}

[id^="filterPanel-"] .btn {
    white-space: nowrap;
}

.tab-pane .table-responsive {
    border: 1px solid #e9ecef;
    border-radius: 10px;
    background: #fff;
}

.tab-pane .table {
    margin-bottom: 0;
}

.tab-pane .table thead th {
    position: sticky;
    top: 0;
    z-index: 2;
    background: #f1f3f5;
    border-bottom: 1px solid #dee2e6;
    font-weight: 700;
}

.tab-pane .table tbody tr:hover {
    box-shadow: inset 0 0 0 9999px rgba(13, 110, 253, 0.04);
}

.cell-readonly {
    background: rgba(108, 117, 125, 0.06) !important;
}

@media screen and (max-width: 768px) {
    /* Обёртка таблицы: не скролл, а карточки */
    .table-responsive {
        overflow-x: visible !important;
    }

    .nav-tabs {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
        border-bottom: 2px solid #dee2e6;
        padding-bottom: 0;
        margin-bottom: 10px;
        scrollbar-width: thin;
    }

    .nav-tabs .nav-item {
        flex: 0 0 auto;
        margin-bottom: 0;
    }

    .nav-tabs .nav-link {
        font-weight: 700 !important;
        padding: 8px 12px;
        font-size: 14px;
        border: none;
        border-bottom: 3px solid transparent;
        white-space: nowrap;
    }

    .nav-tabs .nav-link.active {
        border-bottom: 3px solid #007bff;
    }

    .nav-tabs::-webkit-scrollbar {
        height: 3px;
    }

    .nav-tabs::-webkit-scrollbar-track {
        background: #f1f1f1;
    }

    .nav-tabs::-webkit-scrollbar-thumb {
        background: #007bff;
        border-radius: 3px;
    }

    /* Карточки на мобильных: таблица как блоки, заголовки по вертикали через data-label */
    .table-responsive table,
    .table-responsive thead,
    .table-responsive tbody,
    .table-responsive th,
    .table-responsive td,
    .table-responsive tr {
        display: block !important;
    }

    .table-responsive thead {
        display: none !important;
    }

    .table-responsive tbody tr {
        border: 1px solid #dee2e6;
        border-radius: 10px;
        margin-bottom: 10px;
        padding: 8px 10px;
        background-color: white;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
    }

    .table-responsive td {
        display: flex !important;
        justify-content: space-between;
        align-items: center;
        gap: 8px;
        padding: 6px 4px;
        border: none;
        border-bottom: 1px solid #f0f0f0;
        font-size: 13px;
        text-align: right;
        min-height: 34px;
    }

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

    /* Подпись слева (заголовок по вертикали для каждой карточки) */
    .table-responsive td:before {
        content: attr(data-label) !important;
        font-weight: 600;
        color: #0056b3;
        text-align: left;
        padding-right: 8px;
        width: 42%;
        font-size: 12px;
        white-space: normal;
        overflow: visible;
        text-overflow: clip;
        line-height: 1.2;
    }

    .table-responsive td[data-label="Гос. номер"] {
        font-weight: 700 !important;
    }

    .table-responsive td select {
        width: 58%;
        margin-left: auto;
        text-align: left;
        font-size: 12px;
        padding: 4px 6px;
        height: auto;
    }

    .table-responsive td input[type="checkbox"] {
        width: auto;
        margin-right: 0;
        margin-left: auto;
        transform: scale(1);
    }

    .table-responsive td .editable-date,
    .table-responsive td .editable-comment {
        width: 58%;
        text-align: left;
        display: inline-block;
        font-size: 12px;
        padding: 4px 6px;
    }

    /* Ячейка «Комментарий»: шире поле и удобная зона нажатия на мобильных */
    .table-responsive td[data-label="Комментарий"] {
        min-height: 44px;
        padding: 8px 5px;
        align-items: flex-start;
    }

    .table-responsive td[data-label="Комментарий"]:before {
        width: 28%;
        padding-top: 4px;
    }

    .table-responsive td[data-label="Комментарий"] .editable-comment {
        width: 70%;
        min-height: 36px;
        padding: 8px 10px;
        font-size: 13px;
        display: block;
        box-sizing: border-box;
    }

    .table-responsive td span:not(.sb-placeholder) {
        width: 58%;
        text-align: left;
        font-size: 12px;
        white-space: normal;
        word-break: break-word;
    }

    /* Ячейка «Согласование СБ»: контейнер и чекбокс всегда видны на мобильных */
    .table-responsive td[data-label="Согласование СБ"] {
        min-height: 36px;
    }

    .table-responsive td[data-label="Согласование СБ"] .sb-container,
    .table-responsive td[data-label="Согласование СБ"] .d-flex {
        display: flex !important;
        flex-direction: row;
        align-items: center;
        gap: 6px;
        width: 60%;
        margin-left: auto;
        min-width: 0;
    }

    .table-responsive td[data-label="Согласование СБ"] input[type="checkbox"].sb-checkbox {
        flex-shrink: 0;
        width: 18px;
        height: 18px;
        min-width: 18px;
        min-height: 18px;
        margin: 0;
        display: inline-block;
        opacity: 1;
    }

    .sb-container {
        width: 60%;
        margin-left: auto;
        flex-direction: column;
        align-items: flex-end;
        gap: 3px;
    }

    .sb-container .sb-checkbox {
        margin-left: 0;
        margin-right: 5px;
    }

    .sb-date-editable {
        font-size: 11px;
        padding: 1px 3px;
        width: 100%;
        text-align: right;
    }

    .table-responsive td .badge,
    .table-responsive td .text-muted {
        font-size: 10px;
    }

    .table-responsive td button {
        width: 60%;
        font-size: 11px;
        padding: 2px 6px;
        margin-left: auto;
    }

    @media screen and (max-width: 480px) {
        .table-responsive td {
            font-size: 12px;
            padding: 3px 4px;
        }

        .table-responsive td:before {
            width: 30%;
            font-size: 11px;
        }

        .table-responsive td select,
        .table-responsive td .editable-date,
        .table-responsive td span:not(.sb-placeholder) {
            width: 65%;
            font-size: 11px;
        }

        .table-responsive td[data-label="Комментарий"]:before {
            width: 26%;
        }

        .table-responsive td[data-label="Комментарий"] .editable-comment {
            width: 72%;
            min-height: 40px;
            padding: 10px 12px;
            font-size: 12px;
        }
    }
}

@media screen and (min-width: 992px) {
    .container {
        max-width: 95% !important;
    }

    .table td,
    .table th {
        padding: 0.5rem;
        font-size: 13px;
    }

    .table td:first-child,
    .table th:first-child {
        padding-left: 0.75rem;
    }

    .table td:last-child,
    .table th:last-child {
        padding-right: 0.75rem;
    }

    .table select.form-select-sm {
        font-size: 12px;
        padding: 0.25rem 1.5rem 0.25rem 0.5rem;
        min-width: 130px;
    }

    .table input[type="checkbox"] {
        transform: scale(1.2);
        margin: 0 auto;
        display: block;
    }
}

@media screen and (min-width: 1400px) {
    .container {
        max-width: 98% !important;
    }

    .table td,
    .table th {
        font-size: 14px;
        padding: 0.6rem;
    }
}

.comment-container {
    max-width: 300px;
    position: relative;
}

.editable-comment,
.editable-internal-comment {
    display: inline-block;
    white-space: pre-wrap;
    word-wrap: break-word;
    word-break: break-word;
    line-height: 1.3;
    padding: 2px 4px;
    background-color: #f8f9fa;
    border-radius: 3px;
    cursor: pointer;
    transition: background-color 0.2s;
    font-size: 0.9em;
}

.editable-comment:hover,
.editable-internal-comment:hover {
    background-color: #e9ecef;
}

/* Пустой комментарий: увеличиваем зону клика и показываем подсказку */
.editable-comment:empty,
.editable-internal-comment:empty {
    display: inline-block;
    min-width: 220px;
    min-height: 36px;
    padding: 8px 10px;
    border: 1px dashed #ced4da;
    background: #fff;
}

.editable-comment:empty::before {
    content: "Нажмите дважды, чтобы добавить комментарий";
    color: #6c757d;
    font-size: 0.85em;
}

.editable-internal-comment:empty::before {
    content: "Нажмите дважды, чтобы добавить внутренний комментарий";
    color: #6c757d;
    font-size: 0.85em;
}

.sb-cell {
    min-width: 200px;
}

.sb-container {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
}

.sb-date-editable {
    cursor: pointer;
    padding: 2px 4px;
    background-color: #f8f9fa;
    border-radius: 3px;
    font-size: 0.9em;
}

.sb-date-editable:hover {
    background-color: #e9ecef;
}

.sb-placeholder {
    color: #6c757d;
    font-style: italic;
}

.sb-edit-container {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

/* Toast-уведомления */
.toast-container .toast {
    min-width: 280px;
}
.toast-container .toast.bg-danger .toast-header { background-color: rgba(220, 53, 69, 0.2); }
.toast-container .toast.bg-warning .toast-header { background-color: rgba(255, 193, 7, 0.2); color: #333; }

/* ========== Мобильная адаптация: общие улучшения ========== */
@media screen and (max-width: 767px) {
    body {
        padding-left: env(safe-area-inset-left, 0);
        padding-right: env(safe-area-inset-right, 0);
    }

    main.container-fluid {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
        margin-top: 0.75rem;
    }

    /* Панель управления: компактный заголовок и кнопки для экономии места */
    .d-flex.justify-content-between.align-items-center.mb-3 {
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        gap: 0.4rem;
        margin-bottom: 0.5rem !important;
    }

    .d-flex.justify-content-between.align-items-center.mb-3 h2 {
        font-size: 1.1rem;
        margin-bottom: 0;
        flex: 1 1 auto;
    }

    .d-flex.justify-content-between.align-items-center.mb-3 > div {
        display: flex;
        flex-wrap: wrap;
        gap: 0.35rem;
        justify-content: flex-end;
    }

    .d-flex.justify-content-between.align-items-center.mb-3 .btn {
        min-height: 34px;
        padding: 0.25rem 0.5rem;
        font-size: 0.875rem;
        min-width: auto;
    }

    /* Поиск по гос. номеру — всегда виден на мобильных, вне кнопки «Показать фильтры» */
    .filter-gov-mobile {
        margin-bottom: 0.5rem;
    }
    .filter-gov-mobile .input-group {
        min-height: 34px;
    }
    .filter-gov-mobile .input-group-text {
        padding: 0.25rem 0.4rem;
        font-size: 0.8rem;
    }
    .filter-gov-mobile .form-control {
        padding: 0.25rem 0.4rem;
        font-size: 0.875rem;
    }

    /* Фильтры: сворачиваемые и более компактные */
    .filter-panel-collapsible.collapsed-mobile {
        display: none;
    }

    .filter-toggle-btn {
        width: 100%;
        border-radius: 8px;
        font-size: 0.85rem;
    }

    .filter-panel-grid {
        margin-bottom: 0.5rem !important;
        grid-template-columns: 1fr;
    }

    .filter-panel-grid .input-group .input-group-text {
        padding: 0.25rem 0.4rem;
        font-size: 0.8rem;
        min-width: 0;
        background: #fff;
    }

    .filter-panel-grid .input-group .form-control,
    .filter-panel-grid .input-group .form-select,
    .filter-panel-grid .filter-grid-actions .btn {
        min-height: 34px;
        padding: 0.25rem 0.4rem;
        font-size: 0.875rem;
    }

    .filter-panel-grid .input-group .form-control,
    .filter-panel-grid .input-group .form-select {
        min-width: 0;
    }

    .filter-panel-grid .filter-grid-actions .btn {
        min-width: 64px;
    }

    .filter-grid-actions {
        grid-column: 1 / -1;
    }

    /* Вкладки дашборда: чуть крупнее tap-зона */
    #myTab .nav-link {
        padding: 10px 14px;
        font-size: 14px;
        border-radius: 10px 10px 0 0;
    }

    /* Компактная карточка: первая строка = гос.номер + контрагент без labels */
    .table-responsive tbody tr {
        display: flex !important;
        flex-wrap: wrap;
        position: relative;
    }

    .table-responsive tbody tr td {
        width: 100%;
    }

    .table-responsive tbody tr td[data-label="Гос. номер"],
    .table-responsive tbody tr td[data-label="Контрагент"] {
        width: 50%;
        border-bottom: none;
        padding-top: 2px;
        padding-bottom: 6px;
        min-height: 24px;
    }

    .table-responsive tbody tr td[data-label="Гос. номер"]:before,
    .table-responsive tbody tr td[data-label="Контрагент"]:before {
        content: "" !important;
        display: none !important;
    }

    .table-responsive tbody tr td[data-label="Гос. номер"] {
        width: 100%;
        justify-content: flex-start;
        text-align: left;
        font-size: 14px;
        font-weight: 700 !important;
        padding-right: 42%;
    }

    .table-responsive tbody tr td[data-label="Контрагент"] {
        position: absolute;
        top: 8px;
        right: 10px;
        width: auto;
        max-width: 40%;
        justify-content: flex-end;
        text-align: right;
        font-size: 12px;
        color: #6c757d;
        padding: 0;
    }

    /* Модальные окна на весь экран на маленьких экранах */
    .modal-fullscreen-sm-down .modal-dialog {
        max-width: none;
        margin: 0;
    }

    .modal-fullscreen-sm-down .modal-content {
        min-height: 100vh;
        border-radius: 0;
    }

    .modal-fullscreen-sm-down .modal-footer .btn {
        min-height: 44px;
    }

    /* Навбар и логотип компактнее на мобильных (текст без изменений) */
    .navbar {
        padding-top: 0.35rem;
        padding-bottom: 0.35rem;
    }

    .navbar .container-fluid {
        padding-top: 0.25rem;
        padding-bottom: 0.25rem;
    }

    .navbar-brand {
        font-size: 1rem;
    }

    .navbar-brand img {
        height: 36px;
        width: 36px;
        max-width: 36px;
    }
}

@media screen and (max-width: 480px) {
    main.container-fluid {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }

    .navbar .container-fluid {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }

    .navbar-brand img {
        height: 32px;
        width: 32px;
        max-width: 32px;
    }
}
