/*
 * Final mobile cascade for all authenticated application screens.
 * Loaded from header.php after page-specific styles.
 */

@media (max-width: 900px) {
    html,
    body.app-body {
        width: 100%;
        max-width: 100%;
        overflow-x: clip;
    }

    .app-shell,
    main.app-content.content,
    .app-content > * {
        min-width: 0;
        max-width: 100%;
    }

    main.app-content.content {
        width: 100%;
        height: auto !important;
        min-height: calc(100dvh - var(--topbar-h, 48px));
        max-height: none !important;
        overflow: visible !important;
        padding-bottom: max(24px, env(safe-area-inset-bottom, 0px)) !important;
    }

    .app-content :is(img, video, canvas, svg) {
        max-width: 100%;
    }

    .app-content :is(.table-wrap, .table-scroll, .table-scroll-wrap,
        .table-responsive, .grid-container, .dataTables_wrapper,
        .curr-table-scroll, .mas-table-wrap, .je-table-scroll,
        .lce-table-scroll, .gl-table-scroll, .ua-table-scroll) {
        max-width: 100%;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    .app-content :is(.modal, .modal-dialog, .modal-content, .dialog,
        .popup, .drawer, .email-modal-card, .rev-modal-card) {
        max-width: calc(100vw - 16px);
    }
}

@media (max-width: 600px) {
    .app-topbar {
        min-height: 56px;
    }

    .app-topbar .topbar-row-main {
        min-width: 0;
        gap: 6px;
    }

    .topbar-right {
        min-width: 0;
        gap: 4px;
    }

    .topbar-right .topbar-user,
    .topbar-right #helpTopbarOpen,
    .topbar-right a[href="help_center.php"],
    .topbar-right a[href="my_profile.php"] {
        display: none;
    }

    main.app-content.content {
        padding: 8px 8px max(24px, env(safe-area-inset-bottom, 0px)) !important;
    }

    .app-content :is(.card, .panel, .box, .admin-card, .tx-card) {
        min-width: 0;
        max-width: 100%;
    }

    .app-content :is(.page-header, .page-toolbar, .toolbar-row,
        .filter-row, .filter-grid, .filter-actions, .detail-actions,
        .admin-actions, .header-actions, .section-head, .actions) {
        flex-wrap: wrap;
        min-width: 0;
    }

    .app-content .filter-grid,
    .app-content .filter-row,
    .app-content .form-grid:not(.fxn-customer-grid),
    .app-content .two-column,
    .app-content .two-col,
    .app-content .split-layout {
        grid-template-columns: minmax(0, 1fr) !important;
    }

    .app-content :is(input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]), select, textarea) {
        max-width: 100%;
        font-size: 16px;
    }

    .app-content :is(.filter-actions, .detail-actions, .admin-actions, .actions) > :is(button, .btn, a.btn) {
        flex: 1 1 140px;
        min-height: 42px;
    }

    .app-content table {
        max-width: none;
    }

    .app-content :is(.modal, .modal-dialog, .modal-content, .dialog,
        .popup, .drawer, .email-modal-card, .rev-modal-card) {
        width: calc(100vw - 16px);
        max-height: calc(100dvh - 16px);
        overflow: auto;
    }

    /* Forex Entry's live screen is the workstation, not the hidden legacy form. */
    body.forex-entry-new main.app-content.content {
        height: auto !important;
        min-height: calc(100dvh - var(--topbar-h, 56px));
        overflow: visible !important;
    }

    body.forex-entry-new .fxn-workstation {
        display: flex !important;
        flex-direction: column;
        height: auto !important;
        min-height: 0 !important;
        gap: 8px !important;
        overflow: visible !important;
    }

    .forex-entry-new .fxn-receipt-bar {
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 6px !important;
    }

    .forex-entry-new .fxn-receipt-find {
        grid-column: 1 / -1 !important;
        grid-template-columns: auto minmax(0, 1fr) 40px !important;
    }

    .forex-entry-new .fxn-receipt-find strong {
        grid-column: 2 / -1;
    }

    .forex-entry-new .fxn-nav {
        min-width: 0 !important;
        min-height: 44px !important;
        white-space: normal !important;
    }

    .forex-entry-new .fxn-middle,
    .forex-entry-new .fxn-customer-bottom,
    .forex-entry-new .fxn-lower,
    .forex-entry-new .fxn-right-bottom {
        display: grid !important;
        grid-template-columns: minmax(0, 1fr) !important;
        gap: 8px !important;
        min-height: 0 !important;
    }

    .forex-entry-new .fxn-left,
    .forex-entry-new .fxn-right {
        min-width: 0 !important;
    }

    .forex-entry-new .fxn-customer-grid {
        display: grid !important;
        grid-template-columns: minmax(0, 1fr) !important;
        gap: 8px !important;
    }

    .forex-entry-new .fxn-customer-grid > .fxn-field {
        grid-column: auto !important;
        grid-row: auto !important;
        width: 100% !important;
    }

    .forex-entry-new .fxn-field :is(input, select, textarea),
    .forex-entry-new .fxn-field .customer-lookup-group,
    .forex-entry-new .fxn-field .app-search-select {
        width: 100% !important;
        min-width: 0 !important;
        max-width: none !important;
    }

    .forex-entry-new .fxn-documents,
    .forex-entry-new .fxn-documents #custWorkspaceDocumentsPanel {
        height: auto !important;
        min-height: 240px !important;
        max-height: none !important;
        overflow: visible !important;
    }

    .forex-entry-new .fxn-documents #forexDocDisplayArea {
        max-height: 60dvh !important;
        overflow: auto !important;
    }

    .forex-entry-new .fxn-documents #customerDocumentsList > .cust-workspace-gallery-grid {
        grid-template-columns: minmax(0, 1fr) !important;
    }

    .forex-entry-new .fxn-inline-grid-wrap {
        height: auto !important;
        min-height: 180px !important;
        max-height: 55dvh !important;
        overflow: auto !important;
    }

    .forex-entry-new .fxn-inline-grid {
        min-width: 700px !important;
    }

    .forex-entry-new .fxn-stock-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 6px !important;
    }

    .forex-entry-new .fxn-stock-grid .fxn-stock-metric {
        margin-left: 0 !important;
        min-height: 54px !important;
    }

    .forex-entry-new .fxn-stock-grid .is-net {
        grid-column: 1 / -1 !important;
    }

    .forex-entry-new .fxn-actions {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    }

    .forex-entry-new .fxn-actions .fxn-action {
        min-height: 44px !important;
        height: auto !important;
    }

    /* Quick Transaction keeps its data grid wide but makes every control reachable. */
    body.quick-transaction-page main.app-content.content,
    body.quick-transaction-page.has-trading-ops-console main.app-content.content {
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
    }

    .quick-transaction-page .qt-page-shell {
        height: auto !important;
        overflow: visible !important;
    }

    .quick-transaction-page .entry-command-buttons {
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        width: 100%;
        gap: 6px;
    }

    .quick-transaction-page .qt-grid-card,
    .quick-transaction-page .qt-grid-card .grid-container {
        min-height: 220px !important;
        overflow: auto !important;
    }

    .quick-transaction-page #qtInlineGrid {
        min-width: 800px !important;
    }

    .quick-transaction-page section.qt-stock-position .qt-stock-position-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    }

    .quick-transaction-page section.qt-stock-position .qt-stock-metric.is-net {
        grid-column: 1 / -1 !important;
    }
}
