:root {
    --bg: #f5f7fb;
    --card: #ffffff;
    --text: #172033;
    --muted: #64748b;
    --line: #e2e8f0;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --soft: #eff6ff;
    --accent: #d6a84b;
    --danger: #c93f3f;
    --success: #16825f;
    --warning: #d9901e;
    --info: #0ea5e9;
    --radius: 13px;
    --shadow: 0 8px 24px rgba(15, 23, 42, .07);
    --side-width: 278px;
    --side-mini: 74px;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body {
    min-height: 100%;
    margin: 0;
    overflow-x: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: Tahoma, "Segoe UI", Arial, sans-serif;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font-family: inherit; }
img { max-width: 100%; }

/* ===== Main ERP layout: scoped to avoid conflicts with page-specific CSS ===== */
.app-shell {
    display: flex;
    align-items: flex-start;
    min-height: 100vh;
    width: 100%;
    background: var(--bg);
}

.app-shell > .sidebar {
    flex: 0 0 var(--side-width);
    width: var(--side-width);
    height: 100vh;
    position: sticky;
    top: 0;
    overflow-y: auto;
    overflow-x: hidden;
    background: #fff;
    color: #334155;
    border-inline-end: 1px solid #e5e7eb;
    box-shadow: 0 0 18px rgba(15, 23, 42, .05);
    transition: width .22s ease, flex-basis .22s ease, transform .22s ease;
    z-index: 1040;
    scrollbar-width: thin;
    scrollbar-color: #cbd5e1 transparent;
}
.app-shell > .sidebar::-webkit-scrollbar { width: 7px; }
.app-shell > .sidebar::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 999px; }

.sidebar .brand {
    display: flex;
    align-items: center;
    gap: 11px;
    min-height: 69px;
    padding: 12px 14px;
    border-bottom: 1px solid #e5e7eb;
    background: #fff;
}
.sidebar .brand-badge {
    width: 40px;
    height: 40px;
    min-width: 40px;
    border-radius: 13px;
    display: grid;
    place-items: center;
    background: linear-gradient(135deg, var(--primary), #38bdf8);
    color: #fff;
    font-size: 18px;
    font-weight: 900;
    box-shadow: 0 7px 16px rgba(37, 99, 235, .18);
}
.sidebar .brand strong {
    display: block;
    max-width: 195px;
    overflow: hidden;
    color: #0f172a;
    font-size: 14px;
    line-height: 1.4;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.sidebar .brand small { display: block; color: var(--muted); font-size: 11px; }

.sidebar .side-tools {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 38px;
    gap: 8px;
    padding: 10px 12px;
    border-bottom: 1px solid #e5e7eb;
    background: #fff;
}
.sidebar .side-search {
    width: 100%;
    height: 38px;
    min-width: 0;
    padding: 0 12px;
    border: 1px solid #e2e8f0;
    border-radius: 11px;
    outline: 0;
    background: #f8fafc;
    color: #0f172a;
    font-size: 13px;
}
.sidebar .side-search:focus {
    border-color: #93c5fd;
    background: #fff;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .08);
}
.sidebar .side-search::placeholder { color: #94a3b8; }
.sidebar .mini-btn {
    width: 38px;
    height: 38px;
    display: grid;
    place-items: center;
    border: 1px solid #e2e8f0;
    border-radius: 11px;
    background: #f8fafc;
    color: #475569;
}
.sidebar .mini-btn:hover { background: var(--soft); color: var(--primary); }

.sidebar nav { padding: 10px; }
.sidebar .menu-section {
    margin: 12px 8px 7px;
    color: #94a3b8;
    font-size: 11px;
    font-weight: 800;
}
.sidebar nav > a,
.sidebar .menu-head-link {
    position: relative;
    min-height: 42px;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 11px;
    border-radius: 11px;
    color: #334155;
    font-size: 14px;
    transition: background-color .16s ease, color .16s ease;
}
.sidebar nav > a:hover,
.sidebar .menu-head-link:hover { background: #f1f5f9; color: #0f172a; }
.sidebar nav > a.active,
.sidebar .menu-head-link.active,
.sidebar .accordion-item.active-parent .menu-head-link {
    background: var(--soft);
    color: var(--primary);
    font-weight: 800;
}
.sidebar nav > a.active::before,
.sidebar .menu-head-link.active::before,
.sidebar .accordion-item.active-parent .menu-head-link::before {
    content: "";
    position: absolute;
    inset-inline-start: -10px;
    top: 9px;
    bottom: 9px;
    width: 4px;
    border-radius: 999px;
    background: var(--primary);
}
.sidebar nav a i,
.sidebar .menu-head-link i {
    min-width: 22px;
    text-align: center;
    font-size: 18px;
}
.sidebar .quick-sale { background: #f0fdf4; color: #15803d !important; }
.sidebar .quick-sale.active { background: #dcfce7 !important; color: #166534 !important; font-weight: 800; }

.sidebar .accordion-item { margin-bottom: 5px; border: 0; background: transparent; }
.sidebar .menu-head { display: flex; align-items: stretch; gap: 6px; border-radius: 11px; }
.sidebar .menu-head-link { flex: 1 1 auto; min-width: 0; }
.sidebar .menu-head-link span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar .menu-toggle {
    flex: 0 0 38px;
    width: 38px;
    border: 1px solid #e2e8f0;
    border-radius: 11px;
    background: #f8fafc;
    color: #64748b;
    transition: background-color .16s ease, color .16s ease;
}
.sidebar .menu-toggle:hover { background: var(--soft); color: var(--primary); }
.sidebar .menu-toggle i { display: inline-block; transition: transform .2s ease; }
.sidebar .menu-toggle:not(.collapsed) i { transform: rotate(180deg); }
.sidebar .submenu {
    margin-inline-start: 18px;
    padding: 6px 10px 7px 0;
    border-inline-start: 1px dashed #cbd5e1;
}
html[dir="ltr"] .sidebar .submenu { padding: 6px 0 7px 10px; }
.sidebar .submenu a {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 2px;
    padding: 8px 10px;
    border-radius: 9px;
    color: #475569;
    font-size: 13px;
    transition: background-color .15s ease, color .15s ease;
}
.sidebar .submenu a:hover { background: #f1f5f9; color: #0f172a; }
.sidebar .submenu a.active { background: var(--soft); color: var(--primary); font-weight: 800; }
.sidebar .tiny {
    flex: 0 0 auto;
    padding: 3px 7px;
    border-radius: 999px;
    background: #e0f2fe;
    color: #0369a1;
    font-size: 10px;
}

.app-shell > .main {
    flex: 1 1 auto;
    width: calc(100% - var(--side-width));
    min-width: 0;
    min-height: 100vh;
    margin: 0;
    padding: 0;
    transition: width .22s ease;
}
.app-shell > .main > .topbar {
    min-height: 64px;
    position: sticky;
    top: 0;
    z-index: 1020;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 16px;
    border-bottom: 1px solid #e5e7eb;
    background: rgba(255, 255, 255, .96);
    box-shadow: 0 2px 12px rgba(15, 23, 42, .035);
    backdrop-filter: blur(10px);
}
.app-shell > .main > .page-content {
    width: 100%;
    min-width: 0;
    padding: 18px;
}
.page-title h4 { margin: 0; color: #0f172a; font-size: 18px; font-weight: 900; }
.page-title small { color: var(--muted); font-size: 12px; }
.top-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.top-chip {
    min-height: 34px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    border: 1px solid #e2e8f0;
    border-radius: 999px;
    background: #f8fafc;
    color: #334155;
    font-size: 13px;
}
.top-chip:hover { background: var(--soft); color: var(--primary); }
.sidebar-toggle-btn {
    width: 42px;
    height: 42px;
    display: inline-grid;
    place-items: center;
    flex: 0 0 42px;
    border: 0;
    border-radius: 12px;
    background: var(--soft);
    color: var(--primary);
    font-size: 22px;
    box-shadow: 0 4px 12px rgba(37, 99, 235, .10);
    transition: background-color .18s ease, color .18s ease;
}
.sidebar-toggle-btn:hover { background: var(--primary); color: #fff; }
.sidebar-toggle-btn i { transition: transform .2s ease; }

body.sidebar-collapsed .app-shell > .sidebar { flex-basis: var(--side-mini); width: var(--side-mini); }
body.sidebar-collapsed .app-shell > .main { width: calc(100% - var(--side-mini)); }
body.sidebar-collapsed .sidebar .brand > div:not(.brand-badge),
body.sidebar-collapsed .sidebar .side-search,
body.sidebar-collapsed .sidebar .menu-section,
body.sidebar-collapsed .sidebar span,
body.sidebar-collapsed .sidebar small,
body.sidebar-collapsed .sidebar .submenu,
body.sidebar-collapsed .sidebar .menu-toggle { display: none !important; }
body.sidebar-collapsed .sidebar .side-tools { display: flex; justify-content: center; }
body.sidebar-collapsed .sidebar .brand { justify-content: center; padding-inline: 8px; }
body.sidebar-collapsed .sidebar nav > a,
body.sidebar-collapsed .sidebar .menu-head-link { justify-content: center; padding: 11px; }
body.sidebar-collapsed .sidebar .accordion-collapse { display: none !important; }
body.sidebar-collapsed .sidebar-toggle-btn i { transform: rotate(180deg); }

.mobile-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1035;
    background: rgba(15, 23, 42, .48);
    opacity: 0;
    transition: opacity .2s ease;
}
.mobile-backdrop.show { opacity: 1; }

/* ===== Shared ERP components retained for existing pages ===== */
.btn, .form-control, .form-select, .modal-content { border-radius: 10px; }
.btn-primary { border: 0; background: linear-gradient(135deg, var(--primary), var(--primary-dark)); }
.btn-outline-primary { border-color: var(--primary); color: var(--primary); }
.btn-outline-primary:hover { border-color: var(--primary); background: var(--primary); color: #fff; }
.card, .cardx, .panel, .table-box {
    border: 1px solid var(--line) !important;
    border-radius: var(--radius) !important;
    background: var(--card);
    box-shadow: var(--shadow) !important;
}
.table-responsive { max-width: 100%; }
.table thead th { border-bottom: 1px solid var(--line); background: #f8fafc; color: #60706a; font-size: 12px; font-weight: 900; }
.table td { vertical-align: middle; }
.badge-soft { border: 1px solid #dbeafe; background: var(--soft); color: var(--primary); }
.stat { padding: 18px; }
.stat .icon { width: 48px; height: 48px; display: grid; place-items: center; border-radius: 14px; background: var(--soft); color: var(--primary); font-size: 22px; }
.stat h3 { margin: 8px 0 0; font-weight: 900; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.product-card { height: 100%; position: relative; overflow: hidden; cursor: pointer; border: 1px solid #e2e8f0; border-radius: 14px; background: #fff; transition: transform .18s ease, border-color .18s ease, box-shadow .18s ease; }
.product-card:hover { transform: translateY(-3px); border-color: var(--primary); box-shadow: 0 15px 28px rgba(37, 99, 235, .11); }
.product-card img { width: 100%; height: 125px; object-fit: cover; background: #edf2f7; }
.product-card .pbody { padding: 12px; }
.product-card .fav { position: absolute; top: 9px; left: 9px; padding: 4px 7px; border-radius: 999px; background: rgba(255,255,255,.92); color: var(--accent); }
.cart-panel { position: sticky; top: 82px; }
.cart-row { display: grid; grid-template-columns: 1fr 70px 70px 34px; gap: 6px; align-items: center; padding: 8px 0; border-bottom: 1px dashed var(--line); }
.cart-row input { padding: 5px !important; text-align: center; }
.money-big { color: var(--primary); font-size: 23px; font-weight: 900; }
.nav-tabs .nav-link { border-radius: 10px 10px 0 0; color: #56635e; font-weight: 800; }
.nav-tabs .nav-link.active { color: var(--primary); }
.settings-card { min-height: 100%; }
.permission-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.permission-box { padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: #fff; }
.mobile-menu { display: none; }
.toast-msg { position: fixed; bottom: 18px; left: 18px; z-index: 3000; padding: 11px 15px; border-radius: 10px; background: #17211d; color: #fff; box-shadow: var(--shadow); }

@media (max-width: 991.98px) {
    body.sidebar-collapsed .app-shell > .main,
    .app-shell > .main { width: 100%; }
    .app-shell > .sidebar {
        position: fixed;
        inset-block: 0;
        inset-inline-start: 0;
        width: min(86vw, 310px);
        max-width: 310px;
        height: 100dvh;
        transform: translateX(105%);
        box-shadow: 0 0 28px rgba(15, 23, 42, .22);
    }
    html[dir="ltr"] .app-shell > .sidebar { transform: translateX(-105%); }
    .app-shell > .sidebar.open { transform: translateX(0); }
    .mobile-backdrop { display: block; pointer-events: none; }
    .mobile-backdrop.show { pointer-events: auto; }
    .app-shell > .main > .topbar { padding: 8px 12px; }
    .app-shell > .main > .page-content { padding: 12px; }
    .mobile-menu { display: inline-flex; }
    .page-title small, .top-chip .hide-sm { display: none; }
    .cart-panel { position: static; }
}

@media (max-width: 580px) {
    .app-shell > .main > .topbar { min-height: 60px; }
    .top-actions { gap: 5px; }
    .top-chip { padding: 6px 8px; }
    .product-card img { height: 100px; }
    .cart-row { grid-template-columns: 1fr 55px 58px 28px; }
    .sidebar .brand strong { max-width: 165px; }
}

@media print {
    .app-shell > .sidebar,
    .app-shell > .main > .topbar,
    .mobile-backdrop { display: none !important; }
    .app-shell > .main { width: 100% !important; }
    .app-shell > .main > .page-content { padding: 0 !important; }
}

/* Minimal fallbacks keep the shell usable if the Bootstrap CDN is temporarily unavailable. */
.app-shell > .main > .topbar > div:first-child { display: flex; align-items: center; gap: .5rem; min-width: 0; }
.sidebar .collapse:not(.show) { display: none; }
.sidebar .collapsing { height: 0; overflow: hidden; transition: height .22s ease; }

/* ===== Responsive hardening for all ERP pages ===== */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}
body.sidebar-mobile-open { overflow: hidden; }
body, .app-shell, .main, .page-content, .page-content > *, .row > * { min-width: 0; }
button, [role="button"], .btn, a { touch-action: manipulation; }
input, select, textarea, button { max-width: 100%; }
textarea { min-height: 90px; }
canvas, svg, video, iframe { max-width: 100%; }

.page-content .container,
.page-content .container-sm,
.page-content .container-md,
.page-content .container-lg,
.page-content .container-xl,
.page-content .container-xxl,
.page-content .container-fluid {
    max-width: 100%;
    padding-inline: 0;
}
.page-content .row { --bs-gutter-x: 1rem; }
.page-content .card-header,
.page-content .card-body,
.page-content .card-footer { min-width: 0; }

.table-responsive {
    width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}
.table-responsive > .table { margin-bottom: 0; }
.table th, .table td { overflow-wrap: anywhere; }
.table img { max-width: 72px; height: auto; }

.erp-auto-table-wrap {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.erp-auto-table-wrap > table { margin-bottom: 0; }

.modal { padding-inline: 0 !important; }
.modal-dialog { max-width: min(var(--bs-modal-width, 500px), calc(100vw - 24px)); }
.modal-content { overflow: hidden; }
.modal-body { overflow-wrap: anywhere; }
.dropdown-menu { max-width: calc(100vw - 24px); }

.topbar > div:first-child,
.page-title,
.top-actions { min-width: 0; }
.page-title h4 {
    max-width: min(42vw, 560px);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.top-actions { justify-content: flex-end; }

/* Common page toolbars become safely wrappable without changing page markup. */
.page-actions,
.card-header.d-flex,
.page-content > .d-flex,
.page-content form.row {
    max-width: 100%;
}

@media (max-width: 991.98px) {
    .app-shell > .sidebar {
        visibility: hidden;
        will-change: transform;
    }
    .app-shell > .sidebar.open { visibility: visible; }
    .mobile-backdrop { visibility: hidden; }
    .mobile-backdrop.show { visibility: visible; }
    .page-title h4 { max-width: 44vw; }
    .topbar-optional { display: none !important; }
    .page-content .sticky-top,
    .page-content [style*="position: sticky"],
    .page-content [style*="position:sticky"] { position: static !important; top: auto !important; }
}

@media (max-width: 767.98px) {
    .app-shell > .main > .topbar {
        min-height: 58px;
        gap: 8px;
        padding: 7px 9px;
    }
    .sidebar-toggle-btn {
        width: 40px;
        height: 40px;
        flex-basis: 40px;
    }
    .page-title h4 {
        max-width: 37vw;
        font-size: 15px;
    }
    .top-actions { flex-wrap: nowrap; }
    .top-chip { min-height: 38px; padding: 6px 9px; }
    .top-chip-new-invoice .hide-sm { display: none; }
    .app-shell > .main > .page-content { padding: 10px; }
    .page-content .row { --bs-gutter-x: .75rem; --bs-gutter-y: .75rem; }
    .card, .cardx, .panel, .table-box { border-radius: 11px !important; }
    .card-body { padding: .9rem; }
    .card-header, .card-footer { padding: .75rem .9rem; }
    .btn { min-height: 40px; }
    .btn-sm { min-height: 34px; }
    .form-control, .form-select { min-height: 42px; }
    input.form-control, select.form-select, textarea.form-control { font-size: 16px; }
    .input-group > .btn { min-height: 42px; }
    .table-responsive > .table,
    .erp-auto-table-wrap > table {
        width: max-content;
        min-width: 100%;
    }
    .table-responsive > .table > :not(caption) > * > *,
    .erp-auto-table-wrap > table > :not(caption) > * > * {
        white-space: nowrap;
    }
    .table .text-wrap,
    .table [data-wrap="true"] { white-space: normal !important; min-width: 180px; }
    .modal-dialog { margin: 12px auto; }
    .modal-dialog-scrollable { height: calc(100dvh - 24px); }
    .modal-body { padding: .9rem; }
    .toast-msg { right: 10px; left: 10px; bottom: 10px; text-align: center; }
}

@media (max-width: 479.98px) {
    .page-title h4 { max-width: 31vw; font-size: 14px; }
    .top-chip-language span { display: none; }
    .top-chip-language,
    .top-chip-new-invoice { width: 38px; justify-content: center; padding-inline: 0; }
    .top-actions .btn { width: 38px; padding-inline: 0; }
    .app-shell > .main > .page-content { padding: 8px; }
    .page-actions > .btn,
    .page-actions > a.btn { flex: 1 1 auto; }
    .cart-row { grid-template-columns: minmax(120px, 1fr) 52px 55px 30px; overflow-x: auto; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        scroll-behavior: auto !important;
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
    }
}
@media (max-width: 991.98px) {
    html,
    body {
        overflow-x: hidden;
    }

    .app-shell {
        width: 100%;
        min-height: 100vh;
        overflow: visible !important;
    }

    #sidebar.sidebar {
        position: fixed !important;
        top: 0 !important;
        bottom: 0 !important;
        width: min(320px, 88vw) !important;
        max-width: 88vw !important;
        height: 100vh !important;
        height: 100dvh !important;
        z-index: 1060 !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;

        visibility: hidden !important;
        pointer-events: none !important;

        transition:
            transform 0.28s ease,
            visibility 0.28s ease,
            box-shadow 0.28s ease !important;
    }

    /* العربية: القائمة تخرج من اليمين */
    html[dir="rtl"] #sidebar.sidebar {
        right: 0 !important;
        left: auto !important;
        transform: translate3d(105%, 0, 0) !important;
    }

    /* الإنجليزية: القائمة تخرج من اليسار */
    html[dir="ltr"] #sidebar.sidebar {
        left: 0 !important;
        right: auto !important;
        transform: translate3d(-105%, 0, 0) !important;
    }

    /* فتح القائمة في اللغتين */
    body.mobile-sidebar-open #sidebar.sidebar {
        transform: translate3d(0, 0, 0) !important;
        visibility: visible !important;
        pointer-events: auto !important;
        box-shadow: 0 0 35px rgba(0, 0, 0, 0.24) !important;
    }

    #mobileBackdrop.mobile-backdrop {
        display: block !important;
        position: fixed !important;
        inset: 0 !important;
        z-index: 1055 !important;

        background: rgba(15, 23, 42, 0.52) !important;

        opacity: 0 !important;
        visibility: hidden !important;
        pointer-events: none !important;

        transition:
            opacity 0.28s ease,
            visibility 0.28s ease !important;
    }

    body.mobile-sidebar-open #mobileBackdrop.mobile-backdrop {
        opacity: 1 !important;
        visibility: visible !important;
        pointer-events: auto !important;
    }

    body.mobile-sidebar-open {
        overflow: hidden !important;
    }

    #sidebarSlideBtn.sidebar-toggle-btn {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        position: relative;
        z-index: 1040;
        cursor: pointer;
        touch-action: manipulation;
    }

    main.main {
        width: 100% !important;
        min-width: 0 !important;
        margin-right: 0 !important;
        margin-left: 0 !important;
    }
}

@media (min-width: 992px) {
    #mobileBackdrop.mobile-backdrop {
        display: none !important;
    }

    #sidebar.sidebar {
        visibility: visible !important;
        pointer-events: auto !important;
        transform: none !important;
    }
}