/*
=======================================================================
MUT-SEK TECH — MOBILE ADMIN / CRM RESPONSIVE LAYER
=======================================================================

Purpose:
- Mobile Frappe Desk usability
- Mobile Invoice / Quotation editing
- Mobile child-table usability
- Mobile print-preview scaling

IMPORTANT:
- No Print Format document styling is changed.
- No PDF layout styling is changed.
- All print-preview scaling is SCREEN ONLY.
=======================================================================
*/


/* --------------------------------------------------------------------
   DESK — PHONE LAYOUT
   -------------------------------------------------------------------- */

@media screen and (max-width: 767.98px) {

    /*
     * PAGE HEADER / SAVE / ACTION BUTTONS
     *
     * Allow the document title to surrender width to actions.
     * This prevents Save / primary actions disappearing off-screen.
     */

    body[data-route^="Form"] .page-head .container,
    body[data-route^="Form"] .page-head .container-fluid,
    body[data-route^="List"] .page-head .container,
    body[data-route^="List"] .page-head .container-fluid {
        width: 100% !important;
        max-width: none !important;
        padding-left: 8px !important;
        padding-right: 8px !important;
    }

    body[data-route^="Form"] .page-head .page-head-content,
    body[data-route^="List"] .page-head .page-head-content {
        display: flex !important;
        align-items: center !important;
        flex-wrap: nowrap !important;
        gap: 6px !important;
        width: 100% !important;
    }

    body[data-route^="Form"] .page-title,
    body[data-route^="List"] .page-title {
        flex: 1 1 auto !important;
        min-width: 0 !important;
        overflow: hidden !important;
    }

    body[data-route^="Form"] .page-title .title-area,
    body[data-route^="List"] .page-title .title-area {
        min-width: 0 !important;
        max-width: 100% !important;
        overflow: hidden !important;
    }

    body[data-route^="Form"] .page-title .title-text,
    body[data-route^="List"] .page-title .title-text {
        display: block !important;
        max-width: 30vw !important;
        overflow: hidden !important;
        white-space: nowrap !important;
        text-overflow: ellipsis !important;
    }

    /*
     * Actions remain reachable and do not get cut in half.
     */

    body[data-route^="Form"] .page-actions,
    body[data-route^="List"] .page-actions {
        display: flex !important;
        align-items: center !important;
        flex: 0 0 auto !important;
        min-width: 0 !important;
        max-width: 66vw !important;

        overflow-x: auto !important;
        overflow-y: hidden !important;

        white-space: nowrap !important;

        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    body[data-route^="Form"] .page-actions::-webkit-scrollbar,
    body[data-route^="List"] .page-actions::-webkit-scrollbar {
        display: none;
    }

    body[data-route^="Form"] .page-actions .btn,
    body[data-route^="List"] .page-actions .btn {
        flex: 0 0 auto !important;
        margin-left: 4px !important;
        margin-right: 0 !important;
    }

    body[data-route^="Form"] .page-actions .btn-primary {
        min-width: 48px !important;
    }


    /* ----------------------------------------------------------------
       FORM BODY
       ---------------------------------------------------------------- */

    body[data-route^="Form"] .layout-main-section-wrapper {
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;

        flex: 1 1 100% !important;
    }

    body[data-route^="Form"] .layout-main-section {
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
    }

    body[data-route^="Form"] .form-layout,
    body[data-route^="Form"] .form-page {
        max-width: 100% !important;
        min-width: 0 !important;
    }

    body[data-route^="Form"] .form-column {
        min-width: 0 !important;
    }

    body[data-route^="Form"] .control-input-wrapper,
    body[data-route^="Form"] .control-input,
    body[data-route^="Form"] .form-control {
        max-width: 100% !important;
    }


    /* ----------------------------------------------------------------
       CHILD TABLES — QUOTATION / INVOICE ITEMS
       ----------------------------------------------------------------

       Do NOT crush several desktop columns into 340px.

       Preserve the table at a usable width and allow finger scrolling.
       The user may tap a row to open the full mobile row editor.
       ---------------------------------------------------------------- */

    body[data-route^="Form"] .form-grid {
        width: 100% !important;
        max-width: 100% !important;

        overflow-x: auto !important;
        overflow-y: hidden !important;

        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
    }

    body[data-route^="Form"] .form-grid .grid-heading-row,
    body[data-route^="Form"] .form-grid .grid-row,
    body[data-route^="Form"] .form-grid .data-row {
        min-width: 640px !important;
    }

    body[data-route^="Form"] .form-grid .grid-body {
        min-width: 640px !important;
    }

    body[data-route^="Form"] .grid-footer {
        min-width: 100% !important;
    }

    body[data-route^="Form"] .grid-buttons,
    body[data-route^="Form"] .grid-bulk-actions {
        flex-wrap: wrap !important;
        gap: 6px !important;
    }


    /* ----------------------------------------------------------------
       CHILD ROW EDITOR
       ----------------------------------------------------------------

       When an item is opened, use almost the entire phone screen.
       ---------------------------------------------------------------- */

    body[data-route^="Form"] .grid-row-open .form-in-grid {
        position: fixed !important;

        top: 8px !important;
        left: 50% !important;

        transform: translateX(-50%) !important;

        width: calc(100vw - 16px) !important;
        max-width: calc(100vw - 16px) !important;

        max-height: calc(100vh - 16px) !important;

        margin: 0 !important;
        padding: 8px !important;

        overflow: auto !important;

        border-radius: 12px !important;

        z-index: 1050 !important;
    }

    body[data-route^="Form"] .grid-row-open .form-in-grid .grid-form-body {
        max-height: calc(100vh - 100px) !important;
        overflow-y: auto !important;
    }

    body[data-route^="Form"] .grid-row-open .form-in-grid .form-tabs {
        overflow-x: auto !important;
        flex-wrap: nowrap !important;
    }


    /* ----------------------------------------------------------------
       LIST FILTER TOOLBAR
       ---------------------------------------------------------------- */

    body[data-route^="List"] .page-form {
        width: 100% !important;

        flex-wrap: nowrap !important;

        overflow-x: auto !important;
        overflow-y: hidden !important;

        -webkit-overflow-scrolling: touch;

        scrollbar-width: none;
    }

    body[data-route^="List"] .page-form::-webkit-scrollbar {
        display: none;
    }

    body[data-route^="List"] .page-form .form-group {
        flex: 0 0 auto !important;
    }


    /* ----------------------------------------------------------------
       PRINT PREVIEW INSIDE DESK
       ----------------------------------------------------------------

       Keep the actual document at A4 proportions and SCALE the whole
       document for phone preview instead of crushing its internal grid.
       ---------------------------------------------------------------- */

    .print-preview-wrapper {
        padding: 8px !important;

        overflow-x: hidden !important;
        overflow-y: visible !important;

        background: var(--gray-100, #f5f7fa) !important;
    }

    .print-preview {
        width: 210mm !important;
        min-width: 210mm !important;
        max-width: 210mm !important;

        min-height: 297mm !important;

        margin: 0 auto !important;

        transform-origin: top left !important;

        border-radius: 8px !important;
    }


    /* ----------------------------------------------------------------
       DIRECT /printview
       ---------------------------------------------------------------- */

    .print-format-gutter {
        padding: 8px !important;

        overflow-x: hidden !important;
        overflow-y: visible !important;
    }

    .print-format-gutter > .print-format {
        width: 210mm !important;
        min-width: 210mm !important;
        max-width: 210mm !important;

        margin: 0 auto !important;

        transform-origin: top left !important;
    }
}


/*
 * PHONE-SPECIFIC PREVIEW SCALE
 *
 * 8.3in A4 preview ≈ 797px wide.
 * Scale the entire sheet proportionally.
 */

@media screen and (max-width: 319.98px) {

    .print-preview,
    .print-format-gutter > .print-format {
        zoom: 0.36;
    }
}

@media screen and (min-width: 320px) and (max-width: 359.98px) {

    .print-preview,
    .print-format-gutter > .print-format {
        zoom: 0.38;
    }
}

@media screen and (min-width: 360px) and (max-width: 399.98px) {

    .print-preview,
    .print-format-gutter > .print-format {
        zoom: 0.43;
    }
}

@media screen and (min-width: 400px) and (max-width: 479.98px) {

    .print-preview,
    .print-format-gutter > .print-format {
        zoom: 0.48;
    }
}

@media screen and (min-width: 480px) and (max-width: 575.98px) {

    .print-preview,
    .print-format-gutter > .print-format {
        zoom: 0.58;
    }
}

@media screen and (min-width: 576px) and (max-width: 767.98px) {

    .print-preview,
    .print-format-gutter > .print-format {
        zoom: 0.70;
    }
}


/*
 * Firefox/future fallback when CSS zoom is unavailable.
 */

@supports not (zoom: 1) {

    @media screen and (max-width: 767.98px) {

        .print-preview,
        .print-format-gutter > .print-format {
            transform: scale(0.43);
            transform-origin: top left;
        }
    }
}


/*
 * ABSOLUTE PDF / PRINT PROTECTION
 *
 * Mobile preview behaviour must never leak into printed output.
 */

@media print {

    .print-preview,
    .print-format-gutter > .print-format,
    .print-format {
        zoom: 1 !important;
        transform: none !important;
    }
}


/*
=======================================================================
MUT-SEK TECH — MOBILE ACTION MENU REPAIR
=======================================================================

Reason:
The first responsive layer made .page-actions horizontally scrollable.
Frappe's three-dot dropdown is a child of that action region, therefore
the dropdown could be clipped by overflow.

This block deliberately changes ONLY mobile Desk action/dropdown
behaviour. Commercial Print Formats and PDFs are untouched.
=======================================================================
*/

@media screen and (max-width: 767.98px) {

    /*
     * Never clip toolbar dropdowns.
     */
    body[data-route^="Form"] .page-head,
    body[data-route^="List"] .page-head {
        overflow: visible !important;
        z-index: 1000 !important;
    }

    body[data-route^="Form"] .page-head .container,
    body[data-route^="Form"] .page-head .container-fluid,
    body[data-route^="Form"] .page-head .page-head-content,
    body[data-route^="List"] .page-head .container,
    body[data-route^="List"] .page-head .container-fluid,
    body[data-route^="List"] .page-head .page-head-content {
        overflow: visible !important;
    }


    /*
     * The important correction:
     * actions must not become an overflow clipping container.
     */
    body[data-route^="Form"] .page-actions,
    body[data-route^="List"] .page-actions {
        overflow: visible !important;

        display: flex !important;
        align-items: center !important;
        justify-content: flex-end !important;

        flex-wrap: nowrap !important;

        white-space: nowrap !important;

        max-width: 72vw !important;

        position: relative !important;

        z-index: 1001 !important;
    }


    /*
     * Give actions slightly more room by allowing the title to
     * truncate aggressively on narrow phones.
     */
    body[data-route^="Form"] .page-title,
    body[data-route^="List"] .page-title {
        min-width: 0 !important;
        flex: 1 1 auto !important;
    }

    body[data-route^="Form"] .page-title .title-text,
    body[data-route^="List"] .page-title .title-text {
        max-width: 24vw !important;

        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }


    /*
     * Three-dot / custom menu containers must always survive
     * responsive shrinking.
     */
    body[data-route^="Form"] .page-actions .menu-btn-group,
    body[data-route^="Form"] .page-actions .custom-btn-group,
    body[data-route^="Form"] .page-actions .page-icon-group,
    body[data-route^="List"] .page-actions .menu-btn-group,
    body[data-route^="List"] .page-actions .custom-btn-group,
    body[data-route^="List"] .page-actions .page-icon-group {
        display: inline-flex !important;

        visibility: visible !important;
        opacity: 1 !important;

        position: relative !important;

        overflow: visible !important;

        flex: 0 0 auto !important;
        flex-shrink: 0 !important;

        z-index: 1002 !important;
    }


    /*
     * Preserve the actual three-dot trigger.
     */
    body[data-route^="Form"] .page-actions .menu-btn-group > .btn,
    body[data-route^="Form"] .page-actions .custom-btn-group > .btn,
    body[data-route^="List"] .page-actions .menu-btn-group > .btn,
    body[data-route^="List"] .page-actions .custom-btn-group > .btn {
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;

        visibility: visible !important;
        opacity: 1 !important;

        min-width: 38px !important;

        flex: 0 0 auto !important;
    }


    /*
     * Dropdown opens inward from the right side of the phone.
     *
     * High z-index ensures Print / Email / Duplicate / Delete etc.
     * appear ABOVE document content.
     */
    body[data-route^="Form"] .page-actions .dropdown-menu,
    body[data-route^="List"] .page-actions .dropdown-menu,
    body[data-route^="Form"] .menu-btn-group .dropdown-menu,
    body[data-route^="List"] .menu-btn-group .dropdown-menu {
        position: absolute !important;

        left: auto !important;
        right: 0 !important;

        top: calc(100% + 6px) !important;

        min-width: 210px !important;
        max-width: calc(100vw - 20px) !important;

        max-height: 70vh !important;

        overflow-x: hidden !important;
        overflow-y: auto !important;

        z-index: 10050 !important;

        visibility: visible;

        -webkit-overflow-scrolling: touch;

        box-shadow:
            0 10px 30px rgba(0, 0, 0, 0.18) !important;
    }


    /*
     * Individual dropdown options remain readable and tappable.
     */
    body[data-route^="Form"] .page-actions .dropdown-item,
    body[data-route^="List"] .page-actions .dropdown-item,
    body[data-route^="Form"] .menu-btn-group .dropdown-item,
    body[data-route^="List"] .menu-btn-group .dropdown-item {
        min-height: 42px !important;

        display: flex !important;
        align-items: center !important;

        white-space: normal !important;

        padding: 10px 14px !important;
    }


    /*
     * Ensure document content never paints above an open action menu.
     */
    body[data-route^="Form"] .layout-main,
    body[data-route^="Form"] .layout-main-section-wrapper,
    body[data-route^="Form"] .layout-main-section,
    body[data-route^="List"] .layout-main,
    body[data-route^="List"] .layout-main-section-wrapper,
    body[data-route^="List"] .layout-main-section {
        position: relative;
        z-index: 1;
    }
}


/*
 * Very narrow phones: reserve more space for Save + three dots.
 */
@media screen and (max-width: 399.98px) {

    body[data-route^="Form"] .page-title .title-text,
    body[data-route^="List"] .page-title .title-text {
        max-width: 18vw !important;
    }

    body[data-route^="Form"] .page-actions,
    body[data-route^="List"] .page-actions {
        max-width: 78vw !important;
    }
}
