/* Мобильная адаптация SotaWeb Platform */

button,
input,
select,
textarea {
    touch-action: manipulation;
}

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

.mobile-menu-btn,
.mobile-sidebar-head,
.mobile-sidebar-backdrop {
    display: none;
}

.header-brand {
    min-width: 0;
}

@media (min-width: 769px) and (max-width: 1024px) {
    .grid-3,
    .grid-4 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .toolbar {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        align-items: end;
    }

    .toolbar .field,
    .toolbar > .btn {
        width: 100%;
        min-width: 0;
    }

    .report-filters {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 1024px) {
    html,
    body {
        width: 100%;
        max-width: 100%;
        overflow-x: hidden;
    }

    .table-wrap,
    .report-table-wrap,
    .ws-cycle-table-wrap {
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
    }

    body.mobile-menu-open {
        overflow: hidden;
        overscroll-behavior: none;
    }

    .app-header {
        position: sticky;
        top: 0;
        z-index: 1000;
        display: grid;
        grid-template-columns: 44px minmax(0, 1fr);
        gap: 8px 10px;
        padding: 10px 12px;
        min-height: auto;
    }

    .mobile-menu-btn {
        display: inline-flex;
        width: 44px;
        height: 44px;
        align-items: center;
        justify-content: center;
        border: 1px solid #374151;
        border-radius: 10px;
        background: #1e293b;
        color: #ffffff;
        font-size: 25px;
        line-height: 1;
        cursor: pointer;
    }

    .app-title {
        font-size: 19px;
        line-height: 1.15;
    }

    .app-subtitle {
        display: none;
    }

    .header-right {
        grid-column: 1 / -1;
        display: grid;
        grid-template-columns: 66px minmax(0, 1fr) 76px;
        width: 100%;
        gap: 8px;
    }

    .header-right select,
    .header-right .btn {
        width: 100%;
        min-width: 0;
        min-height: 40px;
        padding: 8px 9px;
        font-size: 13px;
    }

    #currentUser {
        display: none;
    }

    .layout {
        display: block;
        min-height: calc(100dvh - 104px);
    }

    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 1201;
        width: min(86vw, 340px);
        height: 100dvh;
        max-height: 100dvh !important;
        padding: 0 14px 24px;
        overflow-y: auto;
        overscroll-behavior: contain;
        transform: translateX(-105%);
        transition: transform 0.22s ease;
        box-shadow: 16px 0 40px rgba(0, 0, 0, 0.28);
    }

    body.mobile-menu-open .sidebar {
        transform: translateX(0);
    }

    .mobile-sidebar-head {
        position: sticky;
        top: 0;
        z-index: 2;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        margin: 0 -14px 16px;
        padding: max(12px, env(safe-area-inset-top)) 14px 12px;
        background: #0f172a;
        border-bottom: 1px solid rgba(255, 255, 255, 0.12);
    }

    .mobile-sidebar-title {
        font-weight: 800;
        font-size: 17px;
    }

    .mobile-sidebar-close {
        width: 40px;
        height: 40px;
        border: 1px solid rgba(255, 255, 255, 0.2);
        border-radius: 10px;
        background: #1e293b;
        color: #ffffff;
        font-size: 27px;
        line-height: 1;
        cursor: pointer;
    }

    .mobile-sidebar-backdrop {
        position: fixed;
        inset: 0;
        z-index: 1200;
        display: block;
        background: rgba(15, 23, 42, 0.58);
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transition: opacity 0.2s ease, visibility 0.2s ease;
    }

    body.mobile-menu-open .mobile-sidebar-backdrop {
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
    }

    .sidebar-group {
        margin-bottom: 14px;
    }

    .menu-btn {
        min-height: 44px;
        padding: 12px 14px;
    }

    .content {
        width: 100%;
        max-width: 100%;
        padding: 12px;
        overflow-x: hidden;
    }

    .card,
    .kpi,
    .module-card {
        max-width: 100%;
        padding: 13px;
        border-radius: 12px;
        box-shadow: 0 4px 14px rgba(0, 0, 0, 0.06);
    }

    .page-title {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
    }

    .page-title h2 {
        font-size: 21px;
        line-height: 1.2;
    }

    .page-title > .btn,
    .page-title > div:last-child:not(:first-child) {
        width: 100%;
    }

    .page-title > div:last-child:not(:first-child) {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }

    .page-title > div:last-child:not(:first-child) .btn {
        flex: 1 1 160px;
    }

    .report-actions {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
    }

    .report-actions .btn {
        width: 100%;
    }

    .field {
        width: 100%;
        min-width: 0;
    }

    .btn {
        min-height: 42px;
    }

    .table-wrap,
    .report-table-wrap {
        width: 100%;
        max-width: calc(100vw - 24px);
        overflow-x: auto;
        overflow-y: auto;
    }

    .table-wrap table,
    .report-table-wrap table {
        width: max-content;
        min-width: 760px;
    }

    .table-wrap th,
    .table-wrap td,
    .report-table-wrap th,
    .report-table-wrap td {
        padding: 8px 9px;
        font-size: 13px;
    }

    .report-table-wrap {
        max-height: calc(100dvh - 300px);
    }

    .monitoring-layout,
    .monitoring-kpi-row {
        grid-template-columns: 1fr;
    }

    pre,
    code,
    .hardware-json-payload {
        max-width: 100%;
        white-space: pre-wrap;
        overflow-wrap: anywhere;
        word-break: break-word;
    }
}

@media (max-width: 768px) {
    .login-wrap {
        min-height: 100dvh;
        padding: 12px;
        align-items: center;
    }

    .login-card {
        padding: 20px 16px;
        border-radius: 15px;
    }

    .login-title {
        font-size: 25px;
    }

    input,
    select,
    textarea {
        font-size: 16px !important;
    }

    .grid-2,
    .grid-3,
    .grid-4,
    .report-filters,
    .toolbar {
        grid-template-columns: 1fr !important;
    }

    .toolbar {
        display: grid;
        align-items: stretch;
    }

    .toolbar .field,
    .toolbar > .btn,
    .toolbar > select,
    .toolbar > input {
        width: 100% !important;
        min-width: 0 !important;
    }

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

    .kpi-value {
        font-size: 24px;
    }

    .table-wrap table,
    .report-table-wrap table {
        min-width: 680px;
    }

    .sota-modal-overlay {
        align-items: stretch !important;
        justify-content: flex-start !important;
        padding: 0 !important;
    }

    .sota-modal {
        width: 100vw !important;
        max-width: 100vw !important;
        height: 100vh !important;
        height: 100dvh !important;
        max-height: 100vh !important;
        max-height: 100dvh !important;
        border: 0 !important;
        border-radius: 0 !important;
        display: flex !important;
        flex-direction: column !important;
    }

    .sota-modal-header {
        flex: 0 0 auto !important;
        padding: max(12px, env(safe-area-inset-top)) 14px 12px !important;
    }

    .sota-modal-title {
        font-size: 17px !important;
        line-height: 1.25 !important;
    }

    .sota-modal-close {
        width: 42px !important;
        height: 42px !important;
        flex: 0 0 42px !important;
    }

    .sota-modal-body {
        flex: 1 1 auto !important;
        min-height: 0 !important;
        max-height: none !important;
        padding: 12px !important;
        overflow-y: auto !important;
        overscroll-behavior: contain;
    }

    .sota-modal-grid,
    .role-modal-grid,
    .user-main-layout,
    .user-access-row,
    .sota-inline-row,
    .access-top-form,
    .ws-top-grid,
    .ws-form-grid,
    .ws-cycle-grid {
        grid-template-columns: 1fr !important;
    }

    .sota-modal-actions {
        position: sticky !important;
        bottom: -12px !important;
        z-index: 20 !important;
        margin: 18px -12px -12px !important;
        padding: 12px 12px calc(12px + env(safe-area-inset-bottom)) !important;
        background: #ffffff !important;
        border-top: 1px solid #e5e7eb !important;
        box-shadow: 0 -8px 18px rgba(15, 23, 42, 0.08) !important;
    }

    .sota-modal-actions .btn {
        flex: 1 1 140px !important;
        min-height: 44px !important;
    }

    .user-photo-card {
        width: 100% !important;
        max-width: none !important;
        margin: 0 !important;
    }

    .user-photo-frame {
        width: min(210px, 100%) !important;
        height: 250px !important;
    }

    .user-enroll-actions {
        grid-template-columns: 1fr !important;
    }

    .access-fixed-modal {
        height: auto !important;
        overflow: visible !important;
    }

    .access-transfer-grid,
    .role-transfer-grid,
    .ws-transfer-grid {
        grid-template-columns: 1fr !important;
        height: auto !important;
    }

    .access-list-box,
    .role-list-box,
    .ws-list-box,
    .ws-list-box.ws-list-small {
        height: 180px !important;
        min-height: 150px !important;
    }

    .access-transfer-buttons,
    .role-transfer-buttons,
    .ws-transfer-buttons {
        flex-direction: row !important;
        justify-content: flex-start !important;
    }

    .access-transfer-buttons button,
    .role-transfer-buttons button,
    .ws-transfer-buttons button {
        flex: 1 1 0 !important;
        min-height: 40px !important;
    }

    .work-schedule-modal {
        width: 100% !important;
        height: auto !important;
        min-height: 0 !important;
        max-width: none !important;
        max-height: none !important;
        overflow: visible !important;
    }

    .ws-tabs {
        position: sticky !important;
        top: 0 !important;
        z-index: 10 !important;
        margin-left: -12px !important;
        margin-right: -12px !important;
        padding: 0 12px !important;
        background: #ffffff !important;
    }

    .ws-tab-content {
        overflow: visible !important;
        padding-right: 0 !important;
    }

    .ws-cycle-table-wrap {
        max-width: calc(100vw - 24px) !important;
        max-height: 52dvh !important;
    }

    .monitoring-photo-large {
        height: 220px;
    }
}

@media (max-width: 420px) {
    .app-header {
        padding-left: 8px;
        padding-right: 8px;
    }

    .header-right {
        grid-template-columns: 58px minmax(0, 1fr) 68px;
        gap: 6px;
    }

    .header-right select,
    .header-right .btn {
        padding-left: 6px;
        padding-right: 6px;
        font-size: 12px;
    }

    .content {
        padding: 8px;
    }

    .table-wrap,
    .report-table-wrap {
        max-width: calc(100vw - 16px);
    }

    .card,
    .kpi,
    .module-card {
        padding: 11px;
    }

    .page-title h2 {
        font-size: 19px;
    }
}

/* Компактные фильтры и улучшенные таблицы */
@media (max-width: 768px) {
    .mobile-filter-group {
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 7px !important;
        width: 100% !important;
        align-items: stretch !important;
        margin-bottom: 10px !important;
    }

    .mobile-filter-group > input,
    .mobile-filter-group > select,
    .mobile-filter-group > button,
    .mobile-filter-group > .btn {
        width: 100% !important;
        min-width: 0 !important;
        min-height: 38px !important;
        margin: 0 !important;
        padding: 7px 9px !important;
        border-radius: 9px !important;
    }

    /* Длинные значения фильтров должны читаться полностью. */
    .mobile-filter-group > input[type="text"],
    .mobile-filter-group > select,
    .mobile-filter-group > .field:has(input[type="text"]),
    .mobile-filter-group > .field:has(select) {
        grid-column: 1 / -1;
    }

    .mobile-filter-group select {
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .mobile-filter-group .field {
        width: 100% !important;
        min-width: 0 !important;
        gap: 4px !important;
    }

    .mobile-filter-group .field label {
        font-size: 11px !important;
    }

    .mobile-filter-group .field input,
    .mobile-filter-group .field select {
        min-height: 38px !important;
        padding: 7px 9px !important;
    }

    .mobile-filter-group .btn {
        font-size: 13px !important;
    }

    .table-wrap,
    .report-table-wrap,
    .ws-cycle-table-wrap {
        position: relative;
        border-radius: 10px;
        box-shadow: inset -10px 0 12px -14px rgba(15, 23, 42, .75);
    }

    .table-wrap.mobile-scroll-end,
    .report-table-wrap.mobile-scroll-end,
    .ws-cycle-table-wrap.mobile-scroll-end {
        box-shadow: inset 10px 0 12px -14px rgba(15, 23, 42, .75);
    }

    .mobile-scroll-table th,
    .mobile-scroll-table td {
        padding: 7px 8px !important;
        font-size: 12px !important;
        line-height: 1.3 !important;
    }

    .mobile-scroll-table th:first-child,
    .mobile-scroll-table td:first-child {
        position: sticky;
        left: 0;
        z-index: 4;
        background: #fff;
        box-shadow: 7px 0 12px -12px rgba(15, 23, 42, .75);
    }

    .mobile-scroll-table th:first-child {
        background: #f8fafc;
        z-index: 6;
    }

    .mobile-scroll-table .btn {
        min-height: 34px !important;
        padding: 6px 8px !important;
        font-size: 12px !important;
    }
}

@media (max-width: 640px) {
    .table-wrap:has(.mobile-card-table) {
        max-width: 100% !important;
        overflow: visible !important;
        border: 0 !important;
        border-radius: 0 !important;
        box-shadow: none !important;
        background: transparent !important;
    }

    .mobile-card-table,
    .mobile-card-table tbody {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
        background: transparent !important;
    }

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

    .mobile-card-table tbody {
        display: grid !important;
        gap: 9px !important;
    }

    .mobile-card-table tbody tr {
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        width: 100% !important;
        overflow: hidden !important;
        border: 1px solid #d9dee8 !important;
        border-radius: 12px !important;
        background: #fff !important;
        box-shadow: 0 3px 10px rgba(15, 23, 42, .06) !important;
    }

    .mobile-card-table tbody td {
        display: flex !important;
        flex-direction: column !important;
        justify-content: flex-start !important;
        gap: 3px !important;
        min-width: 0 !important;
        padding: 8px 10px !important;
        border: 0 !important;
        border-bottom: 1px solid #eef2f7 !important;
        white-space: normal !important;
        overflow-wrap: anywhere !important;
        font-size: 13px !important;
        line-height: 1.35 !important;
    }

    .mobile-card-table tbody td::before {
        content: attr(data-label);
        display: block;
        color: #64748b;
        font-size: 10px;
        font-weight: 800;
        line-height: 1.2;
        text-transform: uppercase;
        letter-spacing: .25px;
    }

    .mobile-card-table tbody td.mobile-full-cell {
        grid-column: 1 / -1 !important;
    }

    .mobile-card-table tbody td.mobile-actions-cell {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: wrap !important;
        align-items: center !important;
        gap: 6px !important;
        background: #f8fafc !important;
    }

    .mobile-card-table tbody td.mobile-actions-cell::before {
        flex: 0 0 100%;
    }

    .mobile-card-table tbody td.mobile-actions-cell .btn,
    .mobile-card-table tbody td.mobile-actions-cell button {
        flex: 1 1 110px !important;
        min-height: 36px !important;
        padding: 6px 8px !important;
        font-size: 12px !important;
    }

    .mobile-card-table tbody tr.mobile-empty-row {
        display: block !important;
    }

    .mobile-card-table tbody tr.mobile-empty-row td {
        display: block !important;
        padding: 14px !important;
        text-align: center !important;
        border: 0 !important;
    }

    .mobile-card-table tbody tr.mobile-empty-row td::before {
        display: none !important;
    }

    .user-webcam-panel {
        padding: 0 !important;
        align-items: stretch !important;
    }

    .user-webcam-box {
        width: 100vw !important;
        max-width: 100vw !important;
        height: 100vh !important;
        height: 100dvh !important;
        max-height: 100vh !important;
        max-height: 100dvh !important;
        overflow: hidden !important;
        border-radius: 0 !important;
        padding: max(10px, env(safe-area-inset-top)) 10px calc(10px + env(safe-area-inset-bottom)) !important;
        display: flex !important;
        flex-direction: column !important;
    }

    .user-webcam-header {
        flex: 0 0 auto !important;
        margin-bottom: 8px !important;
    }

    .user-webcam-title {
        font-size: 17px !important;
    }

    .user-webcam-subtitle {
        font-size: 12px !important;
    }

    .user-webcam-layout {
        flex: 1 1 auto !important;
        min-height: 0 !important;
        display: block !important;
    }

    .user-webcam-stage {
        width: 100% !important;
        height: 100% !important;
        min-height: 0 !important;
        max-height: none !important;
        aspect-ratio: auto !important;
        border-radius: 12px !important;
    }

    .user-webcam-side {
        display: none !important;
    }

    .user-webcam-actions {
        flex: 0 0 auto !important;
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 7px !important;
        margin-top: 8px !important;
    }

    .user-webcam-actions .btn {
        width: 100% !important;
        min-height: 44px !important;
        padding: 8px 10px !important;
        font-size: 13px !important;
    }
}

/* Видимые подписи полей периода в событиях системы и проходов */
.event-date-field {
    min-width: 145px;
}

.event-date-field input[type="date"] {
    min-width: 0;
    width: 100%;
    color: #111827;
    background: #ffffff;
}

@media (max-width: 768px) {
    .mobile-filter-group > .event-date-field {
        grid-column: auto;
        min-width: 0 !important;
    }

    .event-date-field label {
        display: block !important;
        min-height: 14px;
        color: #4b5563 !important;
        font-size: 11px !important;
        line-height: 1.2;
    }

    .event-date-field input[type="date"] {
        min-height: 40px !important;
        padding: 7px 8px !important;
    }
}
