/*
 * ثيم التطبيق: كل الألوان والمقاسات هنا كمتغيرات، ولا ألوان مباشرة في الصفحات.
 */

:root {
    --app-font: 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;

    --app-primary: #1f5f8b;
    --app-primary-rgb: 31, 95, 139;
    --app-primary-hover: #184c70;
    --app-primary-soft: #e6eff6;

    --app-bg: #f3f5f8;
    --app-surface: #ffffff;
    --app-text: #1d2733;
    --app-muted: #677483;
    --app-border: #dde2e8;
    --app-month-alt: #edf2f7;

    --app-success: #1e7d4f;
    --app-danger: #b3261e;
    --app-warning: #9a6700;

    --app-sidebar-bg: #15293b;
    --app-sidebar-text: #b9c7d4;
    --app-sidebar-muted: #7d8fa1;
    --app-sidebar-hover-bg: rgba(255, 255, 255, 0.06);
    --app-sidebar-active-bg: rgba(255, 255, 255, 0.12);
    --app-sidebar-active-text: #ffffff;
    --app-sidebar-width: 250px;

    /* شاشة الدخول (ألوان هوية الشركة كما في نظام ريدي مكس) */
    --app-auth-navy: #0b2e4f;
    --app-auth-navy-deep: #071f36;
    --app-auth-steel: #1e4e7a;
    --app-auth-panel-bg: #e7e9ec;
    --app-auth-brand-text: #ffffff;
    --app-auth-texture: rgba(255, 255, 255, 0.05);
    --app-auth-shadow: rgba(11, 46, 79, 0.12);

    --app-topbar-height: 56px;
    --app-radius: 10px;

    /* ربط متغيرات Bootstrap بالثيم */
    --bs-body-font-family: var(--app-font);
    --bs-body-bg: var(--app-bg);
    --bs-body-color: var(--app-text);
    --bs-border-color: var(--app-border);
    --bs-secondary-color: var(--app-muted);
    --bs-primary: var(--app-primary);
    --bs-primary-rgb: var(--app-primary-rgb);
    --bs-link-color: var(--app-primary);
    --bs-link-color-rgb: var(--app-primary-rgb);
    --bs-link-hover-color: var(--app-primary-hover);
    --bs-border-radius: var(--app-radius);
}

body {
    background: var(--app-bg);
    color: var(--app-text);
    font-family: var(--app-font);
}

.btn-primary {
    --bs-btn-bg: var(--app-primary);
    --bs-btn-border-color: var(--app-primary);
    --bs-btn-hover-bg: var(--app-primary-hover);
    --bs-btn-hover-border-color: var(--app-primary-hover);
    --bs-btn-active-bg: var(--app-primary-hover);
    --bs-btn-active-border-color: var(--app-primary-hover);
    --bs-btn-disabled-bg: var(--app-primary);
    --bs-btn-disabled-border-color: var(--app-primary);
}

.btn-outline-primary {
    --bs-btn-color: var(--app-primary);
    --bs-btn-border-color: var(--app-primary);
    --bs-btn-hover-bg: var(--app-primary);
    --bs-btn-hover-border-color: var(--app-primary);
    --bs-btn-active-bg: var(--app-primary-hover);
    --bs-btn-active-border-color: var(--app-primary-hover);
}

.form-control:focus,
.form-select:focus {
    border-color: var(--app-primary);
    box-shadow: 0 0 0 0.2rem rgba(var(--app-primary-rgb), 0.15);
}

.card {
    background: var(--app-surface);
    border-color: var(--app-border);
    border-radius: var(--app-radius);
}

.form-card {
    max-width: 560px;
}

.text-muted-app {
    color: var(--app-muted) !important;
}

.num {
    direction: ltr;
    unicode-bidi: isolate;
    font-variant-numeric: tabular-nums;
}

/* خلايا الأرقام في الجداول: بداية الصف العربي (نهاية الاتجاه ltr) لتحاذي عنوان العمود وتصطف الكسور */
.table-app td.num {
    text-align: end;
}

/* ===== الشريط العلوي ===== */
.app-topbar {
    position: sticky;
    top: 0;
    z-index: 1030;
    height: var(--app-topbar-height);
    background: var(--app-surface);
    border-bottom: 1px solid var(--app-border);
    padding-inline: 1rem;
}

.app-brand {
    color: var(--app-text);
    font-weight: 600;
    text-decoration: none;
}

.app-brand:hover {
    color: var(--app-primary);
}

/* ===== الهيكل ===== */
.app-shell {
    display: flex;
    min-height: calc(100vh - var(--app-topbar-height));
}

.app-main {
    flex: 1;
    min-width: 0;
    position: relative;
}

.page-title {
    font-weight: 600;
}

/* ===== القائمة الجانبية ===== */
.app-sidebar {
    --bs-offcanvas-width: var(--app-sidebar-width);
    --bs-offcanvas-bg: var(--app-sidebar-bg);
    --bs-offcanvas-color: var(--app-sidebar-text);
    background: var(--app-sidebar-bg);
    color: var(--app-sidebar-text);
}

@media (min-width: 992px) {
    /* Bootstrap يجعل خلفية offcanvas-lg شفافة على الشاشات الكبيرة */
    .app-sidebar.offcanvas-lg {
        background-color: var(--app-sidebar-bg) !important;
    }

    .app-sidebar {
        width: var(--app-sidebar-width);
        flex-shrink: 0;
        position: sticky;
        top: var(--app-topbar-height);
        height: calc(100vh - var(--app-topbar-height));
        overflow-y: auto;
    }
}

/* على الجوال: القائمة بارتفاع الشاشة، والروابط تتمرر داخلها إذا طالت */
@media (max-width: 991.98px) {
    .app-sidebar .app-nav {
        flex: 1 1 auto;
        min-height: 0;
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }
}

.app-sidebar .offcanvas-header {
    border-bottom: 1px solid var(--app-sidebar-hover-bg);
}

.app-sidebar .btn-close {
    filter: invert(1);
}

.app-nav {
    padding: 0.75rem;
    width: 100%;
}

.app-nav-section {
    color: var(--app-sidebar-muted);
    font-size: 0.75rem;
    font-weight: 600;
    padding: 1rem 0.75rem 0.4rem;
}

.app-nav-link {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.55rem 0.75rem;
    border-radius: 8px;
    color: var(--app-sidebar-text);
    text-decoration: none;
    margin-bottom: 2px;
}

.app-nav-link:hover {
    background: var(--app-sidebar-hover-bg);
    color: var(--app-sidebar-active-text);
}

.app-nav-link.active {
    background: var(--app-sidebar-active-bg);
    color: var(--app-sidebar-active-text);
    font-weight: 500;
}

.app-nav-link .bi {
    font-size: 1.05rem;
}

/* ===== صفحات الدخول ===== */
/* شاشة مقسومة: لوحة الشعار الكحلية + لوحة النموذج (مطابقة لنظام ريدي مكس) */
.layout-auth {
    background: var(--app-auth-panel-bg);
}

.auth-shell {
    display: flex;
    min-height: 100vh;
}

.auth-brand {
    position: relative;
    flex: 0 0 55%;
    max-width: 55%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    padding: 48px;
    background: linear-gradient(160deg, var(--app-auth-navy-deep), var(--app-auth-navy));
    color: var(--app-auth-brand-text);
}

.auth-brand-texture {
    position: absolute;
    inset: 0;
    background-image: repeating-linear-gradient(
        135deg,
        var(--app-auth-texture) 0,
        var(--app-auth-texture) 2px,
        transparent 2px,
        transparent 24px
    );
    pointer-events: none;
}

.auth-brand-inner {
    position: relative;
    z-index: 1;
    text-align: center;
    max-width: 420px;
}

.auth-brand-logo {
    max-height: 88px;
    width: auto;
    max-width: 100%;
    margin-bottom: 24px;
}

.auth-brand-name {
    font-size: 1.6rem;
    font-weight: 700;
    line-height: 1.5;
}

.auth-panel {
    flex: 1 1 45%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.auth-card {
    width: 100%;
    max-width: 420px;
    padding: 32px;
    background: var(--app-surface);
    border-radius: 12px;
    box-shadow: 0 10px 30px var(--app-auth-shadow);
}

.auth-heading {
    font-size: 1.4rem;
    font-weight: 700;
    margin: 0 0 20px;
}

.auth-card .form-label {
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 6px;
}

.auth-card .form-control {
    background: var(--app-surface);
    min-height: 44px;
    border-radius: 8px;
}

.auth-card .btn-primary {
    --bs-btn-bg: var(--app-auth-navy);
    --bs-btn-border-color: var(--app-auth-navy);
    --bs-btn-hover-bg: var(--app-auth-steel);
    --bs-btn-hover-border-color: var(--app-auth-steel);
    --bs-btn-active-bg: var(--app-auth-steel);
    --bs-btn-active-border-color: var(--app-auth-steel);
    min-height: 44px;
    border-radius: 8px;
    font-weight: 600;
}

/* حقل كلمة المرور اتجاهه ltr، فبدايته (اليسار) هي جهة زر العين في الصفحة العربية */
.password-wrap {
    position: relative;
}

.password-wrap .form-control {
    padding-inline-start: 44px;
}

.toggle-password {
    position: absolute;
    inset-inline-end: 6px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: var(--app-muted);
    font-size: 1.1rem;
    line-height: 1;
}

.toggle-password:hover {
    color: var(--app-auth-navy);
}

.toggle-password:focus-visible {
    outline: 2px solid var(--app-auth-steel);
    outline-offset: 2px;
}

.auth-footer {
    margin: 20px 0 0;
    text-align: center;
    font-size: 0.75rem;
    color: var(--app-muted);
}

/* الجوال واللوحي: عمود واحد، ولوحة الشعار تصير رأسًا مختصرًا */
@media (max-width: 991.98px) {
    .auth-shell {
        flex-direction: column;
    }

    .auth-brand {
        flex: 0 0 auto;
        max-width: 100%;
        padding: 28px 20px;
    }

    .auth-brand-logo {
        max-height: 64px;
        margin-bottom: 12px;
    }

    .auth-brand-name {
        font-size: 1.25rem;
    }

    .auth-panel {
        flex: 1 1 auto;
        align-items: flex-start;
        padding: 24px 16px 40px;
    }

    .auth-card {
        padding: 24px;
    }
}

/* ===== الجداول ===== */
.table-app {
    --bs-table-bg: var(--app-surface);
    margin-bottom: 0;
}

/* تمييز الأشهر بالتناوب في الكشوفات: شهر فاتح وشهر أغمق قليلًا */
.table-app .row-month-alt {
    --bs-table-bg: var(--app-month-alt);
}

/* الخدمات: الفاتورة أحمر، المدفوع أخضر */
.util-billed,
.util-billed::placeholder {
    color: var(--app-danger);
}
.util-paid,
.util-paid::placeholder {
    color: var(--app-success);
}
.util-billed::placeholder,
.util-paid::placeholder {
    opacity: 0.45;
}

/* السطر المفتوح من نتائج البحث (رابط ينتهي بـ #tx-رقم) */
.table-app tr:target {
    --bs-table-bg: var(--app-primary-soft);
    outline: 2px solid var(--app-primary);
    outline-offset: -2px;
    scroll-margin-top: calc(var(--app-topbar-height) + 16px);
}

/* تمييز السطور بالتناوب (الفهرس): سطر فاتح وسطر أغمق، مع بقاء سطور العناوين والمجاميع والتنبيه على لونها */
.table-app.table-zebra > tbody > tr:nth-child(even):not(.table-light):not(.table-danger) {
    --bs-table-bg: var(--app-month-alt);
}

/* خط فاصل بين الأعمدة (الفهرس) لكثرة الأرقام */
.table-app.table-zebra > :not(caption) > tr > :not(:last-child) {
    border-inline-end: 1px solid var(--app-border);
}

.table-app th {
    color: var(--app-muted);
    font-weight: 500;
    font-size: 0.85rem;
    white-space: nowrap;
}

.badge-soft-success {
    background: color-mix(in srgb, var(--app-success) 12%, transparent);
    color: var(--app-success);
}

.badge-soft-danger {
    background: color-mix(in srgb, var(--app-danger) 12%, transparent);
    color: var(--app-danger);
}

.badge-soft-primary {
    background: var(--app-primary-soft);
    color: var(--app-primary);
}

.audit-values {
    font-size: 0.8rem;
    color: var(--app-muted);
    margin: 0;
    padding: 0;
    list-style: none;
}

.sql-preview {
    direction: ltr;
    text-align: start;
    background: var(--app-bg);
    border: 1px solid var(--app-border);
    border-radius: 8px;
    padding: 0.75rem;
    font-size: 0.8rem;
    white-space: pre-wrap;
    margin: 0.5rem 0 0;
}

/* صورة الفاتورة: تنبيه أثناء التصغير والرفع، والصورة في النافذة */
.receipt-busy {
    position: fixed;
    bottom: 1rem;
    inset-inline-start: 50%;
    transform: translateX(50%);
    z-index: 1080;
    background: var(--app-sidebar-bg);
    color: var(--app-sidebar-active-text);
    border-radius: var(--app-radius);
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
}

.receipt-full {
    max-height: 75vh;
}

.receipt-thumb {
    max-width: 160px;
    max-height: 160px;
    border: 1px solid var(--app-border);
    border-radius: 8px;
}

/* تمييز عمود الرصيد المتوفر: خلفية أزرق فاتح + border يساري + نص أخضر */
.highlight-balance {
    background-color: color-mix(in srgb, var(--app-primary) 8%, transparent);
    border-inline-start: 3px solid var(--app-primary);
    padding-inline-start: calc(var(--bs-table-padding-x) - 2px);
    color: var(--app-success) !important;
    font-weight: 600;
}

/* ألوان أعمدة جدول الحسابات */
.col-income,
.col-transfer-in {
    color: var(--app-success) !important;
}

.col-expense,
.col-transfer-out {
    color: var(--app-danger) !important;
}

/* مجموع المصاريف (أحمر بدل أخضر) */
.expense-total {
    color: var(--app-danger) !important;
    font-weight: 600;
}

/* الحاسبة السريعة: عمود خلايا يطفو أعلى الصفحة */
.quick-calc-toggle {
    cursor: pointer;
}

.quick-calc {
    margin: 0.25rem 0.5rem 0.75rem;
    background: var(--app-sidebar-hover-bg);
    border-radius: var(--app-radius);
    color: var(--app-sidebar-active-text);
}

.quick-calc-cell {
    border-radius: 0;
    margin-top: -1px;
    text-align: end;
}

.quick-calc-total {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 0.4rem;
    padding-top: 0.4rem;
    border-top: 2px solid var(--app-sidebar-text);
    font-weight: 700;
}

/* استيراد كشف البنك */
.bi-paste {
    font-family: monospace;
    font-size: 0.8rem;
    direction: ltr;
}

.bi-table td {
    vertical-align: top;
}

.bi-orig {
    font-size: 0.72rem;
    color: var(--app-muted);
    direction: ltr;
    text-align: start;
    word-break: break-all;
}

.bi-table tr.bi-done > td:not(.bi-undo) {
    opacity: 0.45;
}
