/* ----------------------------------------------------
   Design Tokens & System Variables
   ---------------------------------------------------- */
:root {
    --primary-blue: #1e3b6e;
    --primary-gold: #c59b27;
    --dark-gold: #947116;
    --bg-parchment: #f9f6ef;
    --paper-border: #bca885;
    --text-dark: #1a1a1a;
    --text-muted: #4b5563;
    --table-header-bg: #1e293b;
    --table-header-text: #ffffff;
    --border-color: #000000;
    
    --font-arabic: 'Tajawal', sans-serif;
    --font-serif: 'Amiri', serif;
}

/* Base Setup */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-arabic);
    background-color: #334155;
    color: var(--text-dark);
    direction: rtl;
    line-height: 1.35;
    min-height: 100vh;
    padding-bottom: 40px;
}

/* ----------------------------------------------------
   Admin Control Bar (Top Fixed Bar)
   ---------------------------------------------------- */
.admin-control-bar {
    position: sticky;
    top: 0;
    z-index: 1000;
    background: #0f172a;
    color: #ffffff;
    padding: 12px 24px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
    border-bottom: 2px solid var(--primary-gold);
}

.control-bar-container {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px;
}

.brand-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand-badge {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 1.05rem;
    color: #f8fafc;
}

.control-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    border-radius: 6px;
    font-family: var(--font-arabic);
    font-weight: 700;
    font-size: 0.9rem;
    cursor: pointer;
    border: none;
    transition: all 0.2s ease-in-out;
}

.btn-add { background-color: #10b981; color: #ffffff; }
.btn-add:hover { background-color: #059669; }

.btn-regenerate { background-color: #3b82f6; color: #ffffff; }
.btn-regenerate:hover { background-color: #2563eb; }

.btn-reset { background-color: #ef4444; color: #ffffff; }
.btn-reset:hover { background-color: #dc2626; }

.btn-print {
    background-color: var(--primary-gold);
    color: #0f172a;
    box-shadow: 0 2px 8px rgba(197, 155, 39, 0.4);
}
.btn-print:hover {
    background-color: #d9a82e;
    transform: translateY(-1px);
}

/* ----------------------------------------------------
   Document Preview Container & Pages Layout
   ---------------------------------------------------- */
.document-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 40px;
    margin-top: 30px;
}

.a4-page {
    width: 210mm;
    height: 297mm;
    min-height: 297mm;
    max-height: 297mm;
    background: url('https://i.postimg.cc/Bbs40NJc/alkhlfyt.jpg') no-repeat center center / 100% 100% !important;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    position: relative;
    box-sizing: border-box;
    overflow: hidden;
}

/* ----------------------------------------------------
   1. FIXED TOP HEADER (في أعلى الصفحة تماماً في النطاق العلوي)
   ---------------------------------------------------- */
.doc-header-fixed {
    position: absolute;
    top: 5mm;
    left: 14mm;
    right: 14mm;
    height: 26mm;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10;
}

.company-brand {
    text-align: center;
}

.company-title {
    font-size: 2.15rem;
    font-weight: 900;
    color: var(--primary-blue);
    line-height: 1.1;
    margin-bottom: 2px;
    letter-spacing: -0.5px;
}

.company-sub {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--primary-blue);
    line-height: 1.25;
}

.company-logo {
    position: absolute;
    left: 0;
    top: 0;
    width: 95px;
    height: 95px;
}

.official-logo-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    filter: drop-shadow(0 2px 5px rgba(0,0,0,0.18));
}

/* ----------------------------------------------------
   2. INNER DOCUMENT BODY (داخلياً بين الحد العلوي والحد السفلي)
   ---------------------------------------------------- */
.doc-inner-body {
    position: absolute;
    top: 36mm;
    bottom: 18mm;
    left: 14mm;
    right: 14mm;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    overflow: hidden;
}

.doc-title-bar {
    text-align: center;
    margin: 2px 0 6px 0;
}

.doc-title {
    font-size: 1.45rem;
    font-weight: 900;
    color: var(--primary-blue);
    display: inline-block;
    padding-bottom: 1px;
    border-bottom: 2px solid var(--primary-blue);
}

.doc-meta-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.88rem;
    font-weight: 700;
    margin-bottom: 8px;
    padding: 0 2px;
}

.meta-item {
    display: flex;
    align-items: center;
    gap: 6px;
}

.meta-label {
    color: var(--text-dark);
}

/* Editable Inputs styling in document */
.editable-field {
    background: transparent;
    border: 1px dashed transparent;
    font-family: inherit;
    font-size: inherit;
    font-weight: inherit;
    color: inherit;
    padding: 1px 4px;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.editable-field:hover, .editable-field:focus {
    background-color: rgba(255, 255, 255, 0.85);
    border-color: #3b82f6;
    outline: none;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.meta-input {
    width: 140px;
    font-weight: 700;
}

/* Client Section */
.client-section {
    margin-bottom: 8px;
    padding: 0 2px;
}

.client-row {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 1.05rem;
    font-weight: 800;
    color: var(--text-dark);
    margin-bottom: 3px;
}

.client-input {
    flex: 1;
    font-size: 1.05rem;
    font-weight: 800;
    color: var(--primary-blue);
}

.greeting-text {
    font-size: 0.92rem;
    font-weight: 700;
    margin-bottom: 2px;
}

.intro-text {
    font-size: 0.88rem;
    font-weight: 500;
    line-height: 1.35;
}

/* Items Table */
.items-table-section {
    margin-bottom: 6px;
}

.quote-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 6px;
    font-size: 0.8rem;
    text-align: center;
}

.quote-table th {
    background-color: #3d3010;
    color: #e8c84a;
    padding: 5px 3px;
    border: 1px solid #2a1f00;
    font-weight: 800;
    line-height: 1.15;
}

.en-head {
    font-size: 0.7rem;
    font-weight: 400;
    display: block;
    color: #d4a827;
}

/* Odd rows = white/cream */
.quote-table tbody tr:nth-child(odd) td {
    border: 1px solid #b8960c;
    padding: 4px 3px;
    vertical-align: middle;
    background-color: rgba(255, 253, 235, 0.85);
    font-weight: 700;
    color: #1a1a1a;
}

/* Even rows = warm gold tint */
/* 1. السكوت عن العرض الثابت والسماح للجدول بالتكيف */
.quote-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 6px;
    font-size: 0.8rem;
    text-align: center;
    table-layout: auto; /* يجعل الخانات تتكيف حسب طول المحتوى بدلاً من التوزيع الثابت */
}

/* 2. ضبط الخلايا للتوسع تلقائياً مع المحتوى */
.quote-table th, 
.quote-table td {
    white-space: normal; /* يسمح بالنزل لسطر جديد إذا كان النص طويلاً جداً */
    word-break: break-word; /* يمنع خروج الكلمات الطويلة خارج الخلية */
    padding: 4px 6px;
}

/* 3. جعل حقول الإدخال المرنة تتكيف مع حجم النص */
.table-input {
    width: 100%;
    min-width: 50px; /* حد أدنى مناسب لعدم اختفاء الحقل */
    text-align: center;
    font-size: 0.8rem;
    box-sizing: border-box;
}
.table-select {
    width: 100%;
    text-align: center;
    background: transparent;
    border: 1px dashed transparent;
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 700;
    cursor: pointer;
}
.table-select:hover, .table-select:focus {
    background: #ffffff;
    border-color: #3b82f6;
}

.btn-del-row {
    background: #ef4444;
    color: white;
    border: none;
    border-radius: 4px;
    width: 22px;
    height: 22px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    margin: 0 auto;
}
.btn-del-row:hover { background: #dc2626; }

/* 1. زيادة عرض جدول الإجماليات ليأخذ مساحة أكبر وتوسيطه */
.totals-summary-container {
    display: flex;
    justify-content: center;
    margin-top: 6px;
    width: 100%;
}

.totals-table {
    border-collapse: collapse;
    width: 100%; /* اجعل الجدول يمتد بعرض المنطقة المتاحة */
    max-width: 500px; /* أو يمكنك زيادتها لـ 600px إذا أردته أعرض */
    font-size: 0.85rem;
    font-weight: 800;
    table-layout: auto;
}

.totals-table td {
    border: 1px solid #b8960c;
    padding: 6px 12px;
}

.total-label {
    background-color: #3d3010;
    color: #e8c84a;
    font-weight: 800;
    text-align: center;
    width: 60%;
}

/* 2. ضبط خانة القيم لضمان وجود الرمز يسار الرقم دائماً في سطر واحد */
.total-value {
    background-color: rgba(255, 253, 235, 0.9);
    direction: ltr; /* يضمن الترتيب من اليسار لليمين: الرقم ثم الرمز أو العكس */
    font-family: sans-serif;
    color: #1a1a1a;
    text-align: center;
    white-space: nowrap; /* يمنع نزول الرمز أسفل/فوق الرقم نهائياً */
    width: 40%;
}

.grand-total-row .total-label {
    background-color: #5a4a1e;
    color: #f0d060;
}

.grand-total-row .total-value {
    background-color: rgba(218, 172, 20, 0.25);
    font-size: 0.9rem;
    font-weight: 900;
    white-space: nowrap;
}

/* 3. محاذاة وتنسيق الرمز ليكون محاذياً للرقم في سطر واحد */
.riyal-symbol {
    display: inline-block;
    vertical-align: middle;
    margin-right: 4px; /* مسافة بين الرقم والرمز */
}
.closing-wish {
    text-align: center;
    font-size: 1.02rem;
    font-weight: 700;
    color: var(--text-dark);
    margin: 6px 0;
}

/* Centered greeting line */
.greeting-text {
    font-size: 0.95rem;
    font-weight: 700;
    margin-bottom: 4px;
    text-align: center;
}

/* 2-COLUMN BILINGUAL TERMS & CONDITIONS */
.terms-section {
    margin-top: 4px;
    font-size: 0.74rem;
    line-height: 1.3;
}

.bilingual-terms-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
    align-items: start;
}

.terms-col-ar {
    text-align: right;
}

.terms-col-en {
    text-align: left;
    font-family: sans-serif;
}

.terms-title {
    font-size: 0.85rem;
    font-weight: 800;
    margin-bottom: 3px;
    color: var(--text-dark);
}

.terms-title-en {
    font-size: 0.85rem;
    font-weight: 800;
    margin-bottom: 3px;
    color: var(--text-dark);
    font-family: sans-serif;
}

.term-unit {
    margin-bottom: 4px;
}

.term-ar-head {
    font-weight: 800;
    color: var(--text-dark);
}

.term-ar-text, .term-en-text {
    margin-top: 1px;
}

.term-en-head {
    font-weight: 700;
    color: var(--text-dark);
}

.term-list-ar {
    list-style-type: none;
    padding-right: 4px;
}

.term-list-ar li::before {
    content: "• ";
    font-weight: bold;
}

.term-list-en {
    list-style-type: none;
    padding-left: 4px;
}

.term-list-en li::before {
    content: "• ";
    font-weight: bold;
}

/* Page 2 Closing - centered in middle of page */
.closing-salutation {
    margin: 16px 0 8px 0;
    text-align: center;
}

/* Closing at the VERY BOTTOM of page 2 */
.closing-salutation-bottom {
    margin-top: auto;
    padding-top: 12px;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.salutation-text {
    font-size: 1.05rem;
    font-weight: 800;
    margin-bottom: 6px;
    text-align: center;
}

.company-sign-wrapper {
    display: inline-block;
    text-align: center;
    position: relative;
}

.company-sign-title {
    font-size: 0.98rem;
    font-weight: 900;
    color: var(--text-dark);
}

.official-stamp-box {
    margin-top: 2px;
    width: 160px;
    height: 95px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.official-stamp-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.15));
}

.bank-details-section {
    display: flex;
    justify-content: center;
    margin-top: 10px;
    padding: 0 2px;
}

.bank-table {
    border-collapse: collapse;
    width: 60%;
    font-size: 0.82rem;
    font-weight: 800;
}

.bank-table td {
    border: 1px solid #b8960c;
    padding: 5px 12px;
}

.bank-label {
    background-color: #3d3010;
    color: #e8c84a;
    font-weight: 800;
    width: 32%;
    text-align: center;
    padding: 5px;
}

.bank-value {
    background-color: rgba(255, 253, 235, 0.9);
    text-align: center;
    padding: 5px;
    color: #1a1a1a;
}
.font-bold { font-weight: 800; }
.font-en { font-family: sans-serif; font-weight: 700; direction: ltr; }

/* New Saudi Riyal Symbol - Rubik ExtraBold */
.riyal-symbol {
    width: 1em;
    height: 1em;
    display: inline-block;
    vertical-align: middle;
    margin-left: 2px;
}
/* ----------------------------------------------------
   3. FIXED BOTTOM FOOTER (في أسفل الصفحة تماماً في النطاق السفلي)
   ---------------------------------------------------- */
.doc-footer-fixed {
    position: absolute;
    bottom: 5mm;
    left: 14mm;
    right: 14mm;
    text-align: center;
    font-size: 0.85rem;
    font-weight: 800;
    color: var(--primary-blue);
    z-index: 10;
}

.footer-content {
    display: flex;
    justify-content: space-around;
    align-items: center;
}

.footer-sep {
    width: 1px;
    height: 12px;
    background-color: var(--primary-blue);
}

/* ----------------------------------------------------
   Print & PDF Export Media Query Rules (@media print)
   ---------------------------------------------------- */
@media print {
    @page {
        size: A4 portrait;
        margin: 0;
    }

    html, body {
        background: #ffffff !important;
        padding: 0 !important;
        margin: 0 !important;
        width: 210mm;
        height: 297mm;
    }

    .no-print {
        display: none !important;
    }

    .document-wrapper {
        margin: 0 !important;
        gap: 0 !important;
    }

    .a4-page {
        width: 210mm !important;
        height: 296mm !important;
        min-height: 296mm !important;
        max-height: 296mm !important;
        box-shadow: none !important;
        page-break-after: always !important;
        page-break-inside: avoid !important;
        background: url('https://i.postimg.cc/Bbs40NJc/alkhlfyt.jpg') no-repeat center center / 100% 100% !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    .editable-field {
        border: none !important;
        background: transparent !important;
        box-shadow: none !important;
        outline: none !important;
        padding: 0 !important;
    }

    .table-select {
        appearance: none;
        -webkit-appearance: none;
        border: none !important;
    }

    .row-action-col {
        display: none !important;
    }
}
