:root {
    --primary: #7851A9;
    --primary-dark: #5E3F86;
    --primary-deep: #4B326C;
    --primary-light: #F3EEFA;
    --primary-extra-light: #F8F5FC;
    --navy: #3B2856;
    --success: #16A34A;
    --warning: #F59E0B;
    --danger: #DC2626;
    --background: #F7F8FC;
    --card: #FFFFFF;
    --text: #1F2937;
    --muted: #6B7280;
    --border: #E5E7EB;
    --bs-primary-rgb: 120, 81, 169;
    --bs-link-color: #7851A9;
    --bs-link-hover-color: #5E3F86;
}

html, body { min-height: 100%; }
body {
    margin: 0;
    font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--text);
    background: var(--background);
    font-size: 16px;
}

a { text-decoration: none; color: var(--primary); }
a:hover { color: var(--primary-dark); }
.text-primary { color: var(--primary) !important; }
.bg-primary { background-color: var(--primary) !important; }
.border-primary { border-color: var(--primary) !important; }

.form-control:focus, .form-select:focus {
    border-color: #9A7BC1;
    box-shadow: 0 0 0 .25rem rgba(120, 81, 169, .16);
}
.form-check-input:checked {
    background-color: var(--primary);
    border-color: var(--primary);
}
.dropdown-item.active, .dropdown-item:active {
    background-color: var(--primary);
}
.page-link { color: var(--primary); }
.page-link:hover { color: var(--primary-dark); }
.active > .page-link, .page-link.active {
    background-color: var(--primary);
    border-color: var(--primary);
}


.app-shell { min-height: 100vh; }
.sidebar {
    width: 255px;
    min-height: 100vh;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 1040;
    background: linear-gradient(180deg, #4B326C 0%, #2F2145 100%);
    padding: 22px 16px;
}
.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    color: #fff;
    font-size: 20px;
    font-weight: 800;
    margin: 2px 8px 30px;
}
.brand-icon {
    width: 46px;
    height: 46px;
    border-radius: 13px;
    display: grid;
    place-items: center;
    background: var(--primary);
    font-size: 24px;
}
.nav-link-app {
    display: flex;
    align-items: center;
    gap: 14px;
    min-height: 52px;
    padding: 12px 14px;
    margin-bottom: 8px;
    border-radius: 12px;
    color: #CBD5E1;
    font-weight: 600;
}
.nav-link-app i { width: 28px; font-size: 21px; text-align: center; }
.nav-link-app:hover, .nav-link-app.active { color: #fff; background: var(--primary); }
.main-content { margin-left: 255px; min-height: 100vh; }
.topbar {
    min-height: 76px;
    background: #fff;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 28px;
    position: sticky;
    top: 0;
    z-index: 1020;
}
.page-wrap { padding: 28px; }
.page-title { font-size: 29px; font-weight: 800; margin-bottom: 3px; }
.page-subtitle { color: var(--muted); font-size: 15px; }
.card-soft {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, .045);
}
.summary-card { padding: 22px; min-height: 142px; }
.summary-icon {
    width: 56px;
    height: 56px;
    border-radius: 16px;
    display: grid;
    place-items: center;
    font-size: 26px;
    margin-bottom: 18px;
}
.summary-label { color: var(--muted); font-weight: 700; font-size: 15px; }
.summary-value { font-size: 28px; font-weight: 800; margin-top: 6px; }
.icon-blue { color: var(--primary); background: var(--primary-light); }
.icon-green { color: #16A34A; background: #F0FDF4; }
.icon-orange { color: #D97706; background: #FFF7ED; }
.icon-red { color: #DC2626; background: #FEF2F2; }
.icon-violet { color: #8B5FBF; background: #F6F1FB; }

.btn { min-height: 48px; border-radius: 10px; font-weight: 700; padding-left: 18px; padding-right: 18px; }
.btn-icon { display: inline-flex; align-items: center; gap: 9px; justify-content: center; }
.btn-primary { background: var(--primary); border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-success { background: var(--success); border-color: var(--success); }
.btn-danger { background: var(--danger); border-color: var(--danger); }
.form-control, .form-select {
    min-height: 50px;
    border-radius: 10px;
    border-color: #CBD5E1;
    font-size: 16px;
}
textarea.form-control { min-height: 110px; }
.form-label { font-weight: 700; margin-bottom: 8px; }
.table { margin-bottom: 0; }
.table th { color: #334155; font-size: 14px; background: #F8FAFC; white-space: nowrap; }
.table td, .table th { padding: 14px 12px; vertical-align: middle; border-color: var(--border); }
.table td { font-size: 15px; }
.amount-cell { font-variant-numeric: tabular-nums; font-weight: 800; }
.status-show { background: #DCFCE7; color: #166534; }
.status-hide { background: #FEE2E2; color: #991B1B; }
.status-badge { padding: 7px 11px; border-radius: 999px; font-weight: 800; font-size: 13px; }
.total-row { background: var(--primary-light); font-size: 18px; font-weight: 800; }
.empty-state { padding: 48px 15px; text-align: center; color: var(--muted); }
.empty-state i { font-size: 44px; margin-bottom: 12px; color: #94A3B8; }
.mobile-cards { display: none; }

.login-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
    background:
        radial-gradient(circle at 20% 15%, rgba(120,81,169,.14), transparent 34%),
        radial-gradient(circle at 85% 80%, rgba(22,163,74,.08), transparent 35%),
        #F8FAFC;
}
.login-card { width: 100%; max-width: 460px; padding: 34px; }
.login-logo {
    width: 72px;
    height: 72px;
    border-radius: 20px;
    display: grid;
    place-items: center;
    margin: 0 auto 18px;
    color: #fff;
    background: var(--primary);
    font-size: 34px;
    box-shadow: 0 14px 30px rgba(120,81,169,.24);
}
.login-title { text-align: center; font-weight: 800; font-size: 27px; }
.login-subtitle { text-align: center; color: var(--muted); margin-bottom: 28px; }

.modal-content { border: 0; border-radius: 18px; }
.modal-header { padding: 20px 22px; }
.modal-body { padding: 22px; }
.modal-footer { padding: 16px 22px 22px; }

.mobile-menu-btn { display: none; }

@media (max-width: 991.98px) {
    .sidebar { transform: translateX(-100%); transition: transform .22s ease; box-shadow: 12px 0 30px rgba(15,23,42,.15); }
    .sidebar.open { transform: translateX(0); }
    .main-content { margin-left: 0; }
    .mobile-menu-btn { display: inline-flex; width: 48px; min-width: 48px; padding: 0; align-items: center; justify-content: center; }
    .topbar { padding: 0 16px; }
    .page-wrap { padding: 18px 14px 28px; }
    .page-title { font-size: 25px; }
}

@media (max-width: 767.98px) {
    body { font-size: 16px; }
    .desktop-table { display: none; }
    .mobile-cards { display: block; }
    .expense-mobile-card, .vendor-mobile-card {
        padding: 18px;
        margin-bottom: 13px;
    }
    .mobile-record-title { font-size: 18px; font-weight: 800; }
    .mobile-label { color: var(--muted); font-size: 13px; font-weight: 700; margin-bottom: 3px; }
    .mobile-value { font-size: 16px; font-weight: 700; }
    .mobile-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }
    .mobile-actions .btn { width: 100%; min-height: 50px; }
    .summary-card { min-height: 128px; padding: 18px; }
    .summary-icon { width: 50px; height: 50px; font-size: 23px; margin-bottom: 12px; }
    .summary-value { font-size: 23px; }
    .login-card { padding: 28px 22px; }
}

/* Arabic / RTL */
html[dir="rtl"] body { font-family: Tajawal, system-ui, sans-serif; line-height: 1.65; }
html[dir="ltr"] body { font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
html[dir="rtl"] .sidebar { right: 0; left: auto; }
html[dir="rtl"] .main-content { margin-right: 255px; margin-left: 0; }
html[dir="rtl"] .nav-link-app i { text-align: center; }
html[dir="rtl"] .dropdown-menu { text-align: right; }
html[dir="rtl"] .form-control, html[dir="rtl"] .form-select, html[dir="rtl"] textarea { text-align: right; }
html[dir="rtl"] input[type="email"], html[dir="rtl"] input[type="password"], html[dir="rtl"] input[type="number"] { direction: ltr; text-align: right; }
html[dir="rtl"] .amount-cell, html[dir="rtl"] .summary-value { direction: ltr; unicode-bidi: isolate; }
html[dir="rtl"] .fa-arrow-right-from-bracket { transform: scaleX(-1); }
.summary-mini { border:1px solid var(--border); border-radius:14px; padding:18px; display:flex; flex-direction:column; gap:8px; min-height:96px; }
.summary-mini span { color:var(--muted); font-weight:700; }
.summary-mini strong { font-size:24px; font-weight:800; }
.remaining-box { background:#FFF7ED; border-color:#FED7AA; }
@media (max-width:991.98px){ html[dir="rtl"] .sidebar { transform: translateX(100%); box-shadow:-12px 0 30px rgba(15,23,42,.15); } html[dir="rtl"] .sidebar.open { transform:translateX(0); } html[dir="rtl"] .main-content { margin-right:0; } }
@media (max-width:575.98px){ .topbar .language-btn { min-width:48px; padding-left:10px; padding-right:10px; font-size:0; } .topbar .language-btn i { font-size:18px; margin:0!important; } }

/* Attachment preview */
.attachment-preview-body {
    min-height: 62vh;
    background: #F8FAFC;
    padding: 12px;
}
.attachment-preview-frame {
    width: 100%;
    height: 68vh;
    border: 0;
    border-radius: 12px;
    background: #fff;
}
.attachment-image-wrap {
    min-height: 62vh;
    display: flex;
    align-items: center;
    justify-content: center;
}
.attachment-preview-image {
    display: block;
    max-width: 100%;
    max-height: 68vh;
    object-fit: contain;
    border-radius: 12px;
    box-shadow: 0 6px 22px rgba(15, 23, 42, .08);
}
@media (max-width: 767.98px) {
    .attachment-preview-body { min-height: 52vh; padding: 8px; }
    .attachment-preview-frame { height: 58vh; }
    .attachment-image-wrap { min-height: 50vh; }
    .attachment-preview-image { max-height: 58vh; }
    #attachmentPreviewModal .modal-footer {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }
    #attachmentPreviewModal .modal-footer .btn { margin: 0; width: 100%; }
}


/* PWA / installed-app enhancements */
[data-install-app] { min-height: 48px; }
.pwa-standalone body { overscroll-behavior-y: contain; }
@media (display-mode: standalone) {
    .topbar { padding-top: env(safe-area-inset-top); height: calc(76px + env(safe-area-inset-top)); }
    .sidebar { padding-top: env(safe-area-inset-top); }
    .page-wrap { padding-bottom: calc(28px + env(safe-area-inset-bottom)); }
}

/* =========================================================
   Mobile expense modal fix
   Keep Save/Cancel visible and make only the form body scroll.
   This also works correctly when the site is installed as a PWA.
   ========================================================= */
@media (max-width: 767.98px) {
    #expenseModal {
        padding: 0 !important;
    }

    #expenseModal .modal-dialog {
        width: 100%;
        max-width: none;
        height: 100vh;
        height: 100dvh;
        min-height: 100vh;
        min-height: 100dvh;
        margin: 0;
        align-items: stretch;
    }

    #expenseModal .modal-content {
        height: 100vh;
        height: 100dvh;
        max-height: 100vh;
        max-height: 100dvh;
        border-radius: 0;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }

    #expenseModal form {
        height: 100%;
        min-height: 0;
        display: flex;
        flex-direction: column;
    }

    #expenseModal .modal-header {
        flex: 0 0 auto;
        padding: 18px 18px 16px;
        background: #fff;
    }

    #expenseModal .modal-body {
        flex: 1 1 auto;
        min-height: 0;
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
        padding: 18px;
        padding-bottom: 28px;
    }

    #expenseModal .modal-footer {
        flex: 0 0 auto;
        position: relative;
        z-index: 5;
        display: grid;
        grid-template-columns: 1fr 1.4fr;
        gap: 10px;
        margin: 0;
        padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
        background: #fff;
        border-top: 1px solid var(--border);
        box-shadow: 0 -8px 24px rgba(15, 23, 42, .08);
    }

    #expenseModal .modal-footer .btn {
        width: 100%;
        min-height: 52px;
        margin: 0;
    }

    #expenseModal textarea.form-control {
        min-height: 130px;
    }
}
