/* تعزيز RTL مع AdminLTE + Bootstrap RTL */
html[dir="rtl"] body {
  font-family: "Segoe UI", Tahoma, Arial, "Helvetica Neue", sans-serif;
}

/*
  AdminLTE + Bootstrap RTL: فرض وضع الـ layout المعكوس (الشريط على اليمين)
  بدون هذا غالباً يبقى الـ sidebar يساراً رغم dir="rtl"
*/
html[dir="rtl"] .wrapper {
  direction: rtl;
  overflow-x: hidden;
  max-width: 100%;
}

/* منع التمرير الأفقي: w-100 مع margin الشريط كان يسبب عرضاً > 100% */
html[dir="rtl"] body.layout-fixed {
  overflow-x: hidden;
}

html[dir="rtl"] body.layout-fixed .content-wrapper {
  min-width: 0;
  box-sizing: border-box;
}

html[dir="rtl"] body.layout-fixed .main-header.main-header--toolbar {
  width: auto;
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
}

html[dir="rtl"] body.layout-fixed .main-sidebar {
  left: auto !important;
  right: 0 !important;
}

html[dir="rtl"] .main-sidebar {
  left: auto !important;
  right: 0 !important;
  text-align: right;
}

/* عرض الشريط الافتراضي في AdminLTE 3 */
html[dir="rtl"] body.sidebar-mini .main-sidebar,
html[dir="rtl"] .main-sidebar {
  width: 250px;
}

/* الهيدر العلوي: قائمة فقط يمين، خروج أقصى الشمال */
.main-header-start {
  flex-wrap: nowrap;
}

.main-header-logout-wrap .nav-link--logout {
  white-space: nowrap;
}

/* شعار + مستخدم في أعلى الشريط الجانبي (بدل intouch reports) */
html[dir="rtl"] .main-sidebar .brand-link.brand-link--sidebar-brand {
  height: auto !important;
  min-height: 3.2rem;
  padding: 0.65rem 0.5rem !important;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  line-height: 1.25;
}

.sidebar-brand-logo-wrap {
  background: transparent !important;
}

.sidebar-brand-logo-img {
  display: inline-block;
  max-height: 2.35rem;
  width: auto;
  max-width: 100%;
  height: auto;
  object-fit: contain;
  background: transparent !important;
  box-shadow: none !important;
  border: none !important;
  vertical-align: middle;
}

.sidebar-brand-user {
  min-width: 0;
}

.sidebar-user-name {
  display: block;
  color: rgba(255, 255, 255, 0.72) !important;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

html[dir="rtl"] body.sidebar-mini.sidebar-collapse .main-sidebar .brand-link.brand-link--sidebar-brand {
  padding-top: 0.45rem !important;
  padding-bottom: 0.45rem !important;
}

html[dir="rtl"] body.sidebar-mini.sidebar-collapse .sidebar-brand-user {
  display: none !important;
}

html[dir="rtl"] body.sidebar-mini.sidebar-collapse .sidebar-brand-logo-img {
  max-height: 1.65rem;
}

/* صفحة الدخول */
html[dir="rtl"] .login-brand-logo-img {
  max-height: 4rem;
  width: auto;
  max-width: min(100%, 18rem);
  height: auto;
  object-fit: contain;
}

/* المحتوى والهيدر والفوتر: إزاحة بعرض الشريط من اليمين */
html[dir="rtl"] body.layout-fixed .content-wrapper {
  margin-left: 0 !important;
  margin-right: 250px !important;
}

html[dir="rtl"] body.layout-fixed .main-header {
  margin-left: 0 !important;
  margin-right: 250px !important;
}

html[dir="rtl"] body.layout-fixed .main-footer {
  margin-left: 0 !important;
  margin-right: 250px !important;
}

/* عند طي الشريط (sidebar-collapse) */
html[dir="rtl"] body.sidebar-collapse .main-sidebar,
html[dir="rtl"] body.sidebar-mini.sidebar-collapse .main-sidebar {
  width: 4.6rem !important;
}

html[dir="rtl"] body.sidebar-mini.sidebar-collapse .content-wrapper,
html[dir="rtl"] body.sidebar-mini.sidebar-collapse .main-header,
html[dir="rtl"] body.sidebar-mini.sidebar-collapse .main-footer {
  margin-right: 4.6rem !important;
}

/* محتوى عربي محاذاة لليمين */
html[dir="rtl"] .content-wrapper .content-header h1 {
  text-align: right;
}

html[dir="rtl"] .card,
html[dir="rtl"] .card .card-body {
  text-align: right;
}

html[dir="rtl"] .main-footer {
  text-align: right;
}

/* صفحة الدخول */
html[dir="rtl"] .login-page .login-logo,
html[dir="rtl"] .login-page .login-box-msg {
  text-align: center;
}

html[dir="rtl"] .login-page .login-card-body form .row {
  flex-direction: row-reverse;
}

html[dir="rtl"] .login-page .input-group {
  flex-direction: row-reverse;
}

html[dir="rtl"] .login-page .input-group .form-control {
  text-align: right;
}

html[dir="rtl"] .login-page .custom-control {
  padding-right: 1.5rem;
  padding-left: 0;
}

html[dir="rtl"] .login-page .custom-control-label {
  margin-right: 0.5rem;
  margin-left: 0;
}

html[dir="rtl"] .login-page .custom-control-input {
  right: 0;
  left: auto;
}

/* الشريط الجانبي */
html[dir="rtl"] .nav-sidebar .nav-link p {
  text-align: right;
}

/* ========== تحسين القائمة الجانبية (RTL): مسافات، خط، تمييز رئيسي/فرعي ========== */

html[dir="rtl"] .main-sidebar .brand-link {
  padding-right: 0.75rem;
  padding-left: 0.75rem;
}

/* إزالة الفراغ الزائد من جهة حافة الشريط (اليمين في RTL) */
html[dir="rtl"] .main-sidebar .sidebar {
  padding-left: 0;
  padding-right: 0;
}

html[dir="rtl"] .main-sidebar .nav-sidebar {
  padding-left: 0.25rem;
  padding-right: 0;
}

/* أيقونة العنصر بمحاذاة حافة الشريط دون فراغ كبير */
html[dir="rtl"] .main-sidebar .nav-sidebar .nav-link {
  position: relative;
  align-items: center;
  border-radius: 0;
  margin-right: 0 !important;
  margin-left: 0 !important;
}

html[dir="rtl"] .main-sidebar .nav-sidebar > .nav-item > .nav-link > .nav-icon {
  position: absolute;
  right: 0.65rem;
  left: auto;
  top: 50%;
  transform: translateY(-50%);
  margin: 0 !important;
  width: 1.25rem;
  text-align: center;
}

/* نص العناصر الرئيسية (بدون الشجرة الداخلية): حجم أصغر قليلاً ومسافة للأيقونة */
html[dir="rtl"] .main-sidebar .nav-sidebar > .nav-item > .nav-link {
  font-size: 0.875rem;
  font-weight: 600;
  line-height: 1.35;
  padding: 0.45rem 0.65rem 0.45rem 0.75rem !important;
  padding-right: 2.35rem !important;
}

/* رأس مجموعة الشجرة (تقارير المبيعات، إعدادات النظام، …) */
html[dir="rtl"] .main-sidebar .nav-sidebar .nav-item.has-treeview > .nav-link {
  font-size: 0.875rem;
  font-weight: 600;
  padding: 0.45rem 0.65rem 0.45rem 0.75rem !important;
  padding-right: 2.35rem !important;
}

html[dir="rtl"] .main-sidebar .nav-sidebar .nav-item.has-treeview > .nav-link > .nav-icon {
  right: 0.65rem;
}

/* سهم الطي بجانب النص (في RTL يُعرض بشكل أوضح مع انعكاس) */
html[dir="rtl"] .main-sidebar .nav-sidebar .nav-link .right {
  position: absolute;
  left: 0.65rem;
  right: auto;
  top: 50%;
  transform: translateY(-50%) rotate(180deg);
  float: none;
  margin: 0 !important;
  font-size: 0.75rem;
  opacity: 0.85;
}

/* مسافة للنص حتى لا يتداخل مع السهم (داخل <p>) */
html[dir="rtl"] .main-sidebar .nav-item.has-treeview > .nav-link > p {
  margin-bottom: 0;
  padding-left: 1.1rem;
}

/* تمييز العناصر الفرعية: أصغر، أفتح، مسافة بادئة نحو يسار الشاشة */
html[dir="rtl"] .main-sidebar .nav-treeview {
  padding-right: 0;
  padding-left: 0;
  margin-right: 0;
  border-right: 2px solid rgba(255, 255, 255, 0.12);
  margin-top: 0.15rem;
  margin-bottom: 0.25rem;
}

html[dir="rtl"] .main-sidebar .nav-treeview .nav-link {
  font-size: 0.8125rem;
  font-weight: 400;
  color: rgba(255, 255, 255, 0.78) !important;
  padding: 0.32rem 0.5rem 0.32rem 1.5rem !important;
  padding-right: 1.85rem !important;
  border-radius: 0;
}

html[dir="rtl"] .main-sidebar .nav-treeview .nav-link:hover {
  color: #fff !important;
  background-color: rgba(255, 255, 255, 0.07);
}

html[dir="rtl"] .main-sidebar .nav-treeview .nav-icon {
  font-size: 0.65rem !important;
  opacity: 0.8;
  margin-left: 0.35rem !important;
  margin-right: 0 !important;
  position: static;
  transform: none;
}

/* تمييز العنصر النشط في الشجرة */
html[dir="rtl"] .main-sidebar .nav-sidebar .nav-link.active {
  box-shadow: none;
  width: 100%;
  box-sizing: border-box;
}

html[dir="rtl"] .main-sidebar .sidebar-dark-primary .nav-sidebar > .nav-item > .nav-link.active {
  background-color: rgba(0, 123, 255, 0.92);
  color: #fff !important;
}

html[dir="rtl"] .main-sidebar .nav-treeview .nav-link.active {
  background-color: rgba(255, 255, 255, 0.12) !important;
  color: #fff !important;
  font-weight: 500;
}

/* عناصر رئيسية بلا شجرة (الرئيسية، الخصوصية): نفس مقاس السطر */
html[dir="rtl"] .main-sidebar .nav-sidebar > .nav-item:not(.has-treeview) > .nav-link {
  font-size: 0.875rem;
}

/* ========== استجابة للشاشات الصغيرة (موبايل / تابلت) ========== */
@media (max-width: 991.98px) {
  html[dir="rtl"] body.layout-fixed .content-wrapper,
  html[dir="rtl"] body.layout-fixed .main-header,
  html[dir="rtl"] body.layout-fixed .main-footer {
    margin-right: 0 !important;
  }

  /*
    AdminLTE يخفي الشريط الجانبي بـ margin سالب على LTR؛ في RTL يجب إزاحته يميناً.
    بدون هذا قد يبقى الشريط ظاهراً أو يتعارض مع المحتوى على الموبايل.
  */
  html[dir="rtl"] .main-sidebar,
  html[dir="rtl"] .main-sidebar::before {
    margin-left: 0 !important;
    margin-right: -250px !important;
  }

  html[dir="rtl"] body.sidebar-open .main-sidebar,
  html[dir="rtl"] body.sidebar-open .main-sidebar::before {
    margin-right: 0 !important;
  }

  .wrapper {
    overflow-x: hidden;
  }

  .content-wrapper > .content-header {
    padding: 0.75rem 0.5rem;
  }

  .content-wrapper .content-header h1 {
    font-size: 1.2rem;
  }

  .content-wrapper > .content > .container-fluid {
    padding-left: 0.5rem;
    padding-right: 0.5rem;
  }

  .card {
    margin-bottom: 0.75rem;
  }

  .card-header,
  .card-body {
    padding: 0.65rem 0.75rem;
  }

  .table-responsive {
    -webkit-overflow-scrolling: touch;
  }

  .main-sidebar {
    box-shadow: 0 0 1rem rgba(0, 0, 0, 0.15);
  }

  /* شريط علوي: صف واحد قدر الإمكان، بدون تمرير أفقي */
  .main-header.main-header--toolbar {
    flex-wrap: nowrap;
    align-items: center;
    min-height: 3.5rem;
    padding-left: 0.35rem;
    padding-right: 0.35rem;
    max-width: 100%;
  }

  .main-header.main-header--toolbar .navbar-nav {
    max-width: 100%;
  }

  .main-header .main-header-start {
    flex: 0 0 auto;
  }

  .main-header .nav-link--pushmenu {
    padding: 0.5rem 0.65rem;
    min-width: 2.75rem;
    text-align: center;
  }
}

@media (max-width: 575.98px) {
  .content .form-row > [class*="col-"] {
    flex: 0 0 100%;
    max-width: 100%;
  }

  .btn {
    white-space: normal;
  }

  .navbar .nav-link {
    padding-left: 0.4rem;
    padding-right: 0.4rem;
  }

  .main-header .nav-link--logout {
    font-size: 0.9rem;
    padding: 0.4rem 0.35rem !important;
  }
}
