/*
 * RAQEEM responsive contract (system-wide, mobile-first fixes).
 *
 * 1. Item/line tables stop forcing a wide canvas on phones: rows become cards.
 * 2. A table wider than its screen scrolls inside itself (never widens the page); healthy tables are untouched.
 * 3. The fixed bottom bar steps aside while the keyboard is open.
 * 4. Unified in-app document viewer (raqeem_file_viewer.js) — always has a way out.
 */

/* ---------- 1. client document builder: rows as cards on phones ---------- */
@media (max-width: 640px) {
    .cdb-excel-scroll {
        overflow-x: visible;
    }

    .cdb-excel-scroll .cdb-excel-head,
    .cdb-excel-scroll .cdb-sheet-head {
        display: none !important;
    }

    .cdb-excel-scroll .cdb-excel-row:not(.cdb-excel-row-merged),
    .cdb-excel-scroll .cdb-excel-row-goods:not(.cdb-excel-head) {
        min-width: 0 !important;
        grid-template-columns: repeat(6, minmax(0, 1fr)) !important;
        gap: 0.4rem !important;
        padding: 0.6rem 0.5rem !important;
        margin-bottom: 0.35rem;
        border: 1px solid rgba(226, 232, 240, 0.95);
        border-radius: 0.75rem;
        background: #fff;
    }

    /* children: 1 item/unit · 2 description · 3 qty · 4 price · 5 total · 6 actions */
    .cdb-excel-scroll .cdb-excel-row:not(.cdb-excel-row-merged) > :nth-child(2),
    .cdb-excel-scroll .cdb-excel-row-goods:not(.cdb-excel-head) > :nth-child(2) {
        grid-column: 1 / -1;
        order: 1;
    }

    .cdb-excel-scroll .cdb-excel-row:not(.cdb-excel-row-merged) > :nth-child(1),
    .cdb-excel-scroll .cdb-excel-row-goods:not(.cdb-excel-head) > :nth-child(1) {
        grid-column: span 2;
        order: 2;
    }

    .cdb-excel-scroll .cdb-excel-row:not(.cdb-excel-row-merged) > :nth-child(3),
    .cdb-excel-scroll .cdb-excel-row-goods:not(.cdb-excel-head) > :nth-child(3),
    .cdb-excel-scroll .cdb-excel-row:not(.cdb-excel-row-merged) > :nth-child(4),
    .cdb-excel-scroll .cdb-excel-row-goods:not(.cdb-excel-head) > :nth-child(4) {
        grid-column: span 2;
        order: 3;
    }

    .cdb-excel-scroll .cdb-excel-row:not(.cdb-excel-row-merged) > :nth-child(5),
    .cdb-excel-scroll .cdb-excel-row-goods:not(.cdb-excel-head) > :nth-child(5) {
        grid-column: span 4;
        order: 4;
        display: flex;
        align-items: center;
        gap: 0.4rem;
        justify-content: flex-start;
        min-height: 2.5rem;
    }

    .cdb-excel-scroll .cdb-excel-row:not(.cdb-excel-row-merged) > :nth-child(6),
    .cdb-excel-scroll .cdb-excel-row-goods:not(.cdb-excel-head) > :nth-child(6) {
        grid-column: span 2;
        order: 5;
        justify-content: flex-end;
    }

    .cdb-excel-scroll .cdb-live-total-row::before {
        content: "الإجمالي";
        font-size: 0.7rem;
        font-weight: 800;
        color: #64748b;
    }

    /* merged rows (section title / section total) fill the card width */
    .cdb-excel-scroll .cdb-excel-row-merged,
    .cdb-excel-scroll .cdb-excel-row-section-title,
    .cdb-excel-scroll .cdb-excel-row-section-total {
        min-width: 0 !important;
        grid-template-columns: minmax(0, 1fr) !important;
    }

    .cdb-excel-scroll .cdb-sheet-input,
    .cdb-excel-scroll .cdb-sheet-textarea {
        min-height: 2.75rem;
        font-size: 16px; /* prevents iOS zoom on focus */
        min-width: 0;
        width: 100%;
    }

    .cdb-excel-scroll [data-client-document-action="delete-row"] {
        min-height: 2.75rem;
        min-width: 2.75rem;
    }

    .cdb-action-dock .cdb-action {
        flex: 1 1 42%;
        min-height: 2.75rem;
    }
}

/* ---------- 2. any other table: scrolls inside itself on phones ---------- */
/* applied by raqeem_file_viewer.js ONLY to a table that really is wider than its screen */
.rq-table-fit {
    display: block;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

@media (max-width: 640px) {
    html, body {
        overflow-x: clip;
    }

    /* long unbroken numbers/names must never push the page wider */
    main {
        min-width: 0;
    }
}

/* ---------- 3. keyboard open: the fixed bottom bar must not cover fields ---------- */
html.rq-kbd-open #mainNav [data-nav="mobile-action-row"] {
    display: none !important;
}

html.rq-kbd-open body > main.flex-grow {
    padding-bottom: 1rem !important;
}

/* ---------- 4. unified in-app document viewer ---------- */
html.rq-viewer-open,
html.rq-viewer-open body {
    overflow: hidden;
}

.rq-viewer {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    flex-direction: column;
    background: #f6f3ee;
    color: #1c1917;
    font-family: inherit;
}

.rq-viewer-header {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: calc(0.5rem + env(safe-area-inset-top, 0px)) 0.6rem 0.5rem;
    background: #fff;
    border-bottom: 1px solid rgba(226, 232, 240, 0.95);
    box-shadow: 0 1px 8px rgba(15, 23, 42, 0.06);
}

.rq-viewer-title {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.9rem;
    font-weight: 800;
    text-align: center;
}

.rq-viewer-btn {
    flex: 0 0 auto;
    min-height: 2.75rem;
    padding: 0 0.8rem;
    border: 1px solid rgba(203, 213, 225, 1);
    border-radius: 0.75rem;
    background: #fff;
    color: #0f172a;
    font: inherit;
    font-size: 0.8rem;
    font-weight: 800;
}

.rq-viewer-btn--primary {
    background: #15803d;
    border-color: #15803d;
    color: #fff;
}

.rq-viewer-btn[disabled] {
    opacity: 0.5;
}

.rq-viewer-status {
    padding: 0.5rem 0.9rem;
    font-size: 0.8rem;
    font-weight: 700;
    background: #eef2ff;
    color: #3730a3;
}

.rq-viewer-status[data-kind="error"] {
    background: #fef2f2;
    color: #b91c1c;
}

.rq-viewer-body {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

.rq-viewer-frame-wrap {
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    background: #e7e5e4;
}

.rq-viewer-frame {
    display: block;
    width: 100%;
    height: 100%;
    border: 0;
    background: #fff;
}

.rq-viewer-fallback {
    flex: 0 0 auto;
    padding: 0.5rem;
    text-align: center;
}

.rq-viewer-link {
    font-size: 0.8rem;
    font-weight: 800;
    color: #4338ca;
}

.rq-viewer-spinner {
    margin: 4rem auto 0;
    width: 2.25rem;
    height: 2.25rem;
    border: 3px solid #cbd5e1;
    border-top-color: #4338ca;
    border-radius: 50%;
    animation: rq-viewer-spin 0.9s linear infinite;
}

@keyframes rq-viewer-spin {
    to { transform: rotate(360deg); }
}

.rq-viewer-retry {
    margin: 3rem auto 0;
    min-height: 2.75rem;
    padding: 0 1.25rem;
    border: 0;
    border-radius: 0.75rem;
    background: #4338ca;
    color: #fff;
    font: inherit;
    font-weight: 800;
}

/* viewer: PDF pages drawn at phone width, always fully visible */
.rq-viewer-pages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    background: #d6d3d1;
    padding: 0.5rem;
    touch-action: pan-y pinch-zoom;
}

.rq-viewer-page {
    display: block;
    width: 100%;
    height: auto;
    margin: 0 auto 0.6rem;
    background: #fff;
    box-shadow: 0 1px 6px rgba(15, 23, 42, 0.25);
}

.rq-viewer[data-rq-kind] [data-rq-viewer-act="send"] {
    display: none;
}

.rq-viewer-filecard {
    margin: 4rem auto 0;
    width: min(100% - 2rem, 22rem);
    padding: 1.5rem 1rem;
    border-radius: 1rem;
    background: #fff;
    border: 1px solid rgba(226, 232, 240, 0.95);
    text-align: center;
}

.rq-viewer-fileicon {
    display: inline-block;
    padding: 0.6rem 0.9rem;
    border-radius: 0.75rem;
    background: #eef2ff;
    color: #3730a3;
    font-weight: 900;
}

.rq-viewer-filename {
    margin: 0.75rem 0 1rem;
    font-weight: 800;
    word-break: break-word;
}

.rq-viewer-btn--wide {
    width: 100%;
}

/* quote builder: ready actions look usable; the faint style stays for "not saved yet" */
.cdb-action.cdb-action-secondary.cdb-action-ready {
    opacity: 1;
    color: #3730a3;
    border-color: rgba(79, 70, 229, 0.45);
    background: #eef2ff;
}

/* quote builder totals block: the template's wide min-widths clipped it on phones */
@media (max-width: 640px) {
    [data-client-document-builder] .cdb-summary-line {
        grid-template-columns: minmax(0, 1fr) minmax(0, 9.5rem) !important;
        gap: 0.5rem !important;
    }

    [data-client-document-builder] .cdb-summary-line .cdb-sheet-input,
    [data-client-document-builder] .cdb-summary-input-wide,
    [data-client-document-builder] .cdb-summary-line-calculated .cdb-live-total,
    [data-client-document-builder] .cdb-live-total-net-authority,
    [data-client-document-builder] .cdb-live-total-net {
        min-width: 0 !important;
        width: 100% !important;
        max-width: 100%;
    }

    [data-client-document-builder] .cdb-financial-closing,
    [data-client-document-builder] .cdb-sheet-summary {
        padding-inline: 0.6rem !important;
        max-width: 100%;
    }

    [data-client-document-builder] .cdb-summary-group {
        padding-inline: 0.2rem;
    }
}
