/*
 * Компоновка на планшетах и телефонах. Подключается после site.css.
 * Блоки до 1024, 600 и 380 пикселей задают последовательное упрощение раскладки.
 * Прокрутка матрицы локальная; поля и кнопки рассчитаны на касание.
 */
/* Правила адаптации интерфейса. Печать и экспорт Excel от них не зависят.
   601–1024 px: планшет; до 600 px: телефон; до 380 px: узкий телефон.
   Минимальная область нажатия — 44 px. Поля — 16 px для ввода без зума iOS. */

@media screen and (max-width: 1024px) {
    .app-main, .content-area, .panel, form, .table-scroll {
        min-width: 0;
    }

    .content-area { padding: 20px 16px 28px; }
    .panel { padding: 20px; }
    .panel.panel-table { padding: 0; }
    .topbar .topbar-user { min-width: 0; flex-shrink: 1; }
    .user-name { max-width: 32vw; overflow: hidden; text-overflow: ellipsis; }
    .section-heading { flex-wrap: wrap; gap: 8px; }
    .stage-heading { flex-wrap: nowrap; gap: 12px; }
    h1, h2, .readonly-field, .details dd { overflow-wrap: anywhere; }

    input[type=text], input[type=password], input:not([type]), select, textarea,
    .matrix-table td.fare-cell input, .matrix-km-inline .km-field {
        font-size: 16px;
        min-height: 44px;
    }

    .btn, .mini-button, #grids-table .action-link { min-height: 44px; }
    #grids-table .action-link { width: 44px; height: 44px; }
    .stop-list .action-cell .mini-button { height: 44px; flex-basis: 44px; }
    .boundary-reset { width: 44px; height: 44px; }

    .table-scroll {
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
    }

    .table-scroll:focus-visible { outline: 2px solid #3C5CB4; outline-offset: 2px; }
    .matrix-scroll { isolation: isolate; }
    .matrix-table td.stop-name-cell, .matrix-table thead th:first-child {
        position: sticky;
        left: 0;
        z-index: 2;
        min-width: 140px;
        max-width: 160px;
        overflow-wrap: anywhere;
    }
    .matrix-table thead th:first-child { z-index: 3; }
    .matrix-table td.fare-cell { min-width: 92px; }
    .matrix-table td.fare-cell input { padding: 7px 4px; }

    .wizard-steps {
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 6px;
        padding: 8px;
    }
    .wizard-step { padding: 6px 4px; justify-content: center; min-height: 44px; }
    .wizard-step.active { background: #e1f1fd; }

    .checkbox-line, .city-insert-option {
        display: flex;
        align-items: flex-start;
        gap: 10px;
        min-height: 44px;
        padding: 10px 0;
    }
    input[type=checkbox], input[type=radio] {
        width: 20px;
        height: 20px;
        flex: 0 0 20px;
    }
    .checkbox-line input, .city-insert-option input { margin: 1px 0 0; }
    .field label.checkbox-line { display: flex; }
    #combined-extra-options .checkbox-line { display: flex; }
    .km-toggle { height: 44px; }
    .km-toggle-track { position: absolute; top: 7px; }
    .km-toggle input { width: 1px; height: 1px; }
    .km-view-control { min-height: 44px; margin-bottom: 12px; }

    .import-result .panel { padding: 18px; }
    .import-settings-grid { gap: 16px; }
    .import-tabs button { min-height: 44px; padding: 8px 12px; }
    .import-options > div { gap: 10px; }
    .import-result-main.has-tabs .import-tab-panel { max-height: 440px; }
}

@media screen and (max-width: 600px) {
    .app-shell { padding-top: 64px; padding-top: calc(64px + env(safe-area-inset-top, 0px)); }
    .topbar {
        height: 64px;
        height: calc(64px + env(safe-area-inset-top, 0px));
        padding: 0 12px;
        padding: env(safe-area-inset-top, 0px) 12px 0;
        gap: 12px;
    }
    .topbar-logo { width: 78px; height: 40px; }
    .topbar .topbar-user { flex: 1 1 auto; min-width: 0; line-height: 1.3; }
    .user-avatar { display: inline-flex; flex: 0 0 20px; }
    .user-name {
        flex: 1 1 auto;
        min-width: 0;
        max-width: none;
        font-size: 13px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .topbar-logout { flex: 0 0 auto; margin-left: 10px; }
    .topbar-logout .btn { padding: 9px 10px; min-width: 44px; }

    .content-area { padding: 16px 12px 24px; padding: 16px 12px calc(24px + env(safe-area-inset-bottom, 0px)); }
    h1 { font-size: 21px; line-height: 1.3; }
    h2 { font-size: 17px; }
    .panel { padding: 16px; margin-bottom: 16px; border-radius: 10px; }
    .panel.panel-table { background: transparent; border: 0; box-shadow: none; }
    .headline { display: flex; flex-direction: column; align-items: stretch; gap: 14px; margin-bottom: 16px; }
    .headline .btn { margin-top: 0; }
    .stage-heading { flex-direction: row; flex-wrap: nowrap; gap: 12px; }
    .stage-heading > div, .stage-heading > h1 { flex: 1 1 0; }
    .stage-home-link { min-height: 44px; padding: 10px; }
    .headline > .route-actions { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
    .route-actions form, .route-actions .btn { width: 100%; }
    .section-heading { margin-bottom: 16px; }
    .section-heading h1, .section-heading h2 { margin-bottom: 0; }
    .form-section { margin-top: 20px; padding-top: 18px; }
    .form-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 0; }
    .form-grid .field { min-width: 0; }
    .field { min-width: 0; margin-bottom: 16px; }
    .field.medium { max-width: none; }
    .readonly-field { font-size: 14px; }
    .choice-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
    .choice-item { min-width: 0; min-height: 52px; padding: 10px; gap: 8px; }
    .choice-item:first-child { grid-column: 1 / -1; }
    .checkbox-line { line-height: 1.5; }

    .wizard-steps { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .wizard-step { justify-content: flex-start; gap: 8px; padding: 7px; font-size: 15px; }
    .wizard-step-circle { width: 28px; height: 28px; line-height: 26px; font-size: 15px; }

    .actions, .actions-between {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        gap: 10px;
        margin-top: 18px;
    }
    .actions-between { padding-top: 16px; }
    .actions > .btn { width: 100%; }
    .action-group { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; width: 100%; }
    .action-group > .btn { width: 100%; padding: 10px; }

    .filters-row { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; margin-bottom: 14px; }
    .search-wrapper { max-width: none; width: 100%; min-width: 0; }
    .filters-row .status-select-wrapper { width: 100%; max-width: none; margin-top: 0; }
    .filters-row select { margin-top: 0; }

    /* Список маршрутов — карточки с сохранением всех полей и действий. */
    #grids-table, #grids-table tbody { display: block; }
    #grids-table thead, .stop-list thead { display: none; }
    #grids-table .grid-data-row {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        gap: 8px 12px;
        padding: 14px;
        margin-bottom: 12px;
        background: #fff;
        border: 1px solid #bddff3;
        border-radius: 10px;
    }
    #grids-table .grid-data-row td {
        min-width: 0;
        padding: 0;
        border: 0;
        white-space: normal;
        overflow-wrap: anywhere;
    }
    #grids-table .grid-data-row td:nth-child(2),
    #grids-table .grid-data-row td:nth-child(3) { grid-column: 1 / -1; }
    #grids-table .grid-data-row td:nth-child(2) { font-size: 15px; font-weight: 600; }
    #grids-table .grid-data-row td:nth-child(4),
    #grids-table .grid-data-row td:nth-child(5) { grid-column: 1 / -1; }
    #grids-table td[data-label]::before { content: attr(data-label) ': '; color: #687c98; font-size: 12px; font-weight: 400; }
    #grids-table td:nth-child(2)::before { display: none; }
    #grids-table .grid-data-row .route-actions-cell { grid-column: 2; grid-row: 1; width: auto; }
    #grids-table .route-actions { gap: 8px; }
    #grids-table .route-actions form { width: auto; }
    #grids-table .empty-cell { display: block; white-space: normal; }

    /* TR остаются в DOM: добавление, удаление и Enter работают как раньше. */
    .stop-list, .stop-list tbody { display: block; width: 100%; }
    .stop-list tbody { display: grid; gap: 12px; }
    .stop-list tbody tr {
        display: grid;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        gap: 12px;
        padding: 12px;
        border: 1px solid #bddff3;
        border-radius: 10px;
    }
    .stop-list tbody tr.add-stop-row { background: #e9f6fd; border-style: dashed; }
    .stop-list tbody tr td {
        min-width: 0;
        width: auto;
        border: 0;
        padding: 0;
        background: transparent;
        white-space: normal;
        text-align: left;
    }
    .stop-list tbody tr td:not(:nth-child(2)) { width: auto; white-space: normal; }
    .stop-list tbody td:nth-child(1), .stop-list tbody td:nth-child(2), .stop-list tbody td:last-child { grid-column: 1 / -1; }
    .stop-list tbody td:nth-child(1) { font-weight: 600; color: #3C5CB4; }
    .stop-list td[data-label]::before { content: attr(data-label); display: block; color: #5d7190; font-size: 12px; margin-bottom: 5px; }
    .stop-list td:nth-child(1)::before { display: inline; margin-right: 5px; }
    .stop-list tbody td input[name^=name_], .stop-list tbody td #new_stop,
    .stop-list tbody td .km-field { width: 100%; min-width: 0; max-width: none; min-height: 44px; }
    .stop-list .badge { white-space: normal; }
    .stop-actions { flex-wrap: wrap; justify-content: flex-end; gap: 6px; }
    .stop-list .action-cell .btn { flex: 1 1 100%; margin-right: 0; padding: 9px 10px; min-height: 44px; }
    .stop-list .action-cell .mini-button { flex-grow: 1; }
    .stop-list .add-stop-row td:last-child .btn { width: 100%; }
    .stop-list .add-stop-row td:nth-child(4) { display: none; }
    .boundary-badge { min-height: 44px; padding: 0 0 0 8px; }

    .km-view-control { justify-content: space-between; gap: 12px; }
    .km-view-label { font-size: 17px; }
    .matrix-section-heading { margin-top: 18px; }
    .matrix-legend { font-size: 13px; }
    .legend-item { align-items: flex-start; }
    .legend-swatch { flex-shrink: 0; }

    .upload-dropzone .upload-area { padding: 24px 14px; }
    .upload-dropzone .upload-area h2 { font-size: 18px; }
    .upload-description { font-size: 13px; margin-bottom: 16px; }
    .upload-select-button { display: inline-flex; min-height: 44px; align-items: center; justify-content: center; }
    .import-overview-heading { gap: 16px; }
    .import-overview-heading > div:first-child { flex-basis: 100%; }
    .import-overview h1 { font-size: 20px; }
    .import-file-info { gap: 12px; }
    .import-primary-actions { width: 100%; display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
    .import-result-main.has-tabs .import-tab-panel { max-height: 430px; min-height: 0; }
    .import-tabs { margin-bottom: 14px; }
    .import-tabs button { flex-shrink: 0; }
    .import-stop-list { max-height: 280px; }

    .auth-shell { padding: 20px 12px; min-height: 100vh; min-height: 100svh; }
    .login-card { width: 100%; padding: 24px 18px; }
    .login-logo { width: 180px; height: 60px; margin-bottom: 20px; }
    .login-card .btn { width: 100%; }
    .technical-error-card { padding: 24px 18px; }

    .modal-overlay { overflow-y: auto; padding: 16px; overscroll-behavior-y: contain; }
    .modal-window, .modal-window.stop-delete-window, .modal-window.matrix-validation-window {
        width: 100%;
        margin: 4vh auto;
        padding: 20px;
        max-height: calc(100vh - 64px);
        max-height: calc(100dvh - 64px);
        overflow-y: auto;
    }
    .modal-window h2 { font-size: 19px; overflow-wrap: anywhere; }
    .modal-buttons { gap: 10px; margin-top: 20px; }
    .modal-buttons .btn { padding: 10px 8px; min-width: 0; }
    .snackbar-container {
        top: 76px;
        top: calc(76px + env(safe-area-inset-top, 0px));
        left: 12px;
        right: 12px;
        width: auto;
        max-height: 45vh;
        max-height: 45dvh;
        overflow-y: auto;
        pointer-events: none;
    }
    .snackbar { pointer-events: auto; padding-right: 56px; }
    .snackbar-close { min-width: 44px; min-height: 44px; }
}

@media screen and (max-width: 380px) {
    .topbar-logo { width: 62px; }
    .topbar-logout .btn span { display: none; }
    .topbar-logout { margin-left: 6px; }
    .stop-list .action-cell .btn { flex: 1 1 100%; margin-right: 0; }
    .stop-actions .mini-button { flex-grow: 1; }
    .modal-buttons { flex-direction: column; }
    .modal-buttons .btn { width: 100%; flex-basis: auto; }
}

@media screen and (max-height: 500px) and (max-width: 1024px) {
    .modal-overlay { overflow-y: auto; padding: 12px; }
    .modal-window, .modal-window.stop-delete-window, .modal-window.matrix-validation-window {
        max-height: calc(100vh - 24px);
        max-height: calc(100dvh - 24px);
        overflow-y: auto;
        margin: 0 auto;
    }
    .auth-shell { align-items: flex-start; }
}
