/* NoCrm Mobile — портрет + альбомная ориентация телефонов */

@media (max-width: 768px), (max-height: 500px) {

    /* Вариант C: чип даты + город */
    .mobile-date-trigger {
        display: inline-flex !important;
        align-items: center;
        gap: 6px;
        padding: 8px 12px;
        border: 1px solid var(--nc-line);
        border-radius: var(--nc-r);
        background: var(--nc-surface-2);
        color: var(--nc-text);
        font-size: 12px;
        font-weight: 600;
        flex-shrink: 0;
        white-space: nowrap;
    }

    .mobile-date-trigger-caret {
        font-size: 10px;
        color: var(--nc-muted);
    }

    body.mobile-dates-sheet .calendar-wrapper .calendar-container,
    body.mobile-dates-sheet .calendar-wrapper .quick-dates {
        display: none !important;
    }

    body.mobile-dates-sheet .calendar-wrapper {
        flex: 0 0 auto;
    }

    body.mobile-dates-sheet .city-selector-container {
        flex: 1;
        min-width: 0;
    }

    body.mobile-dates-sheet .city-selector-container select {
        max-width: none !important;
        width: 100%;
        font-size: 12px;
    }

    #mainApp > .page-toolbar.filter-strip {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 8px;
        padding: 8px 12px;
        overflow: visible;
        flex-shrink: 0;
    }

    .city-selector-container {
        flex: 1;
        min-width: 0;
    }

    .city-selector-container select,
    .city-selector-container .modern-select {
        width: 100%;
        max-width: none;
    }

    /* Карточки */
    .nc-order,
    .order-item {
        flex-direction: column;
    }

    .nc-order-actions,
    .order-status-wrapper {
        max-width: none;
        width: 100%;
        padding: 8px 14px 12px;
        border-top: 1px solid var(--nc-line);
        justify-content: flex-start;
    }

    .order-main-info {
        padding: 12px;
    }

    /* Нижняя навигация */
    .bottom-nav {
        display: flex !important;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 300;
        height: calc(60px + env(safe-area-inset-bottom, 0px));
        padding-bottom: env(safe-area-inset-bottom, 0px);
        background: var(--nc-surface);
        border-top: 1px solid var(--nc-line);
        align-items: center;
        justify-content: space-around;
    }

    .bottom-nav .nav-btn {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 2px;
        border: none;
        background: transparent;
        color: var(--nc-muted);
        font-size: 10px;
        font-weight: 600;
        padding: 4px 8px;
        min-width: 52px;
    }

    .bottom-nav .nav-btn .bn-ic {
        font-size: 18px;
        line-height: 1;
    }

    .bottom-nav .nav-btn.active {
        color: var(--nc-accent);
    }

    /* FAB */
    .fab,
    .add-btn.fab {
        position: fixed !important;
        right: 16px;
        bottom: calc(70px + env(safe-area-inset-bottom, 0px));
        width: 48px;
        height: 48px;
        font-size: 24px;
        z-index: 280;
    }

    /* Шторки */
    .mobile-menu-sheet.show,
    .mobile-dates-sheet.show {
        display: block !important;
    }

    .mobile-menu-sheet,
    .mobile-dates-sheet {
        position: fixed;
        inset: 0;
        z-index: 400;
    }

    .mobile-menu-sheet-backdrop,
    .mobile-dates-sheet-backdrop {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: calc(60px + env(safe-area-inset-bottom, 0px));
        background: rgba(0, 0, 0, 0.45);
    }

    .mobile-menu-sheet-panel,
    .mobile-dates-sheet-panel {
        position: absolute;
        left: 0;
        right: 0;
        bottom: calc(60px + env(safe-area-inset-bottom, 0px));
        max-height: min(72dvh, 520px);
        background: var(--nc-surface);
        border: 1px solid var(--nc-line);
        border-radius: var(--nc-r-lg) var(--nc-r-lg) 0 0;
        padding: 0 0 calc(12px + env(safe-area-inset-bottom, 0px));
        display: flex;
        flex-direction: column;
        animation: ncSheetUp 0.22s ease;
    }

    @keyframes ncSheetUp {
        from { transform: translateY(100%); }
        to { transform: translateY(0); }
    }

    .mobile-menu-sheet-head,
    .mobile-dates-sheet-head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 14px 16px;
        border-bottom: 1px solid var(--nc-line);
        flex-shrink: 0;
    }

    .mobile-menu-sheet-title,
    .mobile-dates-sheet-head span {
        font-size: 15px;
        font-weight: 700;
    }

    .mobile-menu-sheet-close,
    .mobile-dates-sheet-head button {
        width: 32px;
        height: 32px;
        border: none;
        border-radius: 50%;
        background: var(--nc-surface-2);
        color: var(--nc-text);
        font-size: 20px;
        cursor: pointer;
    }

    .mobile-menu-sheet-body {
        overflow-y: auto;
        padding: 8px;
        display: flex;
        flex-direction: column;
        gap: 2px;
        -webkit-overflow-scrolling: touch;
    }

    .mobile-menu-sheet-body button {
        display: flex;
        align-items: center;
        gap: 10px;
        width: 100%;
        text-align: left;
        padding: 12px 14px;
        border: none;
        background: transparent;
        color: var(--nc-text);
        font-size: 14px;
        font-weight: 500;
        border-radius: var(--nc-r);
        cursor: pointer;
    }

    .mobile-menu-sheet-body button:active {
        background: var(--nc-surface-2);
    }

    .mobile-menu-logout {
        color: var(--nc-red) !important;
        margin-top: 4px;
        border-top: 1px solid var(--nc-line);
        border-radius: 0;
        padding-top: 14px !important;
    }

    .mobile-dates-sheet-mount {
        padding: 12px 16px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .mobile-dates-sheet-mount .quick-dates {
        display: flex !important;
        gap: 6px;
        flex-wrap: nowrap;
        width: max-content;
        min-width: 100%;
    }

    .mobile-dates-sheet-calendar-btn {
        margin: 0 16px 8px;
        padding: 12px;
        border: none;
        border-radius: var(--nc-r);
        background: var(--nc-accent-soft);
        color: var(--nc-accent);
        font-size: 13px;
        font-weight: 700;
        cursor: pointer;
    }

    body.mobile-menu-open,
    body.mobile-dates-sheet-open {
        overflow: hidden;
    }

    body.mobile-menu-open #contentArea,
    body.mobile-dates-sheet-open #contentArea {
        overflow: hidden !important;
    }

    /* Модалки — фиксированная шапка, только body скроллится вертикально */
    .modal-overlay {
        overflow: hidden;
        overscroll-behavior: none;
    }

    .modal-overlay.show {
        align-items: flex-end;
        padding: 0;
    }

    .modal {
        width: 100%;
        max-width: 100%;
        max-height: min(92dvh, calc(100dvh - env(safe-area-inset-top, 0px)));
        margin: 0;
        border-radius: var(--nc-r-lg) var(--nc-r-lg) 0 0;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        flex-shrink: 0;
        touch-action: pan-y;
    }

    .modal-header {
        flex-shrink: 0;
    }

    .modal-header h3 {
        font-size: 16px;
        line-height: 1.3;
        padding-right: 8px;
        word-break: break-word;
    }

    .modal-body {
        flex: 1 1 auto;
        min-height: 0;
        overflow-x: hidden;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-y: contain;
        touch-action: pan-y;
        padding: 16px;
        padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    }

    .modal-body .form-grid {
        grid-template-columns: 1fr;
        width: 100%;
        min-width: 0;
    }

    .modal-body .form-grid > *,
    .modal-body input,
    .modal-body select,
    .modal-body textarea,
    .modal-body .modern-select {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        box-sizing: border-box;
    }

    .modal-body textarea {
        resize: vertical;
        max-width: 100%;
    }

    .modal-body .modal-actions,
    .modal-body .appeal-buttons,
    .modal-body .cancel-options {
        width: 100%;
        min-width: 0;
    }

    /* Без авто-зума iOS при фокусе на полях */
    html {
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }

    body.nocrm {
        touch-action: pan-x pan-y;
    }

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

    .search-container input,
    #searchInput,
    .app-search-global input {
        font-size: 16px !important;
    }

    .form-grid {
        grid-template-columns: 1fr;
    }

    .roi-statistics-filters {
        grid-template-columns: 1fr;
    }

    .period-picker,
    .date-range-v5 {
        flex-wrap: wrap;
        width: 100%;
    }

    .admin-card-head {
        flex-direction: column;
        align-items: stretch;
    }

    .master-actions,
    .manager-actions {
        width: 100%;
    }

    .filter-panel-v5 {
        flex-direction: column;
        align-items: stretch;
    }
}

/* Компактный режим — альбомная ориентация телефона */
@media (max-height: 500px) and (max-width: 1024px) {
    html,
    body.nocrm {
        height: 100dvh;
        max-height: 100dvh;
        overflow: hidden;
    }

    .app-shell,
    .app-main,
    #mainApp.screen.active {
        flex: 1 1 auto;
        min-height: 0;
        max-height: 100%;
        overflow: hidden;
    }

    #contentArea {
        flex: 1 1 auto;
        min-height: 0;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-y: contain;
        touch-action: pan-y;
    }

    body.modal-scroll-locked #contentArea,
    body.mobile-menu-open #contentArea,
    body.mobile-dates-sheet-open #contentArea {
        overflow: hidden !important;
    }

    .modal-body {
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-y;
        overscroll-behavior-y: contain;
    }

    .app-topbar {
        min-height: 0;
        padding: 6px 12px;
        gap: 6px;
        grid-template-columns: 1fr auto auto;
        grid-template-rows: auto;
    }

    .app-topbar .mobile-header-brand {
        grid-column: 1;
        grid-row: 1;
    }

    .app-topbar .topbar-actions {
        grid-column: 2;
        grid-row: 1;
    }

    .app-topbar .app-search-global,
    .app-topbar .search-container {
        grid-column: 1 / -1;
        grid-row: 2;
    }

    .app-topbar .mobile-header-brand h1 {
        font-size: 15px;
    }

    #mainApp > .page-toolbar.filter-strip {
        padding: 6px 12px;
    }

    .content-screen {
        padding: 8px 12px calc(56px + env(safe-area-inset-bottom, 0px));
    }

    .bottom-nav {
        height: calc(48px + env(safe-area-inset-bottom, 0px));
    }

    .bottom-nav .nav-btn {
        font-size: 9px;
        min-width: 44px;
        padding: 2px 4px;
    }

    .bottom-nav .nav-btn .bn-ic {
        font-size: 16px;
    }

    .fab,
    .add-btn.fab {
        width: 44px;
        height: 44px;
        font-size: 22px;
        bottom: calc(56px + env(safe-area-inset-bottom, 0px));
    }

    .mobile-menu-sheet-backdrop,
    .mobile-dates-sheet-backdrop,
    .mobile-menu-sheet-panel,
    .mobile-dates-sheet-panel {
        bottom: calc(48px + env(safe-area-inset-bottom, 0px));
    }

    .mobile-menu-sheet-panel,
    .mobile-dates-sheet-panel {
        max-height: min(80dvh, 420px);
    }
}
