/*
 * SMART SYSTEMS compact responsive layer v1
 * Keeps the application usable on small laptops and high-DPI Windows screens
 * without scaling the whole document or hiding primary actions.
 */

body.ui-v19 #appScreen,
body.ui-v19 #appScreen .main,
body.ui-v19 #appScreen .page,
body.ui-v19 #appScreen .page > *,
body.ui-v19 #appScreen .perm-panel,
body.ui-v19 #appScreen .settings-content {
  min-width: 0;
  max-width: 100%;
}

body.ui-v19 #appScreen :where(.table-wrap,.table-scroll,.data-table-wrap,.report-table-wrap) {
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
}

/* A 1366px laptop and a high-DPI laptop with a short logical viewport both
   receive the same comfortable, compact density. */
@media screen and (max-width: 1680px), screen and (max-height: 1180px) {
  body.ui-v19 {
    /* حجم الخط وعرض القائمة ملك لتفضيلات المستخدم. لا تُعرّفهما هنا على body؛
       لأن متغيرات body تتغلب على القيم المحفوظة على :root في كل الواجهات. */
    --v19-page-pad: 10px;
    --v19-gap: 8px;
    --v19-control-height: 36px;
  }

  body.ui-v19 #appScreen .main {
    width: calc(100% - var(--v19-sidebar-width)) !important;
    padding: var(--v19-page-pad) !important;
    scrollbar-gutter: stable;
  }

  body.ui-v19 #appScreen .sidebar,
  body.ui-v19 #appScreen:not(.sidebar-left) .sidebar,
  body.ui-v19:not(.ui-dark) #appScreen .sidebar,
  body.ui-v19.ui-dark #appScreen .sidebar {
    width: var(--v19-sidebar-width) !important;
  }

  body.ui-v19 .sidebar-head { padding: 8px 8px 7px !important; }
  body.ui-v19 #sidebarCompanyLogo {
    width: 34px !important;
    height: 34px !important;
    margin-bottom: 4px !important;
  }
  body.ui-v19 .nav { padding: 5px !important; }
  body.ui-v19 .nav-group-label {
    min-height: 32px !important;
    padding: 5px 7px !important;
    border-radius: 8px !important;
    font-size: calc(11.5 / 14 * var(--app-font-size)) !important;
  }
  body.ui-v19 .nav-item,
  body.ui-v19:not(.ui-dark) .nav-item,
  body.ui-v19.ui-dark .nav-item {
    min-height: 31px !important;
    max-height: 38px !important;
    padding: 5px 7px !important;
    gap: 6px !important;
    border-radius: 8px !important;
    font-size: calc(11.75 / 14 * var(--app-font-size)) !important;
  }
  body.ui-v19 .sidebar-foot { padding: 5px !important; }
  body.ui-v19 .sidebar-foot button,
  body.ui-v19 .sidebar-logout-btn,
  body.ui-v19 .ios-theme-toggle.compact { min-height: 34px !important; }

  body.ui-v19 .page-head {
    gap: 8px !important;
    margin-bottom: 9px !important;
  }
  /* حجم العنوان يتبع تفضيل العرض العام ولا تحدده طبقة كثافة الشاشة. */
  body.ui-v19 :where(.card,.dash-card,.settings-card,.kpi-card,.dashboard-widget) {
    padding: 10px !important;
  }
  body.ui-v19 :where(.form-grid,.filters-grid,.settings-account-grid) { gap: 8px !important; }

  body.ui-v19 input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
  body.ui-v19 select,
  body.ui-v19 textarea {
    min-height: 36px !important;
    padding-block: 6px !important;
  }
  body.ui-v19 button,
  body.ui-v19 .btn-primary,
  body.ui-v19 .btn-ghost,
  body.ui-v19 .btn-danger {
    min-height: 34px;
    padding-block: 6px;
  }
  body.ui-v19 table :where(th,td) {
    padding: 7px 9px !important;
    line-height: 1.35;
  }

  /* Roles and permissions: one bounded workspace. The permissions scroll in
     the middle while Save remains visible at the bottom. */
  body.ui-v19 #page-roles .roles-page-head {
    min-height: 0;
    padding: 8px 10px !important;
    margin-bottom: 8px !important;
  }
  body.ui-v19 #page-roles .roles-page-head p { margin-top: 2px; line-height: 1.35; }
  body.ui-v19 #page-roles .roles-page-actions { gap: 6px; }
  body.ui-v19 #page-roles .roles-layout {
    grid-template-columns: minmax(160px, 188px) minmax(0, 1fr) !important;
    gap: 8px !important;
    height: calc(100dvh - 156px);
    min-height: 470px;
    align-items: stretch !important;
  }
  body.ui-v19 #page-roles .role-sidebar {
    position: relative !important;
    inset: auto !important;
    height: 100%;
    min-height: 0;
    padding: 7px !important;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
  body.ui-v19 #page-roles .role-sidebar-head p { display: none; }
  body.ui-v19 #page-roles .role-search {
    flex: 0 0 auto;
    min-height: 34px !important;
    margin: 6px 0 !important;
  }
  body.ui-v19 #page-roles .role-list {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
  }
  body.ui-v19 #page-roles .role-btn {
    min-height: 34px !important;
    padding: 6px 7px !important;
    font-size: calc(11.75 / 14 * var(--app-font-size)) !important;
  }
  body.ui-v19 #page-roles .perm-panel {
    height: 100%;
    min-height: 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden !important;
  }
  body.ui-v19 #page-roles .perm-panel-head {
    flex: 0 0 auto;
    padding: 8px 11px !important;
    gap: 7px !important;
  }
  body.ui-v19 #page-roles .perm-panel-head h2 {
    font-size: calc(14 / 14 * var(--app-font-size)) !important;
  }
  body.ui-v19 #page-roles :where(.access-center-stats,.permission-level-flow,.perm-package-note) {
    flex: 0 0 auto;
  }
  body.ui-v19 #page-roles #v255PermissionCommand.permission-command-grid {
    flex: 0 0 auto;
    grid-template-columns: minmax(150px, 1fr) auto minmax(145px, .65fr) auto !important;
    gap: 6px !important;
    padding: 6px 8px !important;
  }
  body.ui-v19 #page-roles .perm-controlbar {
    position: relative !important;
    top: auto !important;
    flex: 0 0 auto;
    min-height: 42px !important;
    padding: 5px 8px !important;
    box-shadow: 0 3px 9px rgba(15,35,70,.08) !important;
  }
  body.ui-v19 #page-roles .perm-view-tab {
    min-height: 32px !important;
    padding: 5px 8px !important;
  }
  body.ui-v19 #page-roles .perm-matrix {
    flex: 1 1 auto;
    min-height: 0;
    padding: 6px 8px !important;
    overflow: auto !important;
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
  }
  html body.ui-v19 #page-roles .rbac-matrix .rbac-head,
  html body.ui-v19 #page-roles .rbac-matrix .rbac-row {
    grid-template-columns: minmax(145px, 1.35fr) repeat(5, minmax(40px, .34fr)) minmax(160px, 1fr) !important;
    gap: 4px !important;
  }
  html body.ui-v19 #page-roles .rbac-matrix > .rbac-head {
    top: 0 !important;
    min-height: 40px !important;
    padding: 6px 7px !important;
    font-size: 11px !important;
  }
  html body.ui-v19 #page-roles .rbac-section-title {
    top: 40px !important;
    padding: 6px 9px !important;
  }
  html body.ui-v19 #page-roles .rbac-row {
    min-height: 48px !important;
    padding: 5px 7px !important;
  }
  html body.ui-v19 #page-roles .rbac-cell { min-height: 34px !important; }
  html body.ui-v19 #page-roles .rbac-check {
    width: 34px !important;
    height: 34px !important;
  }
  html body.ui-v19 #page-roles .rbac-check span {
    width: 21px !important;
    height: 21px !important;
  }
  html body.ui-v19 #page-roles .rbac-scope-choice {
    min-height: 34px !important;
    padding: 5px 7px !important;
  }
  body.ui-v19 #page-roles .perm-save-bar {
    position: relative !important;
    inset: auto !important;
    z-index: 95;
    flex: 0 0 auto;
    min-height: 50px;
    padding: 7px 10px !important;
    display: flex;
    align-items: center;
    justify-content: space-between !important;
    gap: 10px;
    box-shadow: 0 -5px 16px rgba(15,35,70,.08);
  }
  body.ui-v19 #page-roles .perm-save-copy {
    min-width: 0;
    font-size: calc(11.5 / 14 * var(--app-font-size));
  }
  body.ui-v19 #page-roles #savePermsBtn { flex: 0 0 auto; }
}

/* The sidebar becomes a drawer before it can steal the working width. */
@media screen and (max-width: 1180px) {
  body.ui-v19 #appScreen { display: block !important; }
  body.ui-v19 #appScreen .main {
    width: 100% !important;
    max-width: 100vw !important;
    padding: 9px !important;
  }
  body.ui-v19 #appScreen .sidebar,
  body.ui-v19 #appScreen:not(.sidebar-left) .sidebar,
  body.ui-v19:not(.ui-dark) #appScreen .sidebar,
  body.ui-v19.ui-dark #appScreen .sidebar {
    position: fixed !important;
    top: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    left: auto !important;
    z-index: 1200 !important;
    width: min(78vw, 244px) !important;
    height: 100dvh !important;
    transform: translateX(110%) !important;
    transition: transform .2s ease !important;
  }
  body.ui-v19 #appScreen .sidebar.mobile-open { transform: translateX(0) !important; }
  body.ui-v19 #appScreen.sidebar-left .sidebar {
    right: auto !important;
    left: 0 !important;
    transform: translateX(-110%) !important;
  }
  body.ui-v19 #appScreen.sidebar-left .sidebar.mobile-open { transform: translateX(0) !important; }
  body.ui-v19 #mobileSidebarToggle {
    display: inline-flex !important;
    min-height: 36px !important;
    margin-bottom: 7px !important;
    padding: 6px 10px !important;
  }
  body.ui-v19 #mobileSidebarClose {
    display: grid !important;
    place-items: center;
    width: 36px !important;
    height: 36px !important;
  }
}

@media screen and (max-width: 900px) {
  body.ui-v19 #page-roles .roles-page-head { align-items: stretch !important; }
  body.ui-v19 #page-roles .roles-title-block p { display: none; }
  body.ui-v19 #page-roles .roles-layout {
    grid-template-columns: minmax(0, 1fr) !important;
    height: auto;
    min-height: 0;
  }
  body.ui-v19 #page-roles .role-sidebar {
    height: auto;
    max-height: 150px !important;
  }
  body.ui-v19 #page-roles .role-list {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(150px, 210px);
    overflow-x: auto;
    overflow-y: hidden;
  }
  body.ui-v19 #page-roles .perm-panel {
    height: calc(100dvh - 238px);
    min-height: 430px;
  }
  html body.ui-v19 #page-roles .rbac-matrix .rbac-row {
    grid-template-columns: minmax(0, 1fr) repeat(5, 38px) !important;
  }
  body.ui-v19 #page-roles #v255PermissionCommand.permission-command-grid {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) !important;
  }
}

@media screen and (max-width: 600px) {
  body.ui-v19 { --v19-page-pad: 7px; }
  body.ui-v19 #page-roles .roles-page-actions { display: grid; grid-template-columns: 1fr 1fr; }
  body.ui-v19 #page-roles .roles-page-actions button { min-width: 0; white-space: normal; }
  body.ui-v19 #page-roles .perm-panel { height: calc(100dvh - 225px); min-height: 400px; }
  body.ui-v19 #page-roles .perm-panel-head { padding: 6px 8px !important; }
  body.ui-v19 #page-roles #v255PermissionCommand.permission-command-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }
  body.ui-v19 #page-roles .perm-save-copy span { display: none; }
  body.ui-v19 #page-roles #savePermsBtn { min-width: min(58vw, 220px); }
}

/* Do not reduce touch targets below a usable size on tablets. */
@media (pointer: coarse) {
  body.ui-v19 button,
  body.ui-v19 .nav-item,
  body.ui-v19 input,
  body.ui-v19 select { min-height: 40px !important; }
}

@media print {
  /* تبقى الطباعة على مقاس القالب نفسه دون تغيير تفضيل العرض داخل البرنامج. */
}
