/*
 * DachaFlow — общие правила мобильной вёрстки (все страницы).
 * Подключается последним; дополняет Bootstrap и timechimp-theme.
 */

/* Нижняя навигация / заголовок: скрыты на десктопе */
.mobile-bottom-nav,
.mobile-more-overlay,
.mobile-more-sheet,
.mobile-top-title {
    display: none;
}

/* Активы: базовые ширины поиска (десктоп) */
.equipment-page-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.equipment-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.65rem;
    flex: 1 1 auto;
    min-width: 0;
}

.equipment-toolbar-search {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    flex: 1 1 auto;
    min-width: 0;
}

.equipment-search-row {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

#equipmentSearchById {
    width: 6rem;
}

#equipmentSearchText {
    width: 20rem;
    max-width: 100%;
}

.equipment-toolbar-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.4rem;
}

@media (max-width: 767.98px) {
    html {
        -webkit-text-size-adjust: 100%;
    }

    /* Широкий таймлайн не должен раздувать весь документ */
    html,
    body {
        max-width: 100%;
        overflow-x: hidden;
    }

    body.mobile-more-open {
        overflow: hidden;
    }

    /* Вырез экрана (iPhone и др.) + липкая шапка */
    .top-navbar {
        padding-top: env(safe-area-inset-top, 0);
    }

    .top-navbar-container {
        padding-left: max(12px, env(safe-area-inset-left, 0));
        padding-right: max(12px, env(safe-area-inset-right, 0));
        flex-wrap: nowrap;
        align-items: center;
        gap: 8px;
        min-height: 52px;
    }

    .top-navbar-main {
        flex: 1 1 auto;
        min-width: 0;
        flex-wrap: nowrap;
        align-items: center;
        gap: 10px;
    }

    .mobile-menu-toggle {
        display: none !important;
    }

    .top-navbar .nav-links {
        display: none !important;
    }

    .mobile-top-title {
        display: block;
        flex: 1 1 auto;
        min-width: 0;
        font-family: "PT Sans", "PT Sans Narrow", sans-serif;
        font-size: 1.05rem;
        font-weight: 700;
        line-height: 1.2;
        color: var(--color-gray-900, #1f2937);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    html[data-theme="dark"] .mobile-top-title {
        color: #f0f3f7 !important;
    }

    .nav-user-btn .fa-chevron-down {
        display: none;
    }

    main {
        flex: 1 1 auto;
        min-width: 0;
        width: 100%;
        padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
    }

    main > .container-fluid {
        padding-bottom: max(16px, env(safe-area-inset-bottom, 0));
    }

    footer {
        display: none;
    }

    /* ---- Bottom tab bar ---- */
    .mobile-bottom-nav {
        display: flex;
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        max-width: 100%;
        z-index: 1040;
        align-items: stretch;
        justify-content: space-around;
        gap: 2px;
        min-height: 56px;
        padding: 4px 4px calc(4px + env(safe-area-inset-bottom, 0px));
        margin: 0;
        background: #ffffff;
        border-top: 1px solid #e5e7eb;
        box-shadow: 0 -4px 16px rgba(15, 23, 42, 0.06);
        transform: translate3d(0, 0, 0);
        -webkit-backface-visibility: hidden;
        backface-visibility: hidden;
    }

    html[data-theme="dark"] .mobile-bottom-nav {
        background: #1e2228 !important;
        border-top-color: #3d454f !important;
        box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.45);
    }

    .mobile-bottom-nav__item {
        flex: 1 1 0;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        min-width: 0;
        min-height: 48px;
        padding: 4px 2px;
        border: 0;
        background: transparent;
        color: var(--color-gray-500, #6b7280);
        text-decoration: none;
        font-size: 0.68rem;
        font-weight: 600;
        line-height: 1.1;
        -webkit-tap-highlight-color: transparent;
        cursor: pointer;
    }

    .mobile-bottom-nav__item > span {
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .mobile-bottom-nav__item > i,
    .mobile-bottom-nav__icon-wrap {
        font-size: 1.1rem;
        line-height: 1;
    }

    .mobile-bottom-nav__icon-wrap {
        position: relative;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 1.25rem;
        min-height: 1.25rem;
    }

    .mobile-bottom-nav__badge {
        position: absolute;
        top: -6px;
        right: -10px;
        min-width: 1rem;
        height: 1rem;
        padding: 0 3px;
        border-radius: 999px;
        background: #dc2626;
        color: #fff;
        font-size: 0.62rem;
        font-weight: 700;
        line-height: 1rem;
        text-align: center;
    }

    .mobile-bottom-nav__item.is-active,
    .mobile-bottom-nav__item[aria-expanded="true"] {
        color: #2e7d32;
    }

    html[data-theme="dark"] .mobile-bottom-nav__item.is-active,
    html[data-theme="dark"] .mobile-bottom-nav__item[aria-expanded="true"] {
        color: #81c784;
    }

    /* ---- More sheet ---- */
    .mobile-more-overlay {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 1045;
        background: rgba(15, 23, 42, 0.45);
    }

    .mobile-more-overlay[hidden],
    .mobile-more-sheet[hidden] {
        display: none !important;
    }

    .mobile-more-sheet {
        display: flex;
        flex-direction: column;
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 1050;
        max-height: min(78vh, 560px);
        padding: 8px 16px calc(16px + env(safe-area-inset-bottom, 0px));
        border-radius: 16px 16px 0 0;
        background: var(--color-white, #fff);
        box-shadow: 0 -8px 32px rgba(15, 23, 42, 0.18);
    }

    html[data-theme="dark"] .mobile-more-sheet {
        background: var(--color-white, #1e2228);
        box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.5);
    }

    .mobile-more-sheet__handle {
        width: 40px;
        height: 4px;
        margin: 4px auto 12px;
        border-radius: 999px;
        background: var(--color-gray-300, #d1d5db);
    }

    .mobile-more-sheet__title {
        margin: 0 0 12px;
        font-size: 1.05rem;
        font-weight: 700;
    }

    .mobile-more-sheet__list {
        display: flex;
        flex-direction: column;
        gap: 4px;
        overflow-y: auto;
        margin-bottom: 12px;
    }

    .mobile-more-sheet__link {
        display: flex;
        align-items: center;
        gap: 12px;
        min-height: 48px;
        padding: 10px 12px;
        border-radius: 12px;
        color: var(--color-gray-800, #1f2937);
        text-decoration: none;
        font-weight: 600;
    }

    html[data-theme="dark"] .mobile-more-sheet__link {
        color: var(--color-gray-900, #f0f3f7);
    }

    .mobile-more-sheet__link i {
        width: 1.25rem;
        text-align: center;
        color: #2e7d32;
    }

    .mobile-more-sheet__link.is-active {
        background: rgba(46, 125, 50, 0.12);
        color: #2e7d32;
    }

    .mobile-more-sheet__close {
        min-height: 44px;
        border-radius: 12px;
    }

    .page-title {
        font-size: 1.375rem;
        line-height: 1.25;
        margin-bottom: 12px;
        word-break: break-word;
    }

    /* Заголовок = сам flex-контейнер (напр. Активы) */
    .page-header-section.d-flex {
        flex-direction: column !important;
        align-items: stretch !important;
    }

    .page-header-section.d-flex > * {
        width: 100%;
        max-width: 100%;
    }

    .page-header-section.d-flex .btn-group {
        display: flex;
        flex-wrap: wrap;
        justify-content: stretch;
        gap: 0.35rem;
    }

    .page-header-section.d-flex .btn-group > .btn {
        flex: 1 1 auto;
        min-width: min(100%, 10rem);
    }

    /* Заголовок: внутренний ряд «заголовок | кнопки» */
    .page-header-section > .d-flex {
        flex-direction: column;
        align-items: stretch !important;
    }

    .page-header-section > .d-flex > div:last-child {
        width: 100%;
    }

    .page-header-section > .d-flex .btn,
    .page-header-section > .d-flex .btn-group {
        width: 100%;
    }

    .page-header-section > .d-flex .btn-group .btn {
        width: auto;
        flex: 1 1 auto;
    }

    /* Дашборд активов: широкое поле поиска на узком экране */
    #equipmentSearchText {
        width: 100% !important;
        max-width: 100%;
    }

    #equipmentSearchById {
        width: 100% !important;
        max-width: 8rem;
    }

    .page-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap;
        scrollbar-width: none;
    }

    .page-tabs::-webkit-scrollbar {
        display: none;
    }

    .page-tab {
        flex: 0 0 auto;
        white-space: nowrap;
    }

    footer .d-flex {
        flex-direction: column !important;
        align-items: center !important;
        gap: 8px;
        text-align: center;
    }

    footer .d-flex > div {
        width: 100%;
    }

    .modal-dialog {
        margin: 0.5rem auto;
        max-width: calc(100vw - 1rem);
    }

    .modal-dialog.modal-lg,
    .modal-dialog.modal-xl {
        max-width: calc(100vw - 1rem);
    }

    /* Крупнее зона нажатия для основных кнопок в контенте */
    main .btn:not(.btn-sm):not(.btn-xs):not(.btn-link) {
        min-height: 2.5rem;
        padding-top: 0.45rem;
        padding-bottom: 0.45rem;
    }

    img,
    video,
    svg {
        max-width: 100%;
        height: auto;
    }

    /* Обычные таблицы: горизонтальный скролл, не сжимать колонки в кашу */
    .table-responsive {
        -webkit-overflow-scrolling: touch;
        overflow-x: auto;
        max-width: 100%;
    }

    .table-responsive > table:not(.mobile-card-table):not(.license-quarters-table) {
        min-width: 36rem;
    }

    .table-responsive > table:not(.mobile-card-table):not(.license-quarters-table) th,
    .table-responsive > table:not(.mobile-card-table):not(.license-quarters-table) td {
        white-space: nowrap;
        vertical-align: middle;
    }

    /* /licenses/: широкий скролл, ячейки месяцев не ломаем nowrap-правилом выше */
    #license-tables-wrapper .table-responsive {
        margin: 0 -4px;
        padding: 0 4px;
    }

    #license-tables-wrapper .license-quarters-table {
        min-width: 52rem;
    }

    /* GTD: отдельная компактная компоновка для телефона */
    .gtd-page-header {
        margin-bottom: 8px;
    }

    .gtd-page-header,
    .gtd-stats-row,
    .gtd-quick-actions-card,
    .gtd-inbox-card {
        margin-left: 6px !important;
        margin-right: 6px !important;
    }

    .gtd-page-header .page-title {
        margin-bottom: 0;
    }

    .gtd-stats-row {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(min(100%, 10.5rem), 1fr));
        gap: 6px;
        margin-top: 0 !important;
        margin-bottom: 8px !important;
        padding: 0;
        box-sizing: border-box;
        --bs-gutter-x: 0;
        --bs-gutter-y: 0;
    }

    .gtd-stats-row > [class*="col-"] {
        width: auto;
        max-width: none;
        min-width: 0;
        padding: 0;
    }

    .gtd-stats-row .stat-card {
        min-height: 62px;
        padding: 8px 10px;
        border-radius: 10px;
        box-sizing: border-box;
    }

    .gtd-stats-row .stat-card-header {
        align-items: flex-start;
        gap: 6px;
        margin-bottom: 4px;
    }

    .gtd-stats-row .stat-card-title {
        font-size: 0.66rem;
        line-height: 1.15;
        letter-spacing: 0.035em;
    }

    .gtd-stats-row .stat-card-value-inline {
        font-size: 1.125rem;
        line-height: 1;
    }

    .gtd-stats-row .stat-card-link {
        font-size: 0.78rem;
        line-height: 1.2;
    }

    .gtd-quick-actions-card,
    .gtd-inbox-card {
        margin-bottom: 8px !important;
    }

    .gtd-quick-actions-card .card-header,
    .gtd-inbox-card .card-header {
        padding: 8px 10px;
    }

    .gtd-quick-actions-card .card-body,
    .gtd-inbox-card .card-body {
        padding: 10px;
    }

    .gtd-quick-actions {
        display: grid !important;
        grid-template-columns: 1fr;
        gap: 6px !important;
    }

    .gtd-quick-actions .btn {
        width: 100%;
        justify-content: center;
    }

    .gtd-inbox-form {
        display: none !important;
    }

    .gtd-inbox-form > div {
        min-width: 0 !important;
        width: 100%;
    }

    .gtd-inbox-form .btn {
        width: 100%;
    }

    #inboxTable {
        min-width: 620px;
    }

    #inboxTable th:nth-child(7),
    #inboxTable td:nth-child(7) {
        display: none;
    }

    /* ---- Активы: мобильная компоновка ---- */
    .equipment-page-header {
        flex-direction: column;
        align-items: stretch;
        gap: 0.55rem;
        margin-bottom: 0.75rem !important;
    }

    .equipment-page-title {
        display: none;
    }

    .equipment-toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 0.5rem;
        width: 100%;
    }

    .equipment-toolbar-search {
        flex-direction: column;
        align-items: stretch;
        gap: 0.4rem;
        width: 100%;
    }

    .equipment-search-row {
        width: 100%;
    }

    #equipmentSearchById {
        width: 4.75rem !important;
        flex: 0 0 4.75rem;
    }

    #equipmentSearchText {
        width: 100% !important;
        max-width: none !important;
        flex: 1 1 auto;
        min-width: 0;
    }

    .equipment-toolbar-actions {
        width: 100%;
        display: grid;
        grid-template-columns: 1fr auto;
        gap: 0.4rem;
    }

    .equipment-toolbar-actions .equipment-photo-group {
        min-width: 0;
    }

    .equipment-toolbar-actions #equipmentAddDevice {
        grid-column: 1 / -1;
        width: 100%;
    }

    .equipment-photo-btn-label {
        font-size: 0.85rem;
    }

    /* Карточка списка устройств */
    #equipmentViewGrid,
    #equipmentViewList {
        display: none !important;
    }

    .card.mb-4 > .card-header {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.5rem;
        padding: 0.65rem 0.75rem;
    }

    .card.mb-4 > .card-header > .d-flex {
        width: 100%;
        flex-wrap: wrap;
        gap: 0.4rem !important;
    }

    #equipmentHierarchyMode,
    #equipmentFilterDeviceType {
        min-width: 0 !important;
        flex: 1 1 calc(50% - 0.25rem);
        width: auto !important;
    }

    .card.mb-4 > .card-body {
        padding: 0.55rem !important;
    }

    /* Иерархия: одна колонка, без «лесенки» */
    .equipment-hier-dept-columns,
    .equipment-hier-cabinet-columns,
    .equipment-cabinet-devices {
        display: flex !important;
        flex-direction: column;
        gap: 0.4rem !important;
        grid-template-columns: none !important;
    }

    .equipment-hier-dept-column {
        padding: 0 !important;
        border: 0 !important;
        box-shadow: none !important;
        background: transparent !important;
    }

    .equipment-hier-body,
    .equipment-reserve-group-body {
        padding: 0.35rem 0 0 !important;
        margin: 0 !important;
        border: 0 !important;
    }

    .equipment-hier-block {
        margin-bottom: 0.3rem;
    }

    .equipment-hier-org-section {
        margin-bottom: 0.65rem;
    }

    .equipment-hier-header {
        padding: 0.4rem 0.5rem;
        font-size: 0.78rem;
        gap: 0.3rem;
        border-radius: 0.4rem;
    }

    .equipment-hier-header .equipment-hier-header-text {
        white-space: normal;
        overflow: visible;
        text-overflow: unset;
    }

    .equipment-hier-org-banner {
        margin-bottom: 0.35rem !important;
    }

    .equipment-device-card {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        border-left-width: 4px;
        box-sizing: border-box;
    }

    .equipment-device-header {
        padding: 0.35rem 0.5rem 0;
        font-size: 0.75rem;
    }

    .equipment-device-card .equipment-device-body,
    .equipment-device-card.equipment-device-card-compact .equipment-device-body {
        padding: 0.35rem 0.5rem 0.45rem;
        min-width: 0;
    }

    .equipment-device-card.equipment-device-card-compact .equipment-device-name,
    .equipment-device-card .equipment-device-name {
        white-space: normal !important;
        overflow: visible !important;
        text-overflow: unset !important;
        word-break: break-word;
        line-height: 1.25;
        font-size: 0.88rem;
    }

    .equipment-device-card .equipment-device-meta,
    .equipment-device-card .equipment-device-body small {
        word-break: break-word;
        overflow-wrap: anywhere;
    }

    .equipment-device-card.online,
    .equipment-device-card.offline,
    .equipment-device-card.unknown {
        background: var(--color-white, #fff);
    }

    .equipment-device-card.offline {
        background: color-mix(in srgb, #e74a3b 8%, var(--color-white, #fff));
    }

    html[data-theme="dark"] .equipment-device-card.online,
    html[data-theme="dark"] .equipment-device-card.offline,
    html[data-theme="dark"] .equipment-device-card.unknown {
        background: var(--color-white, #1e2228);
    }

    html[data-theme="dark"] .equipment-device-card.offline {
        background: color-mix(in srgb, #e74a3b 12%, var(--color-white, #1e2228));
    }

    html[data-theme="dark"] .equipment-hier-dept-column {
        background: transparent !important;
        border: 0 !important;
    }

    /* ---- Главная (/): таймлайн + таблица проектов ---- */
    #timeline-card {
        max-width: 100%;
        overflow: hidden;
        z-index: 1;
    }

    #timeline-card > .card-header {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 0.55rem;
        padding: 0.65rem 0.75rem;
    }

    #timeline-card > .card-header > .d-flex {
        width: 100%;
        flex-wrap: wrap;
        gap: 0.4rem !important;
        align-items: stretch !important;
    }

    #timeline-card > .card-header .dropdown,
    #timeline-card > .card-header .form-check {
        flex: 1 1 calc(50% - 0.25rem);
        min-width: 0;
        margin: 0 !important;
    }

    #timeline-card > .card-header .dropdown .btn {
        width: 100%;
        justify-content: space-between;
    }

    #timeline-card > .card-header .form-check {
        display: flex;
        align-items: center;
        min-height: 36px;
        padding: 0 4px;
    }

    #timeline-card .card-body {
        padding: 0.5rem !important;
        max-width: 100%;
        overflow: hidden;
    }

    .horizontal-timeline {
        max-width: 100%;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
        padding: 12px 0 4px !important;
    }

    .timeline-vector-container {
        padding-left: 12px !important;
        padding-right: 12px !important;
    }

    .table-actions-bar {
        flex-direction: column;
        align-items: stretch;
        gap: 0.5rem;
        margin-bottom: 10px;
    }

    .table-actions-left,
    .table-actions-right {
        width: 100%;
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.4rem;
    }

    .table-actions-right {
        grid-template-columns: 1fr;
    }

    .table-actions-left .dropdown,
    .table-actions-right .btn-primary-action {
        width: 100%;
    }

    .table-actions-left .action-btn,
    .table-actions-right .btn-primary-action {
        width: 100%;
        justify-content: center;
        min-height: 40px;
    }

    /* ---- Активные проекты: карточки вместо таблицы ---- */
    #activeProjectsTable.mobile-card-table {
        min-width: 0 !important;
        width: 100%;
        border: 0;
    }

    #activeProjectsTable.mobile-card-table thead {
        display: none;
    }

    #activeProjectsTable.mobile-card-table,
    #activeProjectsTable.mobile-card-table tbody {
        display: block;
        width: 100%;
    }

    #activeProjectsTable.mobile-card-table tr.group-header-row,
    #activeProjectsTable.mobile-card-table tr.subgroup-header-row {
        display: block;
        width: 100%;
        margin: 0 0 8px;
        border: 0;
        background: transparent;
    }

    #activeProjectsTable.mobile-card-table .group-header-cell,
    #activeProjectsTable.mobile-card-table .subgroup-header-cell {
        display: block;
        width: 100%;
        max-width: 100%;
        white-space: normal;
        border: 0 !important;
        padding: 8px 4px !important;
        background: transparent !important;
    }

    #activeProjectsTable.mobile-card-table tr.project-data-row {
        display: block;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        margin: 0 0 10px;
        padding: 12px 12px 8px;
        border: 1px solid var(--color-gray-200, #e5e7eb) !important;
        border-radius: 12px;
        background: var(--color-white, #fff);
        box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
    }

    html[data-theme="dark"] #activeProjectsTable.mobile-card-table tr.project-data-row {
        background: var(--color-white, #1e2228);
        border-color: #3d454f !important;
        box-shadow: none;
    }

    #activeProjectsTable.mobile-card-table tr.project-data-row > td {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 12px;
        width: 100%;
        max-width: 100%;
        border: 0 !important;
        padding: 6px 0 !important;
        white-space: normal;
        word-break: break-word;
        overflow-wrap: anywhere;
        background: transparent !important;
    }

    #activeProjectsTable.mobile-card-table tr.project-data-row > td[style*="display: none"],
    #activeProjectsTable.mobile-card-table tr.project-data-row > td:first-child {
        display: none !important;
    }

    #activeProjectsTable.mobile-card-table tr.project-data-row > td::before {
        content: attr(data-label);
        flex: 0 0 38%;
        max-width: 38%;
        color: var(--color-gray-500, #6b7280);
        font-size: 0.78rem;
        font-weight: 600;
        line-height: 1.35;
    }

    #activeProjectsTable.mobile-card-table tr.project-data-row > td > *:not(script) {
        flex: 1 1 auto;
        min-width: 0;
        text-align: right;
    }

    #activeProjectsTable.mobile-card-table tr.project-data-row > td.mobile-card-title-cell {
        display: block;
        padding-bottom: 10px !important;
        margin-bottom: 4px;
        border-bottom: 1px solid var(--color-gray-200, #e5e7eb) !important;
    }

    html[data-theme="dark"] #activeProjectsTable.mobile-card-table tr.project-data-row > td.mobile-card-title-cell {
        border-bottom-color: #3d454f !important;
    }

    #activeProjectsTable.mobile-card-table tr.project-data-row > td.mobile-card-title-cell::before {
        display: none;
    }

    #activeProjectsTable.mobile-card-table tr.project-data-row > td.mobile-card-title-cell a {
        display: block;
        text-align: left;
        font-size: 1.02rem;
        font-weight: 700;
        line-height: 1.3;
        color: var(--color-gray-900, #111827);
    }

    html[data-theme="dark"] #activeProjectsTable.mobile-card-table tr.project-data-row > td.mobile-card-title-cell a {
        color: #f0f3f7;
    }

    /* На телефоне скрываем шумные вторичные поля */
    #activeProjectsTable.mobile-card-table tr.project-data-row > td[data-label="Договор"],
    #activeProjectsTable.mobile-card-table tr.project-data-row > td[data-label="Комментарий"],
    #activeProjectsTable.mobile-card-table tr.project-data-row > td[data-label="Задачи"] {
        display: none !important;
    }
}
