:root[data-theme="dark"] {
    --bg-base: #07090e;
    --panel-bg: rgba(18, 24, 38, 0.95);
    --border-color: rgba(255, 255, 255, 0.08);
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --accent: #0ea5e9;
    --accent-hover: #0284c7;
    --input-bg: rgba(15, 23, 42, 0.7);
}

:root[data-theme="light"] {
    --bg-base: #f1f5f9;
    --panel-bg: rgba(255, 255, 255, 0.98);
    --border-color: rgba(0, 0, 0, 0.08);
    --text-main: #0f172a;
    --text-muted: #64748b;
    --accent: #0284c7;
    --accent-hover: #0369a1;
    --input-bg: #f8fafc;
}

* {
    box-sizing: border-box;
    font-family: 'Vazirmatn', sans-serif;
    margin: 0;
    padding: 0;
}

body {
    background-color: var(--bg-base);
    color: var(--text-main);
    min-height: 100vh;
    overflow-x: hidden;
}

/* هدر ابزارهای شناور در بالا */
.top-floating-tools {
    position: fixed;
    top: 1.25rem;
    left: 1.25rem;
    display: flex;
    gap: 0.75rem;
    z-index: 1000;
}

.tool-btn {
    background: var(--panel-bg);
    border: 1px solid var(--border-color);
    border-radius: 50%;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--accent);
    backdrop-filter: blur(10px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    transition: all 0.3s ease;
}

.tool-btn:hover {
    border-color: var(--accent);
    transform: scale(1.1) rotate(15deg);
}

.tool-btn svg {
    width: 20px;
    height: 20px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.n1n-quick-menu {
    position: relative;
}

/* اصلاح منوی کشویی برای اینکه از صفحه خارج نشود */
.n1n-fab-dropdown {
    position: absolute;
    top: 54px;
    left: 0;
    right: auto;
    background: var(--panel-bg);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    width: 180px;
    box-shadow: 0 15px 35px rgba(0,0,0,0.25);
    backdrop-filter: blur(16px);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-8px);
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    z-index: 1100;
}

.n1n-fab-dropdown.active {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.n1n-fab-dropdown a {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.5rem 0.7rem;
    border-radius: 8px;
    text-decoration: none;
    color: var(--text-main);
    font-size: 0.8rem;
    font-weight: 700;
    transition: background 0.2s, color 0.2s;
}

.n1n-fab-dropdown a svg {
    width: 16px;
    height: 16px;
    stroke: var(--accent);
}

.n1n-fab-dropdown a:hover, .n1n-fab-dropdown a.active {
    background: rgba(14, 165, 233, 0.1);
    color: var(--accent);
}

.app-wrapper {
    display: flex;
    min-height: 100vh;
    width: 100%;
}

/* پنل کنترل سمت راست */
.control-panel {
    width: 48%;
    background: var(--panel-bg);
    border-left: 1px solid var(--border-color);
    padding: 2rem 1.25rem;
    overflow-y: auto;
    max-height: 100vh;
    backdrop-filter: blur(16px);
}

.panel-header {
    margin-bottom: 2rem;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 1rem;
}

.panel-header h2 {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--accent);
}

.form-section {
    margin-bottom: 1.5rem;
    background: rgba(0,0,0,0.02);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1rem;
}

.form-section h3 {
    font-size: 0.9rem;
    margin-bottom: 0.8rem;
    color: var(--accent);
}

.section-title-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.8rem;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    grid-column: span 1;
}

.form-group.full-width {
    grid-column: span 2;
}

label {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 700;
}

input, select, textarea {
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.5rem 0.7rem;
    color: var(--text-main);
    font-size: 0.85rem;
    outline: none;
    width: 100%;
    transition: border-color 0.3s;
}

input:focus, select:focus, textarea:focus {
    border-color: var(--accent);
}

.btn-primary {
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 0.4rem 0.8rem;
    border-radius: 8px;
    font-weight: 700;
    cursor: pointer;
    font-size: 0.78rem;
}

.btn-danger {
    background: rgba(239, 68, 68, 0.1);
    color: #ef4444;
    border: 1px solid rgba(239, 68, 68, 0.2);
    border-radius: 6px;
    width: 28px;
    height: 28px;
    cursor: pointer;
    font-weight: 700;
}

.items-table-ctrl {
    width: 100%;
    border-collapse: collapse;
}

.items-table-ctrl th, .items-table-ctrl td {
    padding: 0.25rem;
    font-size: 0.78rem;
}

.items-table-ctrl input {
    width: 100%;
    font-size: 0.8rem;
    padding: 0.4rem;
}

/* پنل پیش‌نمایش فاکتور */
.invoice-preview-container {
    width: 52%;
    padding: 2rem 1rem;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    overflow-y: auto;
    max-height: 100vh;
}

.invoice-sheet {
    background: #ffffff;
    color: #111827;
    width: 210mm;
    min-height: 297mm;
    padding: 15mm;
    box-shadow: 0 20px 40px rgba(0,0,0,0.2);
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    position: relative;
    box-sizing: border-box;
}

/* استایل قالب استاندارد فاکتور */
.inv-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 2px solid #0ea5e9;
    padding-bottom: 1.2rem;
    margin-bottom: 1.2rem;
}

.invoice-main-title-text {
    font-size: 1.5rem;
    font-weight: 700;
    color: #0284c7;
}

.inv-meta-box {
    background: #f0f9ff;
    border: 1.5px solid #bae6fd;
    padding: 0.7rem 1.2rem;
    border-radius: 10px;
    text-align: left;
    min-width: 200px;
}

.inv-title-badge {
    font-weight: 700;
    color: #0369a1;
    font-size: 0.9rem;
    margin-bottom: 0.3rem;
    text-align: center;
    border-bottom: 1px dashed #7dd3fc;
    padding-bottom: 0.2rem;
}

.meta-row {
    display: flex;
    justify-content: space-between;
    font-size: 0.78rem;
    margin-bottom: 0.15rem;
    color: #334155;
}

.inv-parties-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin-bottom: 1.2rem;
}

.party-box {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-right: 4px solid #0ea5e9;
    border-radius: 6px;
    padding: 0.8rem 1rem;
    font-size: 0.82rem;
    line-height: 1.6;
}

.inv-items-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 1.2rem;
}

.inv-items-table th, .inv-items-table td {
    border: 1px solid #cbd5e1;
    padding: 0.6rem;
    font-size: 0.82rem;
    text-align: center;
    vertical-align: middle;
}

.inv-items-table th {
    background: #0ea5e9;
    color: #ffffff;
}

.inv-items-table td:nth-child(2) {
    text-align: right;
    padding-right: 0.8rem;
}

.inv-summary-section {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 1.5rem;
    gap: 1rem;
}

.inv-notes-area {
    flex: 1;
    font-size: 0.78rem;
    color: #334155;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    padding: 0.7rem;
    border-radius: 6px;
    min-height: 75px;
}

.inv-notes-area strong {
    display: block;
    margin-bottom: 0.2rem;
}

.inv-totals-box {
    width: 270px;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    overflow: hidden;
}

.total-row {
    display: flex;
    justify-content: space-between;
    padding: 0.45rem 0.75rem;
    font-size: 0.82rem;
    border-bottom: 1px solid #e2e8f0;
    background: #f8fafc;
}

.total-row:last-child {
    border-bottom: none;
}

.total-row.final-payable {
    background: #0f172a;
    color: #ffffff;
    font-weight: 700;
    font-size: 0.9rem;
}

.inv-signatures {
    display: flex;
    justify-content: space-between;
    margin-bottom: 1.2rem;
}

.sig-box {
    width: 45%;
    border: 1px dashed #94a3b8;
    height: 75px;
    border-radius: 6px;
    text-align: center;
    padding-top: 0.4rem;
    font-size: 0.72rem;
    color: #64748b;
}

.action-bar-preview {
    margin-bottom: 1rem;
}

.btn-print-large {
    width: 100%;
    background: linear-gradient(135deg, #0ea5e9, #0284c7);
    color: white;
    border: none;
    padding: 0.9rem;
    border-radius: 10px;
    font-size: 0.95rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 10px 25px -5px rgba(14, 165, 233, 0.4);
    transition: transform 0.2s;
}

.btn-print-large:hover {
    transform: translateY(-2px);
}

.inv-brand-footer {
    text-align: center;
    font-size: 0.72rem;
    color: #64748b;
    border-top: 1px solid #e2e8f0;
    padding-top: 0.6rem;
}

.inv-brand-footer a {
    color: #0284c7;
    text-decoration: none;
    font-weight: 700;
}

/* ریسپانسیو موبایل و تبلت */
@media (max-width: 992px) {
    .app-wrapper {
        flex-direction: column;
    }
    .control-panel {
        width: 100%;
        max-height: none;
        border-left: none;
        border-bottom: 1px solid var(--border-color);
        padding: 1.5rem 1rem;
    }
    .invoice-preview-container {
        width: 100%;
        padding: 1rem 0.5rem;
        background: rgba(0,0,0,0.1);
        max-height: none;
    }
    .invoice-sheet {
        width: 100% !important;
        max-width: 100% !important;
        min-height: auto !important;
        padding: 10mm !important;
    }
    .form-grid {
        grid-template-columns: 1fr;
    }
    .form-group.full-width, .form-group {
        grid-column: span 1;
    }
}

/* Print CSS */
@media print {
    .no-print, .control-panel, .top-floating-tools {
        display: none !important;
    }
    body, .app-wrapper, .invoice-preview-container {
        background: white !important;
        padding: 0 !important;
        margin: 0 !important;
        width: 100% !important;
        overflow: visible !important;
    }
    .invoice-sheet {
        box-shadow: none !important;
        width: 100% !important;
        min-height: 100vh !important;
        padding: 10mm !important;
        margin: 0 !important;
    }
}

/* استایل سفارشی و مدرن برای آپلود فایل */
.file-upload-wrapper {
    position: relative;
    overflow: hidden;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    background: var(--input-bg);
    border: 1px dashed var(--border-color);
    border-radius: 8px;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    transition: border-color 0.3s;
}

.file-upload-wrapper:hover {
    border-color: var(--accent);
}

.file-upload-wrapper input[type="file"] {
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
}

.file-upload-btn {
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 0.4rem 0.8rem;
    border-radius: 6px;
    font-size: 0.78rem;
    font-weight: 700;
    pointer-events: none;
}

.file-upload-name {
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* کنترل ابعاد و وسط‌چین کردن لوگو در بالای فاکتور (حداکثر 512x512 پیکسل) */
.inv-logo-top-center {
    text-align: center;
    margin-bottom: 0.8rem;
}

.inv-logo-top-center img {
    max-width: 100px;
    max-height: 100px;
    width: 100%;
    height: auto;
    object-fit: contain;
    display: block;
    margin: 0 auto;
}

/* ================= بهینه‌سازی ریسپانسیو پیش‌نمایش فاکتور در موبایل ================= */
@media (max-width: 768px) {
    .invoice-sheet {
        padding: 8mm !important;
        font-size: 0.75rem !important;
    }

    /* سربرگ فاکتور در موبایل: تبدیل به ستون تکی با تراز وسط */
    .inv-header {
        flex-direction: column-reverse;
        align-items: center;
        text-align: center;
        gap: 1rem;
        padding-bottom: 0.8rem;
        margin-bottom: 0.8rem;
    }

    .invoice-main-title-text {
        font-size: 1.15rem !important;
        text-align: center;
    }

    .inv-meta-box {
        width: 100% !important;
        text-align: center !important;
    }

    .meta-row {
        justify-content: center;
        gap: 1rem;
    }

    /* باکس‌های فروشنده و خریدار در موبایل زیر هم قرار گیرند */
    .inv-parties-grid {
        grid-template-columns: 1fr !important;
        gap: 0.6rem !important;
        margin-bottom: 0.8rem;
    }

    .party-box {
        font-size: 0.75rem !important;
    }

    /* جدول اقلام در موبایل با اسکرول افقی ایمن برای جلوگیری از به هم ریختگی */
    .invoice-sheet .inv-items-table {
        display: block;
        width: 100%;
        overflow-x: auto;
        white-space: nowrap;
    }

    .inv-items-table th, .inv-items-table td {
        padding: 0.4rem !important;
        font-size: 0.72rem !important;
    }

    /* بخش محاسبات و توضیحات در موبایل زیر هم قرار گیرند */
    .inv-summary-section {
        flex-direction: column-reverse;
        gap: 0.8rem !important;
        margin-bottom: 1rem !important;
    }

    .inv-totals-box {
        width: 100% !important;
    }

    .inv-notes-area {
        width: 100% !important;
        min-height: 50px !important;
    }

    /* مهر و امضا در موبایل */
    .inv-signatures {
        flex-direction: column;
        gap: 0.6rem;
        margin-bottom: 0.8rem;
    }

    .sig-box {
        width: 100% !important;
        height: 60px !important;
    }

    .btn-print-large {
        padding: 0.75rem !important;
        font-size: 0.85rem !important;
    }
}

/* استایل کادر امضای فروشنده و تصویر آن */
.seller-sig-box {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.seller-sig-box img {
    position: absolute;
    max-height: 55px;
    max-width: 120px;
    object-fit: contain;
    bottom: 5px;
    pointer-events: none;
}
/* استایل ردیف آپلود همراه با دکمه حذف */
.file-upload-container-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.btn-remove-file {
    background: rgba(239, 68, 68, 0.1);
    color: #ef4444;
    border: 1px solid rgba(239, 68, 68, 0.2);
    border-radius: 8px;
    padding: 0 0.9rem;
    height: 38px;
    font-size: 0.8rem;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.btn-remove-file:hover {
    background: #ef4444;
    color: #ffffff;
}