/* ============================================================
   System Theme Layer — مشترك لكل الأنظمة
   Path: /core/assets/css/theme.css
   القيم تأتي من ThemeManager (متغيرات CSS) — هذا الملف ثابت.
   كل الخطوط محلية (Offline).
============================================================ */

/* ---------- الخطوط المحلية ---------- */
@font-face {
    font-family: 'Tajawal';
    src: url('../fonts/cairo/Tajawal-Bold.ttf') format('truetype');
    font-weight: 400 900;
    font-display: swap;
}
@font-face {
    font-family: 'Lateef';
    src: url('../fonts/cairo/LateefRegOT.ttf') format('truetype');
    font-weight: 400 900;
    font-display: swap;
}
@font-face {
    font-family: 'Cairo';
    src: url('../fonts/cairo/Cairo-Bold.woff2') format('woff2'),
         url('../fonts/cairo/Cairo-Bold.woff') format('woff');
    font-weight: 400 900;
    font-display: swap;
}

html[data-font="cairo"]   { --th-font: 'Cairo', 'latif', sans-serif; }
html[data-font="tajawal"] { --th-font: 'Tajawal', 'Cairo', sans-serif; }
html[data-font="lateef"]  { --th-font: 'Lateef', 'latif', serif; }
html[data-font="system"]  { --th-font: system-ui, -apple-system, "Segoe UI", Tahoma, Arial, sans-serif; }

html[data-font]:not([data-font="default"]) body,
html[data-font]:not([data-font="default"]) body *:not(i):not([class*="fa-"]):not([class^="fa"]):not(code):not(pre):not(kbd) {
    font-family: var(--th-font) !important;
}

/* ---------- العناوين ---------- */
html[data-theme] :is(h1, h2, h3, h4, h5, h6, .page-title, .settings-title, .modal-title) {
    font-weight: var(--th-heading-weight);
}

/* ---------- لون التحديد والتركيز ---------- */
html[data-theme] ::selection {
    background: rgba(var(--th-primary-rgb), .25);
}
html[data-theme] :is(.form-control, .form-select, input, select, textarea):focus {
    border-color: var(--th-primary) !important;
    box-shadow: 0 0 0 .2rem rgba(var(--th-primary-rgb), .15) !important;
    outline: none;
}

/* ---------- الأزرار الأساسية ---------- */
html[data-theme] .btn-primary {
    --bs-btn-bg: var(--th-primary);
    --bs-btn-border-color: var(--th-primary);
    --bs-btn-hover-bg: var(--th-primary);
    --bs-btn-hover-border-color: var(--th-primary);
    --bs-btn-active-bg: var(--th-primary);
    --bs-btn-color: var(--th-on-primary);
    --bs-btn-hover-color: var(--th-on-primary);
}
html[data-theme] .btn-primary:hover { filter: brightness(.94); }
html[data-theme] .btn-outline-primary {
    --bs-btn-color: var(--th-primary);
    --bs-btn-border-color: var(--th-primary);
    --bs-btn-hover-bg: var(--th-primary);
    --bs-btn-hover-border-color: var(--th-primary);
    --bs-btn-hover-color: var(--th-on-primary);
    --bs-btn-active-bg: var(--th-primary);
    --bs-btn-active-border-color: var(--th-primary);
    --bs-btn-active-color: var(--th-on-primary);
}
html[data-theme] :is(.nav-pills .nav-link.active, .page-item.active .page-link) {
    background-color: var(--th-primary);
    border-color: var(--th-primary);
    color: var(--th-on-primary);
}
html[data-theme] .page-link { color: var(--th-primary); }
html[data-theme] .text-primary { color: var(--th-primary) !important; }
html[data-theme] .bg-primary { background-color: var(--th-primary) !important; }
html[data-theme] .border-primary { border-color: var(--th-primary) !important; }
html[data-theme] a:not([class]) { color: var(--th-primary); }
html[data-theme] input[type="checkbox"], html[data-theme] input[type="radio"], html[data-theme] progress {
    accent-color: var(--th-primary);
}
html[data-theme] .form-check-input:checked {
    background-color: var(--th-primary);
    border-color: var(--th-primary);
}

/* ---------- خلفية الصفحة ---------- */
html[data-bg="solid"] body {
    background: var(--th-page) !important;
}
html[data-bg="gradient"] body {
    background:
        radial-gradient(circle at top right, rgba(var(--th-primary-rgb), .10), transparent 28%),
        radial-gradient(circle at bottom left, color-mix(in srgb, var(--th-primary-2) 10%, transparent), transparent 30%),
        var(--th-page) !important;
    background-attachment: fixed !important;
}

/* ---------- الشريط العلوي ---------- */
html[data-topbar="primary"] :is(.topbar, [class$="-topbar"], [class*="-topbar "]) {
    background: linear-gradient(135deg, var(--th-primary), var(--th-primary-2)) !important;
    border-color: transparent !important;
    color: var(--th-on-primary) !important;
}
html[data-topbar="primary"] :is(.topbar, [class$="-topbar"], [class*="-topbar "]) :is(h1,h2,h3,h4,h5,h6,span,div,small,i,a,p,strong) {
    color: inherit !important;
}
html[data-topbar="primary"] :is(.topbar, [class$="-topbar"], [class*="-topbar "]) :is(button, .btn) {
    background: rgba(255,255,255,.18) !important;
    color: var(--th-on-primary) !important;
    border-color: rgba(255,255,255,.25) !important;
}
html[data-topbar="flat"] :is(.topbar, [class$="-topbar"], [class*="-topbar "]) {
    background: var(--th-card) !important;
    box-shadow: none !important;
    border: 1px solid var(--th-border) !important;
    backdrop-filter: none !important;
}

/* ---------- الشريط الجانبي ---------- */
html[data-theme] .sidebar {
    background: linear-gradient(180deg, var(--sidebar-bg), var(--sidebar-bg-soft)) !important;
    color: var(--sidebar-text);
}
html[data-theme] .sidebar :is(.active, .is-active, [aria-current="page"]) > a,
html[data-theme] .sidebar a.active,
html[data-theme] .sidebar a.is-active {
    background: var(--th-sidebar-active) !important;
    color: var(--th-sidebar-active-text) !important;
}

/* ============================================================
   الوضع الداكن / المظلم
============================================================ */
html:is([data-theme="dim"], [data-theme="dark"]) {
    color-scheme: dark;
    scrollbar-color: var(--th-border) var(--th-page);
}
html:is([data-theme="dim"], [data-theme="dark"]) body {
    background: var(--th-page) !important;
    color: var(--th-text) !important;
}
html:is([data-theme="dim"], [data-theme="dark"])[data-bg="gradient"] body {
    background:
        radial-gradient(circle at top right, rgba(var(--th-primary-rgb), .14), transparent 30%),
        var(--th-page) !important;
}

/* إخفاء لحظي للمحتوى حتى يكتمل تكييف الألوان (مع fallback تلقائي) */
html:is([data-theme="dim"], [data-theme="dark"]):not(.th-adapted) :is(.page-content, .home-section, main) {
    opacity: 0;
    animation: thReveal .01s linear 1.2s forwards;
}
html.th-adapted :is(.page-content, .home-section, main) {
    opacity: 1;
    transition: opacity .12s ease;
}
@keyframes thReveal { to { opacity: 1; } }

html:is([data-theme="dim"], [data-theme="dark"]) :is(
    .card, .modal-content, .dropdown-menu, .list-group-item, .popover, .toast, .offcanvas,
    .bg-white, .bg-light, .accordion-item, .accordion-button, .nav-tabs .nav-link.active, .page-link
) {
    background-color: var(--th-card) !important;
    color: var(--th-text) !important;
    border-color: var(--th-border) !important;
}
html:is([data-theme="dim"], [data-theme="dark"]) :is(.modal-header, .modal-footer, .card-header, .card-footer) {
    border-color: var(--th-border) !important;
}
html:is([data-theme="dim"], [data-theme="dark"]) .dropdown-item { color: var(--th-text); }
html:is([data-theme="dim"], [data-theme="dark"]) .dropdown-item:hover { background: var(--th-hover); color: var(--th-text); }

html:is([data-theme="dim"], [data-theme="dark"]) :is(.text-dark, .text-black, .text-body) { color: var(--th-text) !important; }
html:is([data-theme="dim"], [data-theme="dark"]) :is(.text-muted, .text-secondary, small.text-muted) { color: var(--th-muted) !important; }
html:is([data-theme="dim"], [data-theme="dark"]) :is(.border, .border-top, .border-bottom, .border-start, .border-end, hr) { border-color: var(--th-border) !important; }
html:is([data-theme="dim"], [data-theme="dark"]) hr { opacity: .6; }

/* النماذج */
html:is([data-theme="dim"], [data-theme="dark"]) :is(
    .form-control, .form-select, input[type="text"], input[type="number"], input[type="search"], input[type="email"],
    input[type="password"], input[type="tel"], input[type="date"], input[type="datetime-local"], input[type="month"],
    input[type="time"], input[type="url"], input:not([type]), select, textarea
) {
    background-color: var(--th-input) !important;
    color: var(--th-text) !important;
    border-color: var(--th-border) !important;
}
html:is([data-theme="dim"], [data-theme="dark"]) :is(.form-control, .form-select, input, textarea):disabled,
html:is([data-theme="dim"], [data-theme="dark"]) .form-control[readonly] {
    background-color: var(--th-card) !important;
    color: var(--th-muted) !important;
}
html:is([data-theme="dim"], [data-theme="dark"]) ::placeholder { color: var(--th-muted) !important; opacity: .75; }
html:is([data-theme="dim"], [data-theme="dark"]) .input-group-text {
    background: var(--th-card); color: var(--th-muted); border-color: var(--th-border);
}
html:is([data-theme="dim"], [data-theme="dark"]) .form-select {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23aab4c4' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m2 5 6 6 6-6'/%3e%3c/svg%3e") !important;
}

/* الجداول */
html:is([data-theme="dim"], [data-theme="dark"]) .table {
    --bs-table-color: var(--th-text);
    --bs-table-bg: transparent;
    --bs-table-border-color: var(--th-border);
    --bs-table-striped-bg: var(--th-hover);
    --bs-table-striped-color: var(--th-text);
    --bs-table-hover-bg: rgba(var(--th-primary-rgb), .08);
    --bs-table-hover-color: var(--th-text);
    --bs-table-active-bg: rgba(var(--th-primary-rgb), .12);
    color: var(--th-text);
    border-color: var(--th-border);
}
html:is([data-theme="dim"], [data-theme="dark"]) :is(.table-light, .table-light > th, .table-light > td, thead.table-light th) {
    --bs-table-bg: var(--th-card);
    --bs-table-color: var(--th-text);
    background-color: var(--th-card) !important;
    color: var(--th-text) !important;
}

/* الأزرار الفاتحة */
html:is([data-theme="dim"], [data-theme="dark"]) :is(.btn-light, .btn-outline-light, .btn-white) {
    background: var(--th-card) !important;
    color: var(--th-text) !important;
    border-color: var(--th-border) !important;
}
html:is([data-theme="dim"], [data-theme="dark"]) .btn-outline-secondary { color: var(--th-muted); border-color: var(--th-border); }
html:is([data-theme="dim"], [data-theme="dark"]) .btn-close:not(.btn-close-white) { filter: invert(1) grayscale(100%) brightness(200%); }

/* Select2 */
html:is([data-theme="dim"], [data-theme="dark"]) :is(.select2-container--default .select2-selection--single, .select2-container--default .select2-selection--multiple, .select2-dropdown, .select2-search__field) {
    background-color: var(--th-input) !important;
    border-color: var(--th-border) !important;
    color: var(--th-text) !important;
}
html:is([data-theme="dim"], [data-theme="dark"]) .select2-container--default .select2-selection--single .select2-selection__rendered { color: var(--th-text) !important; }
html:is([data-theme="dim"], [data-theme="dark"]) .select2-results__option { color: var(--th-text); }
html:is([data-theme="dim"], [data-theme="dark"]) .select2-container--default .select2-results__option--highlighted[aria-selected],
html:is([data-theme="dim"], [data-theme="dark"]) .select2-container--default .select2-results__option--highlighted.select2-results__option--selectable {
    background: var(--th-primary) !important; color: var(--th-on-primary) !important;
}
html:is([data-theme="dim"], [data-theme="dark"]) .select2-container--default .select2-results__option--selected,
html:is([data-theme="dim"], [data-theme="dark"]) .select2-container--default .select2-results__option[aria-selected="true"] {
    background: var(--th-hover) !important;
}
html:is([data-theme="dim"], [data-theme="dark"]) .select2-container--default .select2-selection--multiple .select2-selection__choice {
    background: var(--th-card) !important; border-color: var(--th-border) !important; color: var(--th-text) !important;
}

/* Flatpickr */
html:is([data-theme="dim"], [data-theme="dark"]) .flatpickr-calendar {
    background: var(--th-card) !important; box-shadow: var(--th-shadow-md) !important; border: 1px solid var(--th-border);
}
html:is([data-theme="dim"], [data-theme="dark"]) .flatpickr-calendar :is(.flatpickr-day, .flatpickr-weekday, .flatpickr-month, .flatpickr-current-month, .cur-month, .numInput, .flatpickr-monthDropdown-months, span.flatpickr-weekday) {
    color: var(--th-text) !important; fill: var(--th-text) !important; background: transparent;
}
html:is([data-theme="dim"], [data-theme="dark"]) .flatpickr-day:hover { background: var(--th-hover) !important; }
html:is([data-theme="dim"], [data-theme="dark"]) .flatpickr-day.selected { background: var(--th-primary) !important; border-color: var(--th-primary) !important; color: #fff !important; }
html:is([data-theme="dim"], [data-theme="dark"]) .flatpickr-day:is(.prevMonthDay, .nextMonthDay, .flatpickr-disabled) { color: var(--th-muted) !important; opacity: .5; }

/* متفرقات */
html:is([data-theme="dim"], [data-theme="dark"]) :is(code, kbd, pre) { background: var(--th-input); color: var(--th-text); }
html:is([data-theme="dim"], [data-theme="dark"]) img[src$=".svg"].invert-on-dark { filter: invert(1); }
html:is([data-theme="dim"], [data-theme="dark"]) ::-webkit-scrollbar { width: 10px; height: 10px; }
html:is([data-theme="dim"], [data-theme="dark"]) ::-webkit-scrollbar-track { background: var(--th-page); }
html:is([data-theme="dim"], [data-theme="dark"]) ::-webkit-scrollbar-thumb { background: var(--th-border); border-radius: 10px; border: 2px solid var(--th-page); }

/* الطباعة دائماً فاتحة */
@media print {
    html[data-theme] body { background: #fff !important; color: #000 !important; }
    html[data-theme] { zoom: 1 !important; }
}

/* ---------- زر القائمة (موحد بكل الصفحات وكل الأنظمة) ---------- */
html[data-theme] #toggleSidebar {
    width: 42px;
    height: 42px;
    min-width: 42px;
    flex: 0 0 42px;
    padding: 0;
    border: none;
    border-radius: 12px;
    background: linear-gradient(135deg, var(--th-primary), var(--th-primary-2));
    color: var(--th-on-primary, #fff);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 17px;
    line-height: 1;
    box-shadow: 0 10px 20px rgba(var(--th-primary-rgb), .18);
    cursor: pointer;
}
html[data-theme] #toggleSidebar i { color: inherit; font-size: inherit; }
html[data-theme] #toggleSidebar:hover { filter: brightness(1.06); }
@media (max-width: 576px) {
    html[data-theme] #toggleSidebar { width: 38px; height: 38px; min-width: 38px; flex-basis: 38px; font-size: 15px; border-radius: 11px; }
}

/* ============================================================
   هيدر المودلات (موحد بكل الصفحات وكل الأنظمة)
   data-modal = primary | sidebar | soft
============================================================ */
html[data-modal] :is(.modal .modal-header, dialog .dialog-head) {
    border-bottom: 0 !important;
}
html[data-modal="primary"] :is(.modal .modal-header, dialog .dialog-head) {
    background: linear-gradient(135deg, var(--th-primary), var(--th-primary-2)) !important;
    color: var(--th-on-primary) !important;
}
html[data-modal="sidebar"] :is(.modal .modal-header, dialog .dialog-head) {
    background: linear-gradient(135deg, var(--sidebar-bg-1), var(--sidebar-bg-2)) !important;
    color: var(--sidebar-text) !important;
}
html[data-modal="soft"] :is(.modal .modal-header, dialog .dialog-head) {
    background: var(--th-primary-soft) !important;
    color: var(--th-text) !important;
    border-bottom: 2px solid var(--th-primary) !important;
}

/* العنوان والنصوص داخل الهيدر تاخذ لون الهيدر */
html[data-modal] :is(.modal .modal-header, dialog .dialog-head) :is(.modal-title, h1, h2, h3, h4, h5, h6, p, small, strong, .text-muted, .text-secondary, .text-dark) {
    color: inherit !important;
}
html[data-modal] :is(.modal .modal-header, dialog .dialog-head) :is(small, p, .text-muted, .text-secondary) { opacity: .85; }
html[data-modal] :is(.modal .modal-header, dialog .dialog-head) :is(.modal-title, h1, h2, h3, h4, h5, h6) > i,
html[data-modal] .modal .modal-header > i { color: inherit !important; }
html[data-modal="soft"] .modal .modal-header :is(.modal-title, h1, h2, h3, h4, h5, h6) > i,
html[data-modal="soft"] .modal .modal-header > i { color: var(--th-primary) !important; }

/* زر الإغلاق X بنفس لون نص الهيدر (يشتغل بالفاتح والداكن) */
html[data-modal] .modal .modal-header .btn-close {
    background: currentColor !important;
    -webkit-mask: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath d='M.293.293a1 1 0 0 1 1.414 0L8 6.586 14.293.293a1 1 0 1 1 1.414 1.414L9.414 8l6.293 6.293a1 1 0 0 1-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 0 1-1.414-1.414L6.586 8 .293 1.707a1 1 0 0 1 0-1.414z'/%3e%3c/svg%3e") center / .8em auto no-repeat;
            mask: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath d='M.293.293a1 1 0 0 1 1.414 0L8 6.586 14.293.293a1 1 0 1 1 1.414 1.414L9.414 8l6.293 6.293a1 1 0 0 1-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 0 1-1.414-1.414L6.586 8 .293 1.707a1 1 0 0 1 0-1.414z'/%3e%3c/svg%3e") center / .8em auto no-repeat;
    color: inherit !important;
    filter: none !important;
    opacity: .85 !important;
    border: 0 !important;
    box-shadow: none !important;
}
html[data-modal] .modal .modal-header .btn-close:hover { opacity: 1 !important; }

/* أزرار عادية داخل الهيدر (مثل زر إغلاق نصي) */
html[data-modal]:not([data-modal="soft"]) dialog .dialog-head .net-btn {
    background: rgba(255, 255, 255, .18) !important;
    color: inherit !important;
}
