/* ==========================================================================
   FATOORA SaaS Platform - Single Unified CSS File
   Aesthetic, Modern, Responsive, Multi-Theme (Light & Dark)
   ========================================================================== */

@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;600&display=swap');
@import url('https://fonts.googleapis.com/icon?family=Material+Icons');
@import url('https://cdn.jsdelivr.net/gh/rastikerdar/vazirmatn@v33.003/Vazirmatn-font-face.css');

:root {
    --font-main: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    --font-mono: 'JetBrains Mono', monospace;

    /* Light Theme Palette */
    --bg-page: #f8fafc;
    --bg-card: #ffffff;
    --bg-sidebar: #0f172a;
    --bg-header: #ffffff;
    --border-color: #e2e8f0;
    --border-subtle: #f1f5f9;

    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --text-inverse: #ffffff;

    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-light: #eff6ff;
    --primary-text: #1d4ed8;

    --success: #10b981;
    --success-bg: #ecfdf5;
    --success-text: #047857;

    --warning: #f59e0b;
    --warning-bg: #fffbeb;
    --warning-text: #b45309;

    --danger: #ef4444;
    --danger-bg: #fef2f2;
    --danger-text: #b91c1c;

    --info: #06b6d4;
    --info-bg: #ecfeff;
    --info-text: #0e7490;

    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
    --sidebar-width: 260px;
    --header-height: 70px;
}

[data-theme="dark"] {
    --bg-page: #0b0f19;
    --bg-card: #131b2e;
    --bg-sidebar: #070a12;
    --bg-header: #131b2e;
    --border-color: #1e293b;
    --border-subtle: #1e293b;

    --text-primary: #f8fafc;
    --text-secondary: #cbd5e1;
    --text-muted: #64748b;
    --text-inverse: #0f172a;

    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --primary-light: rgba(59, 130, 246, 0.15);
    --primary-text: #93c5fd;

    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.3);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.4);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.5);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-main);
    background-color: var(--bg-page);
    color: var(--text-primary);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

/* App Layout */
.app-container {
    display: flex;
    min-height: 100vh;
}

/* Sidebar */
.app-sidebar {
    width: var(--sidebar-width);
    background-color: var(--bg-sidebar);
    color: #fff;
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 100;
    transition: transform 0.3s ease;
}

.sidebar-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    padding: 0 1.5rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.brand-logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-weight: 800;
    font-size: 1.25rem;
    color: #fff;
    text-decoration: none;
    letter-spacing: -0.025em;
}

.brand-icon {
    font-size: 1.75rem !important;
    color: #38bdf8;
}

.sidebar-nav {
    flex: 1;
    padding: 1.25rem 0.75rem;
    overflow-y: auto;
}

.nav-section-title {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #64748b;
    padding: 0.75rem 0.75rem 0.25rem;
    font-weight: 700;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 0.85rem;
    padding: 0.65rem 0.85rem;
    color: #94a3b8;
    text-decoration: none;
    border-radius: var(--radius-sm);
    font-weight: 500;
    font-size: 0.9rem;
    transition: all 0.2s ease;
    margin-bottom: 0.2rem;
}

.nav-item:hover {
    color: #fff;
    background-color: rgba(255, 255, 255, 0.06);
}

.nav-item.active {
    color: #fff;
    background-color: var(--primary);
}

.nav-item .material-icons {
    font-size: 1.25rem;
}

.sidebar-footer {
    padding: 1rem;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    font-size: 0.8rem;
    color: #64748b;
}

/* Main Wrapper */
.app-main {
    margin-left: var(--sidebar-width);
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* Header */
.app-header {
    height: var(--header-height);
    background-color: var(--bg-header);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 2rem;
    position: sticky;
    top: 0;
    z-index: 90;
    box-shadow: var(--shadow-sm);
}

.header-search {
    position: relative;
    width: 320px;
}

.header-search input {
    width: 100%;
    padding: 0.5rem 1rem 0.5rem 2.4rem;
    border-radius: 9999px;
    border: 1px solid var(--border-color);
    background: var(--bg-page);
    color: var(--text-primary);
    font-size: 0.875rem;
    outline: none;
    transition: border-color 0.2s;
}

.header-search input:focus {
    border-color: var(--primary);
}

.header-search .material-icons {
    position: absolute;
    left: 0.75rem;
    top: 50%;
    transform: translateY(-50%);
    font-size: 1.1rem;
    color: var(--text-muted);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 1.25rem;
}

.theme-toggle-btn {
    background: none;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
}

.theme-toggle-btn:hover {
    background: var(--border-subtle);
    color: var(--primary);
}

.user-profile-badge {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.35rem 0.75rem;
    border-radius: 9999px;
    border: 1px solid var(--border-color);
    background: var(--bg-card);
    text-decoration: none;
    color: var(--text-primary);
}

.user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.85rem;
}

.user-info {
    display: flex;
    flex-direction: column;
}

.user-name {
    font-size: 0.85rem;
    font-weight: 600;
    line-height: 1.2;
}

.user-role-badge {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: capitalize;
}

/* Page Content */
.page-content {
    padding: 2rem;
    flex: 1;
}

.page-header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 2rem;
}

.page-title {
    font-size: 1.5rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    color: var(--text-primary);
}

.page-subtitle {
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-top: 0.25rem;
}

/* Metric Cards */
.metrics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.metric-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1.25rem 1.5rem;
    box-shadow: var(--shadow-sm);
    display: flex;
    align-items: center;
    justify-content: space-between;
    transition: transform 0.2s, box-shadow 0.2s;
}

.metric-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.metric-info h4 {
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.metric-info .metric-value {
    font-size: 1.75rem;
    font-weight: 800;
    color: var(--text-primary);
    margin: 0.25rem 0;
}

.metric-icon-wrap {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--primary-light);
    color: var(--primary);
}

/* Cards & Containers */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    margin-bottom: 2rem;
    overflow: hidden;
}

.card-header {
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.card-title {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-primary);
}

.card-body {
    padding: 1.5rem;
}

/* Dashboard & Analytics Multi-Column Grids */
.dashboard-grid-main,
.dashboard-grid-secondary {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.reports-grid,
.performance-grid,
.responsive-two-col-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.settings-letterhead-grid {
    display: grid;
    grid-template-columns: minmax(200px, 300px) 1fr;
    gap: 1.5rem;
    align-items: start;
}

/* Tables */
.table-responsive {
    width: 100%;
    overflow-x: auto;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    text-align: left;
}

.data-table th {
    background: var(--bg-page);
    color: var(--text-secondary);
    font-weight: 600;
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
}

.data-table td {
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-primary);
    vertical-align: middle;
}

.data-table tr:hover td {
    background: var(--border-subtle);
}

/* Status Badges */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.25rem 0.65rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: capitalize;
}

.badge-pending { background: var(--warning-bg); color: var(--warning-text); }
.badge-invoiced { background: var(--success-bg); color: var(--success-text); }
.badge-paid { background: var(--success-bg); color: var(--success-text); }
.badge-cancelled { background: var(--danger-bg); color: var(--danger-text); }
.badge-draft { background: var(--border-subtle); color: var(--text-secondary); }
.badge-sent { background: var(--info-bg); color: var(--info-text); }
.badge-overdue { background: var(--danger-bg); color: var(--danger-text); }

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.6rem 1.1rem;
    font-size: 0.875rem;
    font-weight: 600;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.2s;
    font-family: inherit;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--bg-card);
    border-color: var(--border-color);
    color: var(--text-secondary);
}

.btn-secondary:hover {
    background: var(--border-subtle);
    color: var(--text-primary);
}

.btn-danger {
    background: var(--danger);
    color: #fff;
}

.btn-danger:hover {
    background: #dc2626;
}

.btn-sm {
    padding: 0.35rem 0.65rem;
    font-size: 0.8rem;
    border-radius: var(--radius-sm);
}

.btn-icon {
    width: 32px;
    height: 32px;
    padding: 0;
    border-radius: var(--radius-sm);
}

/* Forms */
.form-group {
    margin-bottom: 1.25rem;
}

.form-label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.4rem;
}

.form-control {
    width: 100%;
    padding: 0.65rem 0.85rem;
    font-size: 0.9rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-card);
    color: var(--text-primary);
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    font-family: inherit;
}

.form-control:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1rem;
}

/* Filter & Query Forms */
.filter-form {
    display: flex;
    align-items: flex-end;
    gap: 1rem;
    flex-wrap: wrap;
}

.filter-group {
    width: 200px;
}

.filter-group-select {
    flex: 1 1 320px;
    min-width: 240px;
}

.filter-submit-btn {
    height: 42px;
}

/* Select2 Custom Overrides for unified theme */
.select2-container {
    max-width: 100% !important;
}

.select2-container--default .select2-selection--single {
    height: 42px !important;
    padding: 6px 12px;
    border: 1px solid var(--border-color) !important;
    border-radius: var(--radius-sm) !important;
    background-color: var(--bg-card) !important;
    display: flex !important;
    align-items: center;
}

.select2-container--default .select2-selection--single .select2-selection__rendered {
    color: var(--text-primary) !important;
    line-height: 28px !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
    display: block !important;
    padding-right: 20px !important;
    width: 100% !important;
}

.select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 40px !important;
}

.select2-dropdown {
    border: 1px solid var(--border-color) !important;
    background-color: var(--bg-card) !important;
    box-shadow: var(--shadow-lg) !important;
    border-radius: var(--radius-sm) !important;
    color: var(--text-primary) !important;
}

.select2-results__option {
    color: var(--text-primary) !important;
}

.select2-container--default .select2-results__option--highlighted[aria-selected] {
    background-color: var(--primary) !important;
    color: #fff !important;
}

/* Lightbox Modal System (Instruction Requirement: Mandatory for ALL alerts/dialogs) */
.lightbox-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 23, 42, 0.65);
    backdrop-filter: blur(4px);
    z-index: 1000;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
    animation: fadeIn 0.2s ease forwards;
}

.lightbox-overlay.active {
    display: flex !important;
}

.lightbox-dialog {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 600px;
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
    margin: auto;
    transform: scale(0.95);
    animation: scaleUp 0.2s ease forwards;
}

.lightbox-dialog.large {
    max-width: 850px;
}

.lightbox-dialog.xlarge {
    max-width: 1100px;
}

.lightbox-header {
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.lightbox-title {
    font-size: 1.15rem;
    font-weight: 700;
}

.lightbox-close-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s;
}

.lightbox-close-btn:hover {
    color: var(--text-primary);
}

.lightbox-body {
    padding: 1.5rem;
}

.lightbox-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.75rem;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes scaleUp {
    from { transform: scale(0.95); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

/* Invoice Templates Styling in Web View */
.invoice-container {
    background: #fff;
    color: #1e293b;
    padding: 2.5rem;
    border-radius: 8px;
    font-family: inherit;
    box-shadow: var(--shadow-md);
}

.invoice-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 2rem;
    border-bottom: 2px solid #e2e8f0;
    padding-bottom: 1.5rem;
}

.invoice-title {
    font-size: 2rem;
    font-weight: 800;
    color: var(--primary);
    margin-bottom: 0.5rem;
}

.invoice-parties {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
}

.party-box {
    background: #f8fafc;
    padding: 1.25rem;
    border-radius: 6px;
    border: 1px solid #e2e8f0;
}

.party-name {
    font-size: 1.1rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
}

.invoice-items-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 2rem;
}

.invoice-items-table th {
    background: #0f172a;
    color: #fff;
    padding: 0.75rem 1rem;
    font-size: 0.85rem;
    text-transform: uppercase;
}

.invoice-items-table td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid #e2e8f0;
}

.invoice-totals-wrapper {
    display: flex;
    justify-content: space-between;
    margin-bottom: 2rem;
}

.invoice-totals-table {
    width: 320px;
    border-collapse: collapse;
}

.invoice-totals-table td {
    padding: 0.5rem 0.75rem;
}

.invoice-totals-table tr.grand-total-row td {
    border-top: 2px solid #0f172a;
    font-size: 1.15rem;
    color: var(--primary);
}

.invoice-footer-signatures {
    display: flex;
    justify-content: space-between;
    margin-top: 3rem;
    padding-top: 1rem;
}

.signature-cell, .stamp-cell {
    text-align: center;
    min-width: 160px;
    border-top: 1px dashed #cbd5e1;
    padding-top: 0.5rem;
}

/* Template Themes */
.theme-classic-blue .invoice-title { color: #1e40af; }
.theme-classic-blue .invoice-items-table th { background: #1e40af; }
.theme-modern-slate .invoice-title { color: #334155; }
.theme-modern-slate .invoice-items-table th { background: #334155; }
.theme-emerald-exec .invoice-title { color: #065f46; }
.theme-emerald-exec .invoice-items-table th { background: #065f46; }
.theme-tech-horizon .invoice-title { color: #4338ca; }
.theme-tech-horizon .invoice-items-table th { background: #4338ca; }
.theme-elegant-crimson .invoice-title { color: #991b1b; }
.theme-elegant-crimson .invoice-items-table th { background: #991b1b; }
.theme-warm-amber .invoice-title { color: #d97706; }
.theme-warm-amber .invoice-items-table th { background: #d97706; }
.theme-royal-purple .invoice-title { color: #6b21a8; }
.theme-royal-purple .invoice-items-table th { background: #6b21a8; }
.theme-teal-horizon .invoice-title { color: #0f766e; }
.theme-teal-horizon .invoice-items-table th { background: #0f766e; }

/* Tabs Component (Multi-Tab UI) */
.nav-tabs {
    display: flex;
    gap: 0.5rem;
    border-bottom: 2px solid var(--border-color);
    margin-bottom: 1.5rem;
}

.tab-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.25rem;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-muted);
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    cursor: pointer;
    transition: all 0.2s ease;
    text-decoration: none;
}

.tab-btn:hover {
    color: var(--text-primary);
}

.tab-btn.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.tab-badge {
    background: var(--border-subtle);
    color: var(--text-secondary);
    padding: 0.15rem 0.5rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 700;
}

.tab-btn.active .tab-badge {
    background: var(--primary-light);
    color: var(--primary);
}

.tab-pane {
    display: none;
}

.tab-pane.active {
    display: block;
}

/* ==========================================================================
   Mobile Navigation & Drawer Components
   ========================================================================== */
.mobile-nav-toggle {
    display: none;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    width: 40px;
    height: 40px;
    cursor: pointer;
    transition: background-color 0.2s, color 0.2s;
}

.mobile-nav-toggle:hover {
    background-color: var(--border-subtle);
    color: var(--primary);
}

.sidebar-backdrop {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(3px);
    z-index: 105;
    opacity: 0;
    transition: opacity 0.3s ease;
}

/* ==========================================================================
   Responsive Breakpoints & Mobile Optimization
   ========================================================================== */

/* Tablet & Mobile Screens (<= 992px) */
@media screen and (max-width: 992px) {
    .mobile-nav-toggle {
        display: inline-flex;
    }

    /* Transform Sidebar into Off-Canvas Drawer */
    .app-sidebar {
        transform: translateX(-100%);
        z-index: 110;
        box-shadow: var(--shadow-lg);
    }

    body.sidebar-open .app-sidebar {
        transform: translateX(0);
    }

    body.sidebar-open .sidebar-backdrop {
        display: block;
        opacity: 1;
    }

    body.sidebar-open {
        overflow: hidden;
    }

    /* Collapse main container margin */
    .app-main {
        margin-left: 0 !important;
        width: 100%;
    }

    .app-header {
        padding: 0 1.25rem;
    }

    .header-search {
        width: 240px;
    }

    .page-content {
        padding: 1.25rem;
    }

    .page-header-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
        margin-bottom: 1.5rem;
    }

    .page-header-row > div:last-child {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .metrics-grid {
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 1rem;
    }

    .card {
        margin-bottom: 1.5rem;
    }

    .card-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.75rem;
        padding: 1rem 1.25rem;
    }

    .card-header > div:last-child {
        width: 100%;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .card-header .form-control {
        width: 100% !important;
    }

    /* Multi-column Grids Collapse to 1 Single Column */
    .dashboard-grid-main,
    .dashboard-grid-secondary,
    .reports-grid,
    .performance-grid,
    .responsive-two-col-grid,
    .settings-letterhead-grid {
        grid-template-columns: 1fr !important;
        gap: 1.25rem;
    }

    div[style*="grid-template-columns: 1fr 1fr"],
    div[style*="grid-template-columns: 2fr 1fr"] {
        grid-template-columns: 1fr !important;
    }
}

/* Mobile Handsets (<= 640px) */
@media screen and (max-width: 640px) {
    .app-header {
        padding: 0 0.85rem;
        height: 60px;
    }

    .page-content {
        padding: 0.85rem;
    }

    /* Hide full search input text to keep clean header bar, expand on focus if needed */
    .header-search {
        width: 150px;
    }

    .header-search input {
        font-size: 0.8rem;
        padding-left: 2rem;
    }

    .header-search .material-icons {
        font-size: 1rem;
        left: 0.5rem;
    }

    .header-actions {
        gap: 0.5rem;
    }

    .user-profile-badge {
        padding: 0.2rem 0.4rem;
    }

    .user-info {
        display: none; /* Hide username text on small screens, keep avatar & logout */
    }

    .theme-toggle-btn {
        width: 32px;
        height: 32px;
    }

    .page-title {
        font-size: 1.25rem;
    }

    .page-subtitle {
        font-size: 0.8rem;
    }

    /* Stack Metrics 1 column on small phones */
    .metrics-grid {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }

    .metric-card {
        padding: 1rem;
    }

    .metric-info .metric-value {
        font-size: 1.4rem;
    }

    .metric-icon-wrap {
        width: 40px;
        height: 40px;
    }

    /* Form rows full-width on mobile */
    .form-row {
        grid-template-columns: 1fr !important;
        gap: 0.75rem;
    }

    /* Filter & Search Forms full-width on mobile */
    .filter-form,
    .user-selector-form {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 0.85rem !important;
    }

    .filter-group,
    .filter-group-select {
        width: 100% !important;
        max-width: 100% !important;
        flex: 1 1 100% !important;
    }

    .filter-group-select .select2-container,
    .user-selector-form .select2-container {
        width: 100% !important;
    }

    .filter-submit-btn,
    .user-selector-form .btn {
        width: 100% !important;
        justify-content: center;
    }

    /* Grids strictly 1 column (charts and tables stack 1 per row) */
    .dashboard-grid-main,
    .dashboard-grid-secondary,
    .reports-grid,
    .performance-grid,
    .responsive-two-col-grid,
    .settings-letterhead-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem;
    }

    div[style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
    }

    /* Scrollable Tabs */
    .nav-tabs {
        overflow-x: auto;
        white-space: nowrap;
        flex-wrap: nowrap;
        padding-bottom: 2px;
        -webkit-overflow-scrolling: touch;
    }

    .tab-btn {
        padding: 0.6rem 0.85rem;
        font-size: 0.825rem;
    }

    /* Responsive Data Tables */
    .table-responsive {
        border: 0;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .data-table th, 
    .data-table td {
        padding: 0.65rem 0.75rem;
        font-size: 0.8rem;
    }

    /* Modals & Dialogs */
    .lightbox-overlay {
        padding: 0.5rem;
    }

    .lightbox-dialog {
        max-height: 94vh;
        border-radius: var(--radius-md);
    }

    .lightbox-header {
        padding: 1rem;
    }

    .lightbox-body {
        padding: 1rem;
    }

    .lightbox-footer {
        padding: 0.75rem 1rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .lightbox-footer .btn {
        flex: 1 1 calc(50% - 0.5rem);
        min-width: 100px;
    }

    /* Action buttons in forms and headers */
    .btn {
        padding: 0.5rem 0.85rem;
        font-size: 0.825rem;
    }

    /* Invoice & Quotation Totals Section */
    .invoice-totals-wrapper {
        flex-direction: column;
        gap: 1.5rem;
    }

    .invoice-totals-table {
        width: 100%;
    }

    .invoice-footer-signatures {
        flex-direction: column;
        gap: 2rem;
    }

    /* Business Settings Template Grid */
    .template-grid {
        grid-template-columns: 1fr !important;
    }

    /* Line item rows inside Quote / Invoice Builder */
    .line-item-row {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.5rem;
        background: var(--border-subtle);
        padding: 0.75rem;
        border-radius: var(--radius-sm);
        margin-bottom: 0.75rem;
    }
}

/* ===================================
   MULTI-LANGUAGE & RTL SUPPORT
   =================================== */

:root[dir="ltr"] {
    --text-align-start: left;
    --text-align-end: right;
    --margin-start: margin-left;
    --margin-end: margin-right;
    --padding-start: padding-left;
    --padding-end: padding-right;
    --border-start: border-left;
    --border-end: border-right;
}

:root[dir="rtl"] {
    --text-align-start: right;
    --text-align-end: left;
    --margin-start: margin-right;
    --margin-end: margin-left;
    --padding-start: padding-right;
    --padding-end: padding-left;
    --border-start: border-right;
    --border-end: border-left;
    --font-main: 'Vazirmatn', 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* Language Switcher Styles - Matching Theme Toggle Button */
.language-switcher {
    display: inline-flex;
    align-items: center;
    position: relative;
    gap: 0;
}

.language-toggle-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.25rem;
    min-width: 45px;
    height: 38px;
    padding: 0 0.5rem;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-primary);
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s ease;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.language-toggle-btn:hover {
    background-color: var(--border-subtle);
    border-color: var(--primary);
    color: var(--primary);
}

.language-toggle-btn.active {
    background-color: var(--primary-light);
    border-color: var(--primary);
    color: var(--primary);
}

.language-toggle-btn .material-icons {
    font-size: 1.25rem;
    transition: transform 0.2s ease;
}

.language-toggle-btn.active .material-icons {
    transform: rotate(180deg);
}

.language-abbr {
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
}

.language-dropdown-menu {
    display: none;
    position: absolute;
    top: calc(100% + 0.5rem);
    right: 0;
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    z-index: 1000;
    min-width: 200px;
    overflow: hidden;
}

.language-dropdown-menu.active {
    display: block;
    animation: slideDown 0.2s ease;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.language-option {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    width: 100%;
    padding: 0.75rem 1rem;
    border: none;
    background-color: transparent;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.15s ease;
    font-size: 0.9375rem;
    text-align: left;
}

.language-option:hover {
    background-color: var(--border-subtle);
    color: var(--primary);
}

.language-option[data-active="true"] {
    background-color: var(--primary-light);
    color: var(--primary);
    font-weight: 600;
}

.language-option-abbr {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 32px;
    height: 32px;
    background-color: rgba(37, 99, 235, 0.1);
    border-radius: 4px;
    font-weight: bold;
    font-size: 0.8125rem;
}

.language-option[data-active="true"] .language-option-abbr {
    background-color: var(--primary);
    color: white;
}

.language-option-name {
    flex: 1;
}

.language-option .check-icon {
    font-size: 1.25rem;
    color: var(--primary);
    margin-left: auto;
}

.language-select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background: none;
    background-color: transparent;
    border: none;
    color: var(--text-primary);
    width: 0;
    height: 0;
    padding: 0;
    cursor: pointer;
    position: absolute;
    opacity: 0;
    top: 0;
    left: 0;
    z-index: -1;
}

.language-select option {
    background-color: var(--bg-card);
    color: var(--text-primary);
    padding: 0.5rem;
    border: none;
}

/* Dark Theme */
[data-theme="dark"] .language-toggle-btn {
    background-color: var(--bg-card);
    color: var(--text-primary);
}

[data-theme="dark"] .language-toggle-btn:hover {
    background-color: var(--border-subtle);
    border-color: var(--primary);
}

[data-theme="dark"] .language-dropdown-menu {
    background-color: var(--bg-card);
}

/* RTL Adjustments */
[dir="rtl"] .language-toggle-btn {
    flex-direction: row-reverse;
}

[dir="rtl"] .language-toggle-btn .material-icons {
    margin-right: -0.25rem;
}

[dir="rtl"] .language-dropdown-menu {
    right: auto;
    left: 0;
}

[dir="rtl"] .language-option {
    flex-direction: row-reverse;
    text-align: right;
}

[dir="rtl"] .language-option .check-icon {
    margin-left: 0;
    margin-right: auto;
}

/* RTL Layout Adjustments */
[dir="rtl"] .app-sidebar {
    right: 0;
    left: auto;
    border-left: none;
    border-right: 1px solid var(--border-subtle);
}

[dir="rtl"] .app-main {
    margin-left: 0;
    margin-right: var(--sidebar-width, 250px);
}

[dir="rtl"] .sidebar-backdrop {
    left: auto;
    right: 0;
}

[dir="rtl"] .mobile-nav-toggle {
    margin-right: auto;
    margin-left: 0;
}

[dir="rtl"] .header-search {
    margin-left: auto;
    margin-right: 0;
}

[dir="rtl"] .header-search input {
    padding-left: 0.75rem;
    padding-right: 2rem;
}

[dir="rtl"] .header-search .material-icons {
    left: auto;
    right: 0.75rem;
}

[dir="rtl"] .user-profile-badge {
    flex-direction: row-reverse;
}

[dir="rtl"] .user-avatar {
    margin-left: 0;
    margin-right: 0.75rem;
}

[dir="rtl"] .sidebar-nav a {
    text-align: right;
    padding-left: 0.75rem;
    padding-right: 1rem;
}

[dir="rtl"] .sidebar-nav a .material-icons {
    margin-right: 0;
    margin-left: 0.75rem;
    order: 2;
}

[dir="rtl"] .sidebar-nav a span,
[dir="rtl"] .sidebar-nav a div {
    order: 1;
}

[dir="rtl"] .nav-item {
    flex-direction: row-reverse;
    text-align: right;
    justify-content: flex-end;
    padding-left: 1rem;
    padding-right: 0.85rem;
}

[dir="rtl"] .nav-item .material-icons {
    margin-left: 0.85rem;
    margin-right: 0;
}

[dir="rtl"] .nav-section-title {
    text-align: right;
}

[dir="rtl"] .page-content {
    padding-right: 1.5rem;
    padding-left: 1rem;
}

[dir="rtl"] .form-group {
    text-align: right;
}

[dir="rtl"] .form-label {
    text-align: right;
}

[dir="rtl"] .form-control {
    text-align: right;
}

[dir="rtl"] textarea {
    text-align: right;
}

[dir="rtl"] .data-table th,
[dir="rtl"] .data-table td {
    text-align: right;
}

[dir="rtl"] .btn {
    text-align: center;
}

[dir="rtl"] .lightbox-dialog {
    direction: rtl;
}

[dir="rtl"] .modal-header {
    flex-direction: row-reverse;
}

[dir="rtl"] .modal-close-btn {
    margin-left: 0;
    margin-right: auto;
}

[dir="rtl"] .checkbox,
[dir="rtl"] .radio {
    flex-direction: row-reverse;
}

[dir="rtl"] .checkbox input,
[dir="rtl"] .radio input {
    margin-right: 0;
    margin-left: 0.5rem;
}

/* Language-specific Font Adjustments */
html[lang="ar"],
html[lang="fa"] {
    font-family: 'Segoe UI', 'Tahoma', 'Arial', sans-serif;
}

html[lang="ar"] {
    font-weight: 500;
}

html[lang="fa"] {
    font-weight: 400;
}

/* Transition for Language/RTL Changes */
html[data-language-transition] {
    transition: direction 0.3s ease;
}

html[data-language-transition] * {
    transition: text-align 0.2s ease, margin-left 0.2s ease, margin-right 0.2s ease, padding-left 0.2s ease, padding-right 0.2s ease;
}

/* Tab Order and Accessibility for RTL */
[dir="rtl"] .flex-row {
    flex-direction: row-reverse;
}

[dir="rtl"] .flex-col {
    flex-direction: column;
}

[dir="rtl"] .gap-1 { gap: 0.25rem; }
[dir="rtl"] .gap-2 { gap: 0.5rem; }
[dir="rtl"] .gap-3 { gap: 0.75rem; }
[dir="rtl"] .gap-4 { gap: 1rem; }

/* Number and Currency Formatting - LTR always for numbers */
.number, .currency, .price, .amount, .percentage {
    direction: ltr;
    unicode-bidi: embed;
}

/* RTL Responsive Adjustments */
@media (max-width: 768px) {
    [dir="rtl"] .app-sidebar {
        width: var(--sidebar-width, 250px);
    }

    [dir="rtl"] .app-main {
        margin-left: 0;
        margin-right: var(--sidebar-width, 250px);
    }

    [dir="rtl"] .sidebar-backdrop.active ~ .app-main {
        margin-right: 0;
    }

    [dir="rtl"] .language-select {
        padding: 0.4rem 0.5rem;
        font-size: 0.8rem;
    }
}


