/* ==========================================================================
   Base Styles for Invoice Maker
   ========================================================================== */

.invoice-page {
    padding: 50px 0;
    background-color: var(--dark-bg);
}

.invoice-container {
    background-color: var(--light-bg);
    border-radius: 20px;
    padding: 30px 40px;
    box-shadow: 0 15px 45px rgba(0, 0, 0, 0.3);
    max-width: 1200px;
    margin: 0 auto;
    border: 1px solid #444;
}

.invoice-main-header {
    text-align: center;
    margin-bottom: 30px;
}

.invoice-main-header h1 {
    font-size: 2.2rem;
    color: var(--text-color);
}

.invoice-main-header h1 i {
    color: var(--primary-color);
    margin-left: 10px;
}

.invoice-main-header p {
    font-size: 1.1rem;
    color: var(--light-text-color);
}


/* ==========================================================================
   Form Layout & Input Styles (Desktop First)
   ========================================================================== */

/* Top Details: Date & Currency */
.top-details-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    justify-content: flex-start;
    padding-bottom: 20px;
    margin-bottom: 20px;
    border-bottom: 1px solid #444;
}

/* Main Header Grid: Store, Customer Info etc. */
.invoice-header-grid {
    display: grid;
    grid-template-columns: 40% 30% 25%;
    gap: 10px;
    margin-bottom: 30px;
    align-items: center; /* Align items vertically */
}

/* --- Grid Column Spans for specific items --- */
.form-group.store-name { grid-column: span 3; }
.form-group.invoice-title { grid-column: span 3; }
.form-group.invoice-number { grid-column: span 2; }
.form-group.customer-name { grid-column: span 4; }
.form-group.customer-phone { grid-column: span 3; }
.form-group.form-group-full { grid-column: 1 / -1; } /* Full width for Address */


/* General Form Group Styling */
.form-group {
    display: flex;
    align-items: center;
}

.form-group label {
    flex-shrink: 0;
    width: 90px; /* Consistent width for labels */
    text-align: left;
    font-size: 0.85rem;
    color: var(--light-text-color);
    padding-left: 5px;
}

.form-group input,
.form-group select,
.form-group textarea {
    flex-grow: 1;
    width: 100%;
    padding: 3px 15px;
    background-color: var(--dark-bg);
    border: 1px solid #444;
    border-radius: 8px;
    color: var(--text-color);
    font-family: var(--font-family);
    font-size: 1rem;
    transition: border-color 0.3s, box-shadow 0.3s;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

/* Special case for multi-line textareas (Notes) to have label on top */
.invoice-notes.form-group {
    flex-direction: column;
    align-items: flex-start;
}
.invoice-notes.form-group label {
    width: auto;
    text-align: right;
    margin-bottom: 8px;
}


/* ==========================================================================
   Invoice Items Table
   ========================================================================== */

.invoice-items-section {
    background-color: #ffffff;
    color: #333;
    padding: 10px;
    border-radius: 12px;
    margin-bottom: 20px;
}

.invoice-items-table {
    width: 100%;
    border-collapse: collapse;
}

.invoice-items-table thead {
    background-color: #f2f2f2;
}

.invoice-items-table th {
    padding: 3px 15px;
    text-align: right;
    font-weight: 600;
    color: #333;
    border-bottom: 2px solid #ddd;
}

.invoice-items-table td {
    padding: 8px 5px;
    vertical-align: middle;
}

.invoice-items-table input {
    width: 100%;
    background: #f7f7f7;
    border: 1px solid #ccc;
    color: #333;
    padding:3px 8px;
    font-size: 1rem;
    border-radius: 4px;
    font-family: var(--font-family);
}

.invoice-items-table input:focus {
    background: #fff;
    outline: none;
    border-color: var(--primary-color);
}

.invoice-items-table tr:not(:last-child) {
    border-bottom: 1px solid #eee;
}

.item-total-price {
    padding-top: 15px;
    text-align: center;
    font-weight: bold;
}

.delete-row-btn {
    background: transparent;
    border: none;
    color: #e74c3c;
    font-size: 1.1rem;
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    transition: background-color 0.2s, color 0.2s;
}

.delete-row-btn:hover {
    background-color: #e74c3c;
    color: #fff;
}

.btn-add-row {
    background-color: #e8f5e9;
    border: 2px dashed #4caf50;
    color: #4caf50;
    width: auto;
    padding: 8px 20px;
    font-size: 0.95rem;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
    display: inline-block;
    font-family:inherit;
}

.btn-add-row:hover {
    background-color: #4caf50;
    color: #fff;
    border-style: solid;
}


/* ==========================================================================
   Invoice Summary & Totals (FIXED)
   ========================================================================== */

.invoice-summary-grid {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 30px;
    margin-top: 20px;
    align-items: start;
}

.invoice-notes.form-group-full {
    grid-column: auto;
}

.invoice-totals .total-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 3px 0;
}

.invoice-totals .total-row:last-child {
    border-bottom: none;
}

.total-label {
    font-weight: 500;
    flex-shrink: 0; /* Prevent label from shrinking */
}

.total-value {
    font-weight: bold;
    font-size: 1.2rem;
    direction: ltr;
}

/* FIX for discount input alignment */
.total-row label.total-label {
    margin-right: 10px;
}
.total-value-input {
    background: var(--dark-bg);
    border: 1px solid #444;
    text-align: left;
    color: var(--text-color);
    font-size: 1.2rem;
    font-weight: bold;
    direction: ltr;
    padding: 8px;
    max-width: 150px;
    border-radius: 4px;
}
.total-value-input:focus {
    background: var(--dark-bg);
    color: var(--text-color);
    border-color: var(--primary-color);
}

.final-total {
    font-size: 1.2rem;
    color: var(--secondary-color);
    font-weight: bold;
}


/* ==========================================================================
   Action Buttons (FIXED)
   ========================================================================== */

.invoice-actions {
    text-align: center;
}

.btn-primary-print {
    display: inline-block;
    padding: 15px 50px;
    font-size: 1.2rem;
    font-weight: bold;
    border-radius: 50px;
    background-image: linear-gradient(45deg, var(--primary-color), #2980b9);
    color: #fff;
    border: none;
    cursor: pointer;
    box-shadow: 0 5px 20px rgba(52, 152, 219, 0.4);
    transition: all 0.3s ease;
    font-family:inherit;
}

.btn-primary-print:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 25px rgba(52, 152, 219, 0.5);
}


/* ==========================================================================
   Responsive Styles
   ========================================================================== */

@media (max-width: 768px) {
    .invoice-header-grid {
        grid-template-columns: 1fr 1fr; /* Simpler 2-column grid for tablets */
    }
    /* Reset all spans for tablet view */
    .invoice-header-grid .form-group {
        grid-column: span 1;
    }
    .invoice-header-grid .form-group.form-group-full {
        grid-column: 1 / -1; /* Full width items take full width */
    }

    .invoice-summary-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 576px) {
    .invoice-container {
        padding: 20px;
    }
    .invoice-header-grid,
    .top-details-grid {
        grid-template-columns: 1fr;
        flex-direction: column;
        gap: 15px;
    }
    .top-details-grid .form-group {
        width: 100%;
    }
    /* Reset all spans for mobile view */
    .invoice-header-grid .form-group {
        grid-column: 1 / -1;
    }

    /* Stack label and input vertically on small screens */
    .form-group {
        flex-direction: column;
        align-items: flex-start;
    }
    .form-group label {
        width: auto;
        text-align: right;
        margin-bottom: 8px;
    }
}

#invoice-preview {
    display: none;
}
/* ==========================================================================
   Print Styles (FIXED)
   ========================================================================== */

@media print {
    @page {
        size: auto;
        margin: 1cm;
    }

    body > * {
        display: none !important;
    }

    #invoice-preview {
        display: none;
    }
    body.is-printing #invoice-preview {
        display: block !important;
    }

    .print-container {
        width: 100%;
        margin: 0;
        padding: 0;
        border: none;
        border-radius: 0;
        box-sizing: border-box;
        font-family: 'Vazirmatn', sans-serif;
        color: #000;
        background: #fff;
    }

    /*  Header Table Styles*/
    .print-info-table {
        width: 100%;
        border-collapse: separate; /* Use separate for spacing */
        border-spacing: 0;
        margin-bottom: 5px;
        font-size: 10pt;
    }
    .print-info-table td {
        padding: 5px 8px;
        vertical-align: middle;
        text-align: right;
        border: none; /* No borders on regular cells */
    }
    .print-info-table .store-name-cell {
        background-color: #e9e9e9 !important;
        -webkit-print-color-adjust: exact;
        color-adjust: exact;
        font-size: 16pt;
        font-weight: 900;
        text-align: center;
        width: 40%;
        border-radius: 8px;
        vertical-align: middle;
    }
    .print-info-table strong {
        margin-left: 5px;
    }
   


    .print-items-table, .print-summary-table {
        border-collapse: separate; /* CRITICAL for border-radius to work */
        border-spacing: 0;
        width: 100%;
        border: 1px solid #aaa;
        border-radius: 8px;
        overflow: hidden;
        
    }

    .print-items-table {
        font-size: 10pt;
    }
    .print-summary-table {
        margin-top: 15px;
        font-size: 10pt;
    }

    .print-items-table th, .print-items-table td,
    .print-summary-table th, .print-summary-table td {
        padding:3px 8px;
        text-align: center;
        vertical-align: middle;
        border-bottom: 1px solid #aaa;
        border-left: 1px solid #aaa;
    }

    .print-items-table tbody tr:last-child td,
    .print-summary-table tbody tr:last-child td {
        border-bottom: none;
    }

    /* FIX: Remove inner-left border on the last cell to prevent double border with table container */
    .print-items-table th:last-child, .print-items-table td:last-child,
    .print-summary-table th:last-child, .print-summary-table td:last-child {
        border-left: none;
    }

/* ... inside @media print ... */
/* ... inside @media print ... */
.print-items-table thead, .print-summary-table tr:first-child {
    background-color: #e9e9e9 !important;
    -webkit-print-color-adjust: exact;
    color-adjust: exact;
}
.print-items-table th, .print-summary-table th {
        font-weight: bold;
        font-size: 9pt;
        border-bottom: 1px solid #aaa;
    }
    .print-summary-table tbody td {
        font-weight: bold;
        font-size: 11pt;
    }

    .print-items-table td:nth-child(2),
    .print-items-table td:last-child {
        text-align: right;
    }

    .print-notes {
        font-size: 10pt;
        min-height: 40px;
        margin-top: 15px;
    }
    .print-notes p {
        margin: 0;
    }

    .print-footer {
        display: flex;
        justify-content: space-between;
        margin-top: 20px;
        font-size: 11pt;
        page-break-inside: avoid;
    }
    .signature-box {
        width: 45%;
        text-align: center;
    }
}

/* ==========================================================================
   Responsive Styles for Invoice Items Table (Mobile View)
   ========================================================================== */

@media (max-width: 768px) {

    /* 1. پنهان کردن هدر اصلی جدول در موبایل */
    .invoice-items-table thead {
        display: none;
    }

    /* 2. تبدیل هر ردیف جدول به یک "کارت" مجزا */
    .invoice-items-table tr {
        display: block;
        position: relative; /* برای موقعیت‌دهی دکمه حذف */
        background-color: #f8f9fa;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 15px;
        padding-left: 50px; /* فضای کافی برای دکمه حذف */
        margin-bottom: 15px;
    }
    
    .invoice-items-table tr:last-child {
        margin-bottom: 0;
    }

    /* 3. تبدیل سلول‌ها به بلاک‌های تمام‌عرض و ایجاد ساختار لیبل-مقدار */
    .invoice-items-table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 10px 0;
        border-bottom: 1px solid #e9ecef;
        text-align: right; /* تراز کردن محتوای سلول به راست */
    }

    /* حذف خط جداکننده برای آخرین سلول در هر کارت */
    .invoice-items-table tr td:last-of-type {
        border-bottom: none;
    }
    
    /* 4. افزودن لیبل‌ها (عناوین ستون) با استفاده از псевдоэлемент ::before */
    .invoice-items-table td::before {
        content: attr(data-label); /* این قسمت در ادامه با جاوااسکریپت یا CSS خالص تکمیل می‌شود */
        font-weight: 600;
        color: #495057;
        padding-left: 10px;
        flex-shrink: 0;
    }
    
    /* تخصیص لیبل به هر ستون به صورت دستی */
    .invoice-items-table td:nth-of-type(1)::before { content: 'نام کالا:'; }
    .invoice-items-table td:nth-of-type(2)::before { content: 'تعداد:'; }
    .invoice-items-table td:nth-of-type(3)::before { content: 'واحد:'; }
    .invoice-items-table td:nth-of-type(4)::before { content: 'قیمت واحد:'; }
    .invoice-items-table td:nth-of-type(5)::before { content: 'قیمت کل:'; }
    .invoice-items-table td:nth-of-type(6)::before { content: 'شرح کالا:'; }
    
    /* 5. استایل‌دهی به محتوای داخل سلول‌ها */
    .invoice-items-table td input,
    .invoice-items-table .item-total-price {
        width: 60%; /* محدود کردن عرض اینپوت‌ها */
        text-align: left; /* تراز کردن اعداد و متن ورودی به چپ */
        direction: ltr;
    }
    
    .invoice-items-table .item-total-price {
        padding: 3px 8px; /* همسان‌سازی با پدینگ اینپوت‌ها */
        text-align: center;
        font-weight: bold;
    }

    /* 6. استایل‌دهی و موقعیت‌دهی مجدد دکمه حذف */
    .invoice-items-table td:nth-of-type(7) {
        display: block; /* برای اینکه بتوانیم دکمه را بیرون بکشیم */
        border: none;
        padding: 0;
        position: absolute;
        top: 10px;
        left: 10px;
        width: auto;
    }
    .invoice-items-table td:nth-of-type(7)::before {
        display: none; /* حذف لیبل برای ستون دکمه حذف */
    }

    .delete-row-btn {
        width: 34px;
        height: 34px;
    }
}


