/* =========================================================
   CRM Invoice Popup - Light Theme with #173624 Accent
   ========================================================= */

.crm-modal {
    display: none; 
    position: fixed; 
    z-index: 9999; 
    left: 0; 
    top: 0;
    width: 100%; 
    height: 100%; 
    overflow-y: auto;
    background-color: rgba(15, 23, 42, 0.5);
    backdrop-filter: blur(6px);
    font-family: 'Yekan Bakh', system-ui, -apple-system, sans-serif; 
    direction: rtl;
    padding: 20px 0;
    box-sizing: border-box;
}

/* جهت ترازبندی مرکزی پاپ‌آپ QR و اصلی در صورت تنظیم display: flex */
.crm-modal[style*="display: flex"] {
    align-items: center;
    justify-content: center;
}

.crm-modal-content {
    background: #ffffff;
    border: 1px solid #e2e8f0;
    margin: auto; 
    padding: 28px;
    width: 92%; 
    max-width: 1150px; /* افزایش عرض برای جاگیری کامل دو ستون */
    max-height: 90vh;
    overflow-y: auto; /* اسکرول داخلی در صورت بلند بودن فرم */
    border-radius: 16px; 
    color: #1e293b;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.12);
    position: relative;
    box-sizing: border-box;
}

.crm-close-btn,
.crm-qr-close-btn { 
    position: absolute; 
    left: 22px; 
    top: 18px; 
    color: #64748b; 
    font-size: 26px; 
    cursor: pointer; 
    line-height: 1;
    transition: color 0.2s ease;
    z-index: 10;
}

.crm-close-btn:hover,
.crm-qr-close-btn:hover { 
    color: #173624; 
}

/* ساختار گرید دو ستونه با ایمنی عدم سرریز */
.crm-modal-grid { 
    display: grid; 
    grid-template-columns: repeat(2, minmax(0, 1fr)); /* جلوگیری از بیرون زدگی ستون‌ها */
    gap: 24px; 
    margin-top: 15px; 
}

/* محدود کردن ستون‌ها جهت مهار متون و جداول */
.crm-form-section,
.crm-preview-section {
    min-width: 0;
    width: 100%;
    box-sizing: border-box;
}

.crm-form-section h3,
.crm-preview-section h3 {
    margin-top: 0;
    margin-bottom: 18px;
    color: #173624;
    font-size: 18px;
    font-weight: 700;
    border-bottom: 2px solid rgba(23, 54, 36, 0.12);
    padding-bottom: 8px;
}

/* اینپوت‌ها و فرم */
.crm-input-group { 
    margin-bottom: 16px; 
}

.crm-input-group label { 
    display: block; 
    margin-bottom: 6px; 
    color: #334155; 
    font-size: 14px; 
    font-weight: 500;
}

.crm-input-group input { 
    width: 100%; 
    padding: 10px 14px; 
    border-radius: 8px; 
    background: #f8fafc; 
    border: 1px solid #cbd5e1; 
    color: #0f172a; 
    font-family: inherit;
    font-size: 14px;
    box-sizing: border-box;
    transition: all 0.2s ease-in-out;
}

.crm-input-group input:focus {
    outline: none;
    background: #ffffff;
    border-color: #173624;
    box-shadow: 0 0 0 3px rgba(23, 54, 36, 0.15);
}

/* پسوند ریال */
.crm-currency-wrapper {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%;
}

.crm-currency-wrapper input {
    padding-left: 45px !important;
}

.crm-currency-wrapper::after {
    content: "ریال";
    position: absolute;
    left: 12px;
    color: #94a3b8;
    font-size: 13px;
    font-weight: 500;
    pointer-events: none;
}

/* چیدمان اکشن‌های فرم */
.crm-form-actions {
    display: flex;
    gap: 8px;
    margin-top: 22px;
    align-items: center;
}

.crm-btn-modern {
    background: #173624;
    color: #ffffff; 
    border: none; 
    padding: 12px 18px;
    border-radius: 8px; 
    cursor: pointer; 
    font-family: inherit; 
    font-weight: 600;
    font-size: 14px;
    transition: background 0.2s ease, transform 0.1s ease, opacity 0.2s ease;
}

.crm-btn-main {
    flex: 2;
    text-align: center;
    white-space: nowrap;
}

.crm-btn-modern:hover:not(:disabled) {
    background: #224d34;
}

.crm-btn-secondary {
    flex: 1;
    background: #ffffff;
    color: #173624;
    border: 1px solid rgba(23, 54, 36, 0.3);
    padding: 11px 8px;
    border-radius: 8px;
    cursor: pointer;
    font-family: inherit;
    font-size: 13px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    white-space: nowrap;
    transition: all 0.2s ease;
}

.crm-btn-secondary:hover:not(:disabled) {
    background: rgba(23, 54, 36, 0.08);
    border-color: #173624;
}

.crm-btn-small { 
    background: rgba(23, 54, 36, 0.08); 
    color: #173624; 
    border: 1px solid rgba(23, 54, 36, 0.2); 
    padding: 8px 14px; 
    border-radius: 6px; 
    cursor: pointer;
    font-family: inherit;
    font-weight: 600;
    font-size: 13px;
    transition: background 0.2s;
}

.crm-btn-small:hover:not(:disabled) {
    background: rgba(23, 54, 36, 0.15);
}

/* وضعیت غیرفعال دکمه‌ها هنگام بارگذاری */
.crm-btn-modern:disabled,
.crm-btn-secondary:disabled,
.crm-btn-small:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* ریپیتر خدمات */
.crm-repeater-item { 
    display: flex; 
    gap: 10px; 
    margin-bottom: 10px; 
}

.crm-remove-row { 
    background: #fee2e2; 
    color: #dc2626; 
    border: 1px solid #fca5a5; 
    border-radius: 6px; 
    cursor: pointer; 
    padding: 0 12px; 
    font-size: 18px;
    line-height: 1;
    transition: background 0.2s;
}

.crm-remove-row:hover {
    background: #fecaca;
}

/* بخش پیش‌نمایش زنده */
.crm-preview-card {
    background: #f8fafc; 
    border: 1px solid #e2e8f0;
    border-radius: 12px; 
    padding: 20px; 
    min-height: 320px;
    box-sizing: border-box;
    overflow: hidden; /* جلوگیری از سرریز محتوا */
    word-break: break-word; /* شکستن کلمات طولانی و آدرس‌ها */
    overflow-wrap: break-word;
}

.preview-header { 
    display: flex; 
    justify-content: space-between; 
    border-bottom: 1px solid #cbd5e1; 
    padding-bottom: 12px; 
    margin-bottom: 16px; 
    color: #334155;
    font-size: 13.5px;
    gap: 10px;
}

.company-info {
    flex: 1;
    min-width: 0;
}

.company-info #prev_company_name {
    color: #173624;
    font-size: 15px;
    font-weight: 600;
}

.meta-info {
    white-space: nowrap;
}

/* جدول پیش‌نمایش */
.preview-body {
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    overflow-x: auto; /* اسکرول افقی نرم اگر جدول خیلی عریض شد */
    background: #ffffff;
}

.crm-preview-table { 
    width: 100%; 
    border-collapse: separate;
    border-spacing: 0;
    font-size: 13px;
}

.crm-preview-table thead {
    background: rgba(23, 54, 36, 0.05);
}

.crm-preview-table th { 
    padding: 10px 12px; 
    color: #173624; 
    font-size: 12.5px;
    font-weight: 700;
    text-align: right;
    border-bottom: 1px solid #e2e8f0;
}

.crm-preview-table th:last-child,
.crm-preview-table td:last-child {
    text-align: left;
}

.crm-preview-table td { 
    padding: 10px 12px; 
    font-size: 13px; 
    color: #334155;
    border-bottom: 1px solid #f1f5f9;
    word-break: break-word;
}

.crm-preview-table .empty-row td {
    text-align: center !important;
    color: #94a3b8;
    padding: 18px 10px;
    font-size: 13px;
}

.preview-footer { 
    margin-top: 15px; 
    padding-top: 15px; 
    border-top: 2px dashed #cbd5e1; 
    font-size: 14.5px; 
    color: #0f172a;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.preview-footer strong {
    color: #173624;
    font-size: 18px;
}

/* پاپ‌آپ کد QR */
.crm-qr-modal-content {
    background: #ffffff;
    border-radius: 16px;
    padding: 25px;
    width: 90%;
    max-width: 460px;
    margin: auto;
    position: relative;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
    box-sizing: border-box;
}

.crm-qr-container {
    display: flex;
    justify-content: space-around;
    gap: 15px;
    margin-top: 15px;
}

.crm-qr-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: #f8fafc;
    padding: 15px;
    border-radius: 12px;
    border: 1px solid #e2e8f0;
    flex: 1;
}

.crm-qr-box img,
.crm-qr-box canvas {
    border-radius: 6px;
    max-width: 100%;
    height: auto;
}

.crm-qr-box span {
    margin-top: 10px;
    font-size: 12.5px;
    font-weight: 600;
    color: #173624;
}

/* =========================================================
   واکنش‌گرایی (Responsive Design)
   ========================================================= */
@media (max-width: 992px) {
    .crm-modal-grid {
        grid-template-columns: 1fr; /* تبدیل به تک ستونی در نمایشگرهای کوچک‌تر */
        gap: 24px;
    }

    .crm-modal-content {
        padding: 20px;
        margin: 20px auto;
        width: 95%;
    }

    .crm-form-actions {
        flex-wrap: wrap;
    }

    .crm-btn-main, 
    .crm-btn-secondary {
        flex: 1 1 100%;
    }

    .crm-qr-container {
        flex-direction: column;
    }
}

/* =========================================================
   ۱. استایل‌های پایه کارت فاکتور (تنظیم خروجی PDF و پیش‌نمایش)
   ========================================================= */
.crm-printable-card,
#crm-printable-card {
    background-color: #ffffff !important;
    color: #0f172a !important;
    box-sizing: border-box;
    width: 100%;
    max-width: 800px;
    margin: 0 auto;
    padding: 24px;
    border-radius: 8px;
    direction: rtl;
}

/* =========================================================
   ۲. استایل‌های مخصوص چاپ مرورگر (Print Styles - Ctrl+P)
   ========================================================= */
@media print {
    /* تنظیم سایز برگه A4 و حذف حاشیه‌های اضافه مرورگر */
    @page {
        size: A4 portrait;
        margin: 10mm;
    }

    /* مخفی‌سازی تمام عناصر صفحه */
    body * {
        visibility: hidden !important;
    }

    /* نمایش اختصاصی کارت فاکتور و فرزندان آن */
    .crm-printable-card, 
    .crm-printable-card *,
    #crm-printable-card, 
    #crm-printable-card * {
        visibility: visible !important;
    }

    /* جانمایی کارت در بالا و چپ صفحه هنگام چاپ */
    .crm-printable-card,
    #crm-printable-card {
        position: absolute !important;
        left: 0 !important;
        top: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
        background: #ffffff !important;
        color: #000000 !important;
        border: none !important;
        box-shadow: none !important;
    }

    /* مخفی کردن دکمه‌ها یا مواردی که نباید چاپ شوند */
    .crm-no-print,
    .crm-close-btn,
    .crm-export-pdf,
    .crm-export-word,
    .crm-export-qr {
        display: none !important;
    }
}

/* =========================================================
   اعمال اولویت بالا (ID Selector + !important) برای حل مشکل عرض پاپ‌آپ
   ========================================================= */

#crm-proforma-modal .crm-modal-content,
#crm-official-modal .crm-modal-content,
div[id*="crm-"][id*="-modal"] .crm-modal-content {
    width: 95% !important;
    max-width: 1000px !important; /* افزایش عرض کلی به ۱۳۰۰ پیکسل */
    box-sizing: border-box !important;
    padding: 30px !important;
}

#crm-proforma-modal .crm-modal-grid,
#crm-official-modal .crm-modal-grid,
div[id*="crm-"][id*="-modal"] .crm-modal-grid {
    display: grid !important;
    grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr) !important; /* سهم ۵۵٪ فرم و ۴۵٪ پیش‌نمایش */
    gap: 28px !important;
    width: 100% !important;
}

/* اجبار به عدم خروج محتوا از ستون‌ها */
#crm-proforma-modal .crm-form-section,
#crm-official-modal .crm-form-section,
#crm-proforma-modal .crm-preview-section,
#crm-official-modal .crm-preview-section {
    width: 100% !important;
    min-width: 0 !important;
    box-sizing: border-box !important;
}