/*
 * Карточки вместо длинных таблиц на телефонах и в установленном PWA.
 * На больших экранах используется прежняя табличная разметка.
 */
.mobile-card-sortbar { display: none; }

@media (max-width: 767.98px) {
    /* Табличные контейнеры больше не заставляют прокручивать страницу вправо. */
    .table-responsive:has(> .mobile-card-table),
    .wear-table-scroll:has(> .mobile-card-table),
    .wear-table-scroll:has(> .wear-matrix),
    .graph-matrix-container:has(> .graph-matrix) {
        overflow: visible !important;
        max-height: none !important;
        min-width: 0;
        border: 0 !important;
        box-shadow: none !important;
        background: transparent !important;
    }

    .mobile-card-table {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
        max-width: 100% !important;
        table-layout: auto !important;
        border-collapse: separate;
        background: transparent !important;
    }
    .mobile-card-table > thead { display: none !important; }
    .mobile-card-table > tbody {
        display: grid !important;
        gap: .85rem;
        width: 100%;
        min-width: 0;
    }
    .mobile-card-table > tbody > tr {
        display: flex !important;
        flex-direction: column;
        width: 100%;
        min-width: 0;
        margin: 0;
        overflow: hidden;
        border: 1px solid var(--corp-border, #d6e3ec);
        border-top: 3px solid var(--corp-blue, #006cb1);
        border-radius: .75rem;
        background: #fff !important;
        box-shadow: 0 2px 9px rgba(0, 45, 85, .065);
    }
    .mobile-card-table > tbody > tr[hidden] { display: none !important; }
    .mobile-card-table > tbody > tr.material-critical-row {
        border-top-color: #d88422;
    }
    .mobile-card-table > tbody > tr > td,
    .mobile-card-table > tbody > tr > th[scope="row"] {
        display: grid !important;
        grid-template-columns: minmax(0, 36%) minmax(0, 1fr);
        gap: .6rem;
        width: 100% !important;
        min-width: 0 !important;
        max-width: none !important;
        padding: .7rem .85rem !important;
        margin: 0;
        box-sizing: border-box;
        white-space: normal !important;
        overflow: visible !important;
        overflow-wrap: anywhere;
        word-break: normal;
        text-align: left !important;
        vertical-align: top;
        background: transparent !important;
        border: 0 !important;
        border-bottom: 1px solid #e8eef3 !important;
        color: var(--corp-ink, #213047);
        font-weight: 400;
    }
    .mobile-card-table > tbody > tr > :last-child { border-bottom: 0 !important; }
    .mobile-card-table > tbody > tr > td::before {
        content: attr(data-mobile-label);
        min-width: 0;
        color: var(--corp-muted, #516273);
        font-size: .78rem;
        font-weight: 600;
        line-height: 1.4;
        overflow-wrap: anywhere;
    }
    .mobile-card-table > tbody > tr > td.mobile-card-primary {
        order: -1;
        display: flex !important;
        flex-direction: column;
        gap: .25rem;
        background: #f3f8fc !important;
        font-size: 1rem;
        font-weight: 650;
        line-height: 1.4;
    }
    .mobile-card-table > tbody > tr > td.mobile-card-primary::before {
        color: #607487;
        font-size: .72rem;
        letter-spacing: .025em;
    }
    .mobile-card-table > tbody > tr > td.mobile-card-actions {
        display: block !important;
        background: #f8fafc !important;
    }
    .mobile-card-table > tbody > tr > td.mobile-card-actions::before {
        display: block;
        margin-bottom: .65rem;
    }
    .mobile-card-table > tbody > tr > td.mobile-card-actions .btn {
        max-width: 100%;
        white-space: normal;
        min-height: 36px;
    }
    .mobile-card-table > tbody > tr > td.mobile-card-empty {
        display: block !important;
        text-align: center !important;
        padding: 1.25rem !important;
    }
    .mobile-card-table > tbody > tr > td.mobile-card-empty::before { content: none; }
    .mobile-card-table > tbody > tr > td .material-location-path,
    .mobile-card-table > tbody > tr > td .wear-location,
    .mobile-card-table > tbody > tr > td .movement-route-cell {
        max-width: none !important;
        min-width: 0 !important;
        white-space: normal !important;
        overflow-wrap: anywhere;
        text-overflow: clip;
    }
    #materialResults .mobile-card-table .material-location-cell { min-width: 0 !important; }
    .mobile-card-table .material-photo-cell { min-width: 0 !important; }

    .mobile-card-sortbar {
        display: flex;
        flex-wrap: wrap;
        align-items: end;
        gap: .5rem;
        padding: .75rem;
        margin-bottom: .75rem;
        background: #f3f8fc;
        border: 1px solid var(--corp-border, #d6e3ec);
        border-radius: .65rem;
    }
    .mobile-card-sortbar label {
        flex: 1 1 11rem;
        min-width: 0;
        font-size: .8rem;
        font-weight: 600;
        color: var(--corp-muted, #516273);
    }
    .mobile-card-sortbar select { width: 100%; margin-top: .3rem; }
    .mobile-card-sortbar button { flex: 0 0 auto; min-height: 38px; }

    /* Выбор всех материалов отчёта должен оставаться доступен на телефоне. */
    .wear-material-table > thead { display: block !important; }
    .wear-material-table > thead > tr { display: block !important; }
    .wear-material-table > thead > tr > th { display: none !important; }
    .wear-material-table > thead > tr > th:first-child {
        display: flex !important;
        gap: .6rem;
        align-items: center;
        margin-bottom: .75rem;
        padding: .75rem .9rem;
        background: #f3f8fc !important;
        border: 1px solid var(--corp-border, #d6e3ec);
        border-radius: .6rem;
    }
    .wear-material-table > thead > tr > th:first-child::after {
        content: "Выбрать все на странице";
        font-size: .86rem;
        font-weight: 600;
    }
    .wear-material-table > tbody > tr > td:first-child:not(.mobile-card-empty) {
        order: -2;
        display: flex !important;
        align-items: center;
        gap: .6rem;
        background: #f8fafc !important;
    }
    .wear-material-table > tbody > tr > td:first-child:not(.mobile-card-empty)::before {
        content: "Выбрать материал";
    }
    .wear-material-table > tbody > tr > td.wear-material-percent {
        text-align: left !important;
    }

    /* Матрица износа: каждая строка типа — карточка; выбор филиалов остаётся виден. */
    .wear-matrix {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
        table-layout: auto !important;
    }
    .wear-matrix > thead { display: block !important; }
    .wear-matrix .wear-header-filials {
        display: flex !important;
        flex-wrap: wrap;
        gap: .5rem;
        padding: .65rem;
        margin-bottom: .85rem;
        border: 1px solid var(--corp-border, #d6e3ec);
        border-radius: .65rem;
        background: #f3f8fc;
    }
    .wear-matrix .wear-header-filials > th {
        position: static !important;
        z-index: auto !important;
        display: block !important;
        width: auto !important;
        height: auto !important;
        min-width: 0 !important;
        max-width: 100% !important;
        padding: 0 !important;
        border: 0 !important;
        background: transparent !important;
    }
    .wear-matrix .wear-header-filials > th:first-child { display: none !important; }
    .wear-matrix .wear-header-band-title,
    .wear-matrix .wear-header-bands { display: none !important; }
    .wear-matrix .wear-filial-heading { min-width: 0 !important; }
    .wear-matrix .wear-filial-choice {
        max-width: 100%;
        padding: .5rem .65rem;
        font-size: .85rem;
    }
    .wear-matrix > tbody {
        display: grid !important;
        gap: .85rem;
    }
    .wear-matrix > tbody > tr {
        display: grid !important;
        grid-template-columns: 1fr;
        overflow: hidden;
        min-width: 0;
        background: #fff;
        border: 1px solid var(--corp-border, #d6e3ec);
        border-top: 3px solid var(--corp-blue, #006cb1);
        border-radius: .75rem;
    }
    .wear-matrix > tbody > tr[hidden] { display: none !important; }
    .wear-matrix > tbody > tr > .wear-type-cell {
        position: static !important;
        z-index: auto !important;
        display: block !important;
        min-width: 0 !important;
        max-width: 100% !important;
        width: 100% !important;
        padding: .85rem !important;
        border-bottom: 1px solid #e8eef3;
        background: #f3f8fc !important;
    }
    .wear-matrix > tbody > tr > .wear-value {
        display: grid !important;
        grid-template-columns: minmax(0, 68%) minmax(0, 32%);
        align-items: center;
        min-width: 0 !important;
        width: 100% !important;
        padding: .3rem .65rem !important;
        border-right: 0 !important;
        border-bottom: 1px solid #e8eef3 !important;
        text-align: left !important;
    }
    .wear-matrix > tbody > tr > .wear-value::before {
        content: attr(data-mobile-label);
        padding: .35rem 0;
        font-size: .78rem;
        font-weight: 600;
        overflow-wrap: anywhere;
    }
    .wear-matrix > tbody > tr > .wear-value a {
        min-width: 0 !important;
        min-height: 36px;
        padding: .2rem;
        justify-content: flex-end;
    }
    .wear-matrix .wear-type-inner { padding-left: min(calc(var(--wear-depth) * .7rem), 3rem); }
    .wear-matrix .wear-type-column { min-width: 0 !important; }

    /* Граф перевозок: стартовая точка — карточка, стоимость по сезону — поле. */
    .graph-matrix-container { max-height: none !important; }
    .graph-matrix {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
        table-layout: auto !important;
    }
    .graph-matrix > thead { display: none !important; }
    .graph-matrix > tbody {
        display: grid !important;
        gap: .85rem;
    }
    .graph-matrix > tbody > tr {
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: .5rem;
        min-width: 0;
        padding: .65rem;
        border: 1px solid var(--corp-border, #d6e3ec);
        border-top: 3px solid var(--corp-blue, #006cb1);
        border-radius: .75rem;
        background: #fff;
    }
    .graph-matrix > tbody > tr > th.graph-matrix-sticky {
        grid-column: 1 / -1;
        position: static !important;
        z-index: auto !important;
        display: block !important;
        width: auto !important;
        min-width: 0 !important;
        max-width: none !important;
        padding: .55rem;
        background: #f3f8fc !important;
        font-size: .9rem;
        overflow-wrap: anywhere;
    }
    .graph-matrix > tbody > tr > td {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
        max-width: none !important;
        padding: .55rem !important;
        border: 1px solid #dce6ee !important;
        border-radius: .45rem;
    }
    .graph-matrix > tbody > tr > td::before {
        content: attr(data-mobile-label);
        display: block;
        font-size: .76rem;
        font-weight: 650;
        line-height: 1.4;
        margin-bottom: .45rem;
        overflow-wrap: anywhere;
    }
    .graph-matrix > tbody > tr > td.graph-matrix-diagonal { display: none !important; }
    .graph-matrix .graph-cost-editor,
    .graph-matrix .graph-cost-input { min-width: 0 !important; width: 100%; }
    .graph-matrix .graph-bidirectional-label { white-space: normal; }
}

@media (max-width: 420px) {
    .mobile-card-table > tbody > tr > td:not(.mobile-card-primary):not(.mobile-card-actions):not(.mobile-card-empty) {
        grid-template-columns: minmax(0, 40%) minmax(0, 60%);
        gap: .4rem;
    }
    .graph-matrix > tbody > tr { grid-template-columns: minmax(0,1fr); }
    .graph-matrix > tbody > tr > th.graph-matrix-sticky { grid-column: 1; }
}
