/**
 * SCAD — global mobile / tablet responsiveness (shell + shared patterns).
 * Loaded on every back-office page via scadRenderBackofficeThemeStack().
 */

/* Prevent accidental horizontal page scroll from wide tables/toolbars */
html {
  overflow-x: clip;
}

body.backoffice-body {
  overflow-x: clip;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* Desktop: menu lives in the sidebar collapse control */
@media (min-width: 993px) {
  .bo-topbar__menu,
  .bo-sidebar-toggle {
    display: none !important;
  }
}

@media (max-width: 992px) {
  /* Drawer sidebar */
  .bo-right {
    margin-left: 0 !important;
    margin-right: 0 !important;
    width: 100%;
    max-width: 100vw;
  }

  [dir="rtl"] .bo-right {
    margin-left: 0 !important;
    margin-right: 0 !important;
  }

  .bo-sidebar {
    width: min(var(--scad-shell-sidebar-w, 248px), 86vw) !important;
    max-width: 320px;
    transform: translateX(-100%);
    z-index: 420 !important;
  }

  .bo-sidebar.open {
    transform: translateX(0);
  }

  [dir="rtl"] .bo-sidebar {
    transform: translateX(100%);
  }

  [dir="rtl"] .bo-sidebar.open {
    transform: translateX(0);
  }

  .bo-sidebar-overlay {
    z-index: 410;
  }

  .bo-sidebar-overlay.open {
    display: block;
  }

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

  body.bo-sidebar-mobile-open .bo-sidebar-nav {
    overscroll-behavior: contain;
  }

  /* Prefer topbar hamburger; hide the floating duplicate only when that button exists */
  .bo-topbar__menu {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    margin: 0;
    border-radius: 10px;
    flex-shrink: 0;
  }

  body:has(#topbarMenuBtn) .bo-sidebar-toggle,
  body:has(#topbarMenuBtn) button.bo-sidebar-toggle {
    display: none !important;
  }

  body:not(:has(#topbarMenuBtn)) .bo-sidebar-toggle {
    display: flex !important;
    align-items: center;
    justify-content: center;
  }

  /* Topbar compaction */
  .bo-topbar,
  .bo-topbar--erp {
    position: sticky;
    top: 0;
    z-index: 200;
    gap: 0.5rem !important;
    padding: 0.55rem 0.75rem !important;
    padding-top: calc(0.55rem + env(safe-area-inset-top, 0px)) !important;
    min-height: 0;
  }

  .bo-topbar--erp {
    grid-template-columns: minmax(0, 1fr) auto !important;
    align-items: center;
  }

  .bo-topbar--erp .bo-topbar__center {
    display: none !important;
  }

  .bo-topbar__left,
  .bo-topbar--erp .bo-topbar__left {
    min-width: 0;
    overflow: hidden;
  }

  .bo-topbar__right,
  .bo-topbar--erp .bo-topbar__right,
  .bo-topbar__util {
    min-width: 0;
    flex-wrap: nowrap;
    justify-content: flex-end;
  }

  .bo-topbar__module-label,
  .bo-topbar__module-caret,
  .bo-topbar__help,
  .bo-topbar__divider,
  .bo-topbar__user-role,
  .bo-topbar__profile-text {
    display: none !important;
  }

  .bo-topbar__module {
    padding: 0.3rem 0.5rem;
    font-size: 0.78rem;
    max-width: 46vw;
    overflow: hidden;
  }

  .bo-topbar__module strong {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .bo-topbar__title {
    font-size: 1rem !important;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 48vw;
  }

  .bo-topbar__user-name,
  .bo-topbar__user {
    display: none !important;
  }

  .bo-btn-logout:not(.bo-btn-logout--icon) {
    padding: 0.45rem;
    font-size: 0;
    gap: 0;
  }

  .bo-btn-logout:not(.bo-btn-logout--icon) i {
    font-size: 1rem;
  }

  .bo-appearance-switcher .bo-appearance-btn[data-scad-color-mode="system"] {
    display: none;
  }

  .bo-lang-switcher {
    border-radius: 8px;
    flex-shrink: 0;
  }

  /* Content gutters + safe area */
  .bo-main,
  .pr-main {
    padding: 0.85rem 0.75rem calc(1rem + env(safe-area-inset-bottom, 0px)) !important;
  }

  /* Module tabs: horizontal scroll instead of wrapping mess */
  .pr-main-tabs,
  [class$="-main-tabs"],
  .bo-tabs,
  .nav-tabs {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    gap: 0.15rem;
  }

  .pr-main-tab,
  [class$="-main-tab"],
  .bo-tabs .bo-tab,
  .nav-tabs .nav-link {
    flex: 0 0 auto;
    white-space: nowrap;
    padding-inline: 0.85rem;
  }

  /* Tables: always scroll inside a wrap */
  .bo-table-wrap,
  .table-responsive,
  [class*="-table-wrap"],
  [class*="table-wrap"],
  .scad-datagrid,
  .dg-wrap,
  .pr-table-wrap,
  .ab-dash-table-wrap {
    display: block;
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  table.bo-table,
  table.pr-table,
  table.data-table,
  table.ab-dash-fuel-table,
  .bo-table-wrap > table,
  .table-responsive > table,
  [class*="-table-wrap"] > table,
  .scad-datagrid table,
  .dg-wrap table {
    min-width: 560px;
  }

  /* Filter / toolbar stacks */
  .bo-filter-bar,
  .db-filter,
  .db-filter.bo-filter-bar,
  .bo-toolbar,
  [class$="-toolbar"],
  .dh-head,
  .pr-page-head,
  .gm-entry-head,
  .bl-entry-head,
  .rc-entry-head {
    flex-wrap: wrap;
  }

  .dh-head,
  .pr-page-head,
  .gm-entry-head,
  .bl-entry-head,
  .rc-entry-head {
    flex-direction: column;
    align-items: stretch !important;
    gap: 0.65rem;
  }

  .dh-head__actions,
  .pr-page-head__actions,
  [class$="-head__actions"] {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
  }

  .bo-toolbar__left,
  .bo-toolbar__right,
  [class$="-toolbar__left"],
  [class$="-toolbar__right"] {
    width: 100%;
  }

  /* Form / KPI grids collapse */
  .bo-form-grid,
  .bo-measure-grid,
  .pr-form-row--2,
  .pr-form-row--3,
  .pr-form-row--5,
  .pr-form-row--6,
  .pr-summary-row,
  .pr-filters__row,
  [class$="-filters"],
  [class$="-kpi"],
  .bo-kpi-grid,
  .bo-kpi-grid--saas {
    grid-template-columns: 1fr !important;
  }

  .bo-kpi-grid--saas,
  #kpiGrid.bo-kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  /* Cards / charts */
  .bo-card,
  [class$="-dash-card"],
  [class$="-card"] {
    max-width: 100%;
  }

  canvas {
    max-width: 100% !important;
  }

  /* Modals / drawers — near full width on phones */
  .bo-modal-backdrop.open .bo-modal,
  .bo-modal {
    width: min(100vw, 100%) !important;
    max-width: 100vw !important;
    border-radius: 0 !important;
  }

  .bo-modal__header,
  .bo-modal__footer,
  [class$="-modal__header"],
  [class$="-modal__footer"] {
    padding-left: max(0.85rem, env(safe-area-inset-left, 0px));
    padding-right: max(0.85rem, env(safe-area-inset-right, 0px));
  }

  /* Notification panel stays on-screen */
  .bo-topbar__notif-panel {
    position: fixed;
    top: calc(3.5rem + env(safe-area-inset-top, 0px));
    left: 0.5rem;
    right: 0.5rem;
    width: auto;
    max-width: none;
  }

  [dir="rtl"] .bo-topbar__notif-panel {
    left: 0.5rem;
    right: 0.5rem;
  }

  /* Toast host */
  .scad-chat-toast-host {
    left: 0.75rem;
    right: 0.75rem;
    width: auto;
    bottom: calc(5.5rem + env(safe-area-inset-bottom, 0px));
  }

  [dir="rtl"] .scad-chat-toast-host {
    left: 0.75rem;
    right: 0.75rem;
  }

  /* Touch-friendlier controls */
  .pr-btn,
  .bo-btn,
  .dz-btn,
  button.ab-dash-btn,
  .bo-sidebar-menu a,
  .bo-sidebar-toggle-item {
    min-height: 42px;
  }

  input,
  select,
  textarea {
    font-size: 16px !important; /* avoids iOS zoom-on-focus */
  }
}

@media (max-width: 560px) {
  .bo-kpi-grid--saas,
  #kpiGrid.bo-kpi-grid {
    grid-template-columns: 1fr !important;
  }

  .bo-topbar--erp .bo-appearance-switcher,
  .bo-topbar .bo-appearance-switcher {
    display: none;
  }

  .pr-main-tab i,
  [class$="-main-tab"] i {
    margin: 0;
  }

  /* On very small screens, icon-only tabs when label is long — keep labels, just smaller */
  .pr-main-tab,
  [class$="-main-tab"] {
    font-size: 0.78rem;
    padding: 0.65rem 0.7rem;
  }

  /* Module chip collides with language / alerts; the page title already names the module */
  .bo-topbar__module {
    display: none !important;
  }
}

/* Phone registers: pinned Status + Actions cover the row. Keep the checkbox and id. */
@media (max-width: 768px) {
  th.scad-pin-id-2,
  td.scad-pin-id-2,
  th.scad-pin-status,
  td.scad-pin-status,
  th.scad-pin-status-2,
  td.scad-pin-status-2,
  th.scad-pin-actions,
  td.scad-pin-actions,
  .ab-reg-table th.ab-reg-col-ref,
  .ab-reg-table td.ab-reg-col-ref,
  .ab-reg-table th.ab-reg-col-status,
  .ab-reg-table td.ab-reg-col-status,
  .ab-reg-table th.ab-reg-col-status-2,
  .ab-reg-table td.ab-reg-col-status-2,
  .ab-reg-table th.ab-reg-col-actions,
  .ab-reg-table td.ab-reg-col-actions {
    inset-inline-start: auto !important;
    inset-inline-end: auto !important;
    left: auto !important;
    right: auto !important;
    box-shadow: none !important;
  }

  .ab-reg-table td.ab-reg-col-ref,
  .ab-reg-table td.ab-reg-col-status,
  .ab-reg-table td.ab-reg-col-status-2,
  .ab-reg-table td.ab-reg-col-actions,
  td.scad-pin-id-2,
  td.scad-pin-status,
  td.scad-pin-status-2,
  td.scad-pin-actions {
    position: static !important;
  }

  th.scad-pin-id,
  td.scad-pin-id {
    min-width: 6.25rem;
    max-width: 8.5rem;
  }

  .ab-reg-head {
    align-items: stretch;
  }

  .ab-reg-head__actions {
    width: 100%;
  }

  .ab-reg-head__actions .ab-reg-btn {
    flex: 1 1 calc(50% - 0.5rem);
    min-width: min(100%, 9.5rem);
    white-space: normal;
    line-height: 1.25;
    text-align: center;
  }

  .ab-reg-filters__grid,
  .ab-reg-filters__grid--row2,
  .ab-report-types {
    grid-template-columns: 1fr !important;
  }

  .ab-reg-table-scroll,
  .scad-bi-table-scroll,
  .bo-datagrid-wrap {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .scad-grid-pager {
    justify-content: center;
  }

  .pr-main-tabs,
  [class$="-main-tabs"] {
    max-width: 100%;
  }

  input[type="date"],
  input[type="time"],
  input[type="datetime-local"],
  input[type="number"] {
    max-width: 100%;
    min-width: 0;
  }
}

/* Login page — phone-friendly */
@media (max-width: 900px) {
  body.login-body {
    min-height: 100dvh;
  }

  .login-page,
  .login-split {
    min-height: 100dvh;
  }

  .login-glass__inner {
    padding: 1.15rem 1.1rem 1rem;
  }

  .login-submit {
    min-height: 48px;
    width: 100%;
  }

  .login-input,
  .login-input--password {
    font-size: 16px !important;
  }
}

/* Landscape phones: keep content usable */
@media (max-width: 992px) and (max-height: 480px) {
  .bo-sidebar-brand {
    min-height: 4.5rem !important;
    padding: 0.75rem 1rem !important;
  }

  .bo-main,
  .pr-main {
    padding-top: 0.5rem !important;
  }
}
