/**
 * AIO ERP Mobile Responsive Improvements
 * Critical fixes for mobile/tablet responsiveness
 */

/* ============================================
   1. SIDEBAR MOBILE RESPONSIVENESS
   ============================================ */

@media (max-width: 1023.98px) {
  /* Hide sidebar off-screen by default.
     Boundary is 1023.98px (not 991px) to match the sidebar's Tailwind `lg`
     (1024px) reveal point — otherwise 992–1023px had neither the static
     sidebar nor the mobile toggle (dead zone). */
  .side-bar {
    position: fixed !important;
    left: 0;
    top: 0;
    height: 100vh;
    z-index: 1030;
    transform: translateX(-100%);
    transition: transform 0.3s ease-in-out;
    background-color: var(--bg-surface, #fff);
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15);
    overflow-y: auto;
    width: 260px !important;
  }

  /* Show when existing app handler adds small-view-side-active.
     Two-class specificity (0-2-0) beats bundle-foundation.css's single-class
     z-index: 1001 !important even though that file loads later. */
  .side-bar.small-view-side-active {
    transform: translateX(0) !important;
    z-index: 1150 !important;
  }

  /* Ensure the business switch button has a proper tap target on mobile */
  .app-sidebar-switch-btn {
    min-width: 36px !important;
    min-height: 36px !important;
    width: 36px !important;
    height: 36px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
  }

  .app-premium-main,
  .content-wrapper,
  .main-content {
    margin-left: 0 !important;
    width: 100% !important;
  }

  #mobile-sidebar-toggle {
    display: inline-flex !important;
  }
}

@media (min-width: 1024px) {
  #mobile-sidebar-toggle {
    display: none !important;
  }
}

/* ============================================
   2. SELECT2 DROPDOWN WIDTH FIX
   ============================================ */

/* Allow Select2 JS to set the natural container-matched width;
   only clamp to prevent viewport overflow. */
.select2-container--open .select2-dropdown {
  max-width: calc(100vw - 20px) !important;
  max-height: 300px;
  overflow-y: auto;
  box-sizing: border-box !important;
}

@media (max-width: 991px) {
  .select2-container--open .select2-dropdown {
    /* Do NOT set width: auto — that shrinks the dropdown below its container width.
       Let Select2 JS set width to match container; only cap max-width. */
    min-width: unset !important;
    max-width: calc(100vw - 20px) !important;
    right: auto !important;
    box-sizing: border-box !important;
  }

  .select2-container {
    max-width: 100% !important;
  }
}

@media (max-width: 576px) {
  .select2-container--open .select2-dropdown {
    max-width: calc(100vw - 16px) !important;
  }

  .select2-container {
    width: 100% !important;
  }
}

/* ============================================
   2b. ACTION DROPDOWN — MOBILE SCROLL FIX
   ============================================ */

@media (max-width: 991px) {
  /* Limit tall dropdowns so the page stays scrollable behind them.
     JS in responsive-mobile.js dynamically clamps position:fixed menus. */
  .dropdown-menu {
    max-height: 65vh !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
  }
}

/* ============================================
   3. FORM LAYOUT RESPONSIVE
   ============================================ */

@media (max-width: 768px) {
  .form-group.col-md-4,
  .form-group.col-md-6,
  .form-group.col-md-3,
  .form-group.col-md-12 {
    flex: 0 0 100% !important;
    max-width: 100% !important;
    margin-bottom: 12px;
  }

  .form-control,
  .select2-container,
  input[type='text'],
  input[type='email'],
  input[type='number'],
  input[type='date'],
  textarea,
  select {
    width: 100% !important;
    max-width: 100% !important;
  }
}

/* ============================================
   4. DATATABLE MOBILE
   ============================================ */

@media (max-width: 768px) {
  .dataTables_wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  table.dataTable {
    min-width: 600px;
    font-size: 13px;
  }

  table.dataTable .btn-group .btn,
  table.dataTable .btn-sm {
    padding: 4px 6px;
    font-size: 11px;
  }
}

/* ============================================
   5. MODAL MOBILE
   ============================================ */

@media (max-width: 576px) {
  .modal-dialog {
    margin: 8px;
    max-width: calc(100% - 16px);
  }

  .modal-dialog.modal-lg,
  .modal-dialog.modal-xl {
    max-width: calc(100% - 16px);
  }

  .modal-footer {
    flex-wrap: wrap;
    gap: 8px;
  }

  .modal-footer .btn {
    flex: 1 1 auto;
    min-width: 100px;
  }
}

@media (max-width: 991px) {
  /* Prevent tables inside modals from blowing past modal boundaries */
  .modal .table-responsive,
  .modal-body .table-responsive {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    max-width: 100% !important;
  }

  /* Override bundle-foundation.css `.view_modal .table:not(.no-border) { overflow: hidden !important }`
     That rule has specificity 0-3-0. We use 0-3-1 (3 classes + element type) to beat it.
     The tables inside view_modal need overflow-x: auto to enable horizontal scroll. */
  .view_modal .modal-body table.table,
  .modal-body table.table-bordered,
  .modal-body table.table-striped {
    display: block !important;
    overflow-x: auto !important;
    overflow-y: visible !important;
    -webkit-overflow-scrolling: touch !important;
    max-width: 100% !important;
    width: 100% !important;
  }

  /* Restore normal table layout inside the scroll wrapper */
  .view_modal .modal-body table.table thead,
  .view_modal .modal-body table.table tbody,
  .view_modal .modal-body table.table tfoot,
  .modal-body table.table-bordered thead,
  .modal-body table.table-bordered tbody,
  .modal-body table.table-bordered tfoot {
    display: table !important;
    width: 100% !important;
  }
}

/* ============================================
   6. TOUCH TARGETS
   ============================================ */

@media (max-width: 768px) {
  .form-control,
  .select2-container .select2-selection {
    min-height: 44px;
    font-size: 16px; /* Prevents auto-zoom on iOS */
  }

  .form-group label {
    margin-bottom: 6px;
    font-size: 13px;
    font-weight: 500;
  }
}

/* ============================================
   7. HEADER MOBILE
   ============================================ */

@media (max-width: 768px) {
  .navbar,
  .top-nav {
    padding: 8px 12px;
  }
}

/* ============================================
   8. SPACING ADJUSTMENTS
   ============================================ */

@media (max-width: 768px) {
  .content,
  .container-fluid {
    padding: 10px;
  }

  .card,
  .box,
  .widget {
    margin-bottom: 12px;
  }
}

/* ============================================
   9. DATERANGEPICKER MOBILE
   ============================================ */

@media (max-width: 767px) {
  /* Fix to viewport so the picker is always fully visible regardless of
     where the trigger input sits in the document. */
  .daterangepicker {
    position: fixed !important;
    top: 10px !important;
    left: 8px !important;
    right: 8px !important;
    bottom: auto !important;
    width: calc(100vw - 16px) !important;
    max-width: calc(100vw - 16px) !important;
    max-height: calc(100vh - 20px) !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    z-index: 9999 !important;
  }

  /* Hide second calendar — single calendar sufficient on small screens */
  .daterangepicker .drp-calendar.right {
    display: none !important;
  }

  /* Full-width single calendar */
  .daterangepicker .drp-calendar.left {
    border: 0 !important;
    padding: 8px !important;
    max-width: 100% !important;
    width: 100% !important;
    float: none !important;
    clear: both !important;
  }

  /* Presets list full-width */
  .daterangepicker .ranges {
    float: none !important;
    width: 100% !important;
    border-bottom: 1px solid var(--border-color, #e2e8f0);
  }

  .daterangepicker.show-calendar .ranges {
    margin: 0 !important;
    border-bottom: 1px solid var(--border-color, #e2e8f0) !important;
  }

  /* Calendar table fills available width */
  .daterangepicker .drp-calendar.left .calendar-table {
    width: 100% !important;
    padding: 0 !important;
  }

  .daterangepicker .drp-calendar.left .calendar-table table {
    width: 100% !important;
  }

  /* Action buttons row */
  .daterangepicker .drp-buttons {
    padding: 8px 12px !important;
    display: flex !important;
    justify-content: flex-end !important;
    gap: 8px !important;
  }

  .daterangepicker .drp-buttons .btn {
    flex: 0 0 auto !important;
  }
}

/* ============================================
   10. DATATABLE SPINNER — PREMIUM REDESIGN
   ============================================ */

/* Remove the card/container box and centre in the viewport (not the wrapper).
   position: fixed ensures visibility regardless of wrapper height.
   Specificity 0-3-2 beats bundle-shadcn.css's 0-3-1 selector. */
html body.premium-ui .dataTables_wrapper .dataTables_processing,
html body .dataTables_wrapper .dataTables_processing {
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
  padding: 0 !important;
  display: flex;
  align-items: center !important;
  justify-content: center !important;
  width: 64px !important;
  height: 64px !important;
  position: fixed !important;
  top: 50vh !important;
  left: 50vw !important;
  transform: translate(-50%, -50%) !important;
  margin: 0 !important;
  z-index: 9998 !important;
}

/* Premium dual-arc spinner with brand colors */
.table-spinner {
  width: 44px !important;
  height: 44px !important;
  border-width: 3px !important;
  border-style: solid !important;
  /* Dim track */
  border-color: rgba(20, 124, 187, 0.12) !important;
  /* Leading arc: brand blue */
  border-left-color: #147cbb !important;
  /* Trailing accent: brand orange */
  border-top-color: #fca424 !important;
  border-radius: 50% !important;
  animation: aio-spin-outer 0.72s cubic-bezier(0.5, 0.15, 0.5, 0.85) infinite !important;
  position: relative !important;
  display: block !important;
  margin: 0 auto !important;
}

/* Inner counter-rotating accent ring */
.table-spinner::after {
  content: '';
  position: absolute;
  inset: 7px;
  border-radius: 50%;
  border: 2px solid transparent;
  border-top-color: rgba(20, 124, 187, 0.35);
  border-bottom-color: rgba(252, 164, 36, 0.35);
  animation: aio-spin-inner 1.1s cubic-bezier(0.5, 0.15, 0.5, 0.85) infinite reverse;
}

@keyframes aio-spin-outer {
  to { transform: rotate(360deg); }
}

@keyframes aio-spin-inner {
  to { transform: rotate(360deg); }
}

/* Hide spinner when table has loaded data (tbody > tr visible) */
.dataTables_wrapper:has(tbody tr:not(.odd):not(.even)) .dataTables_processing {
  display: none !important;
}

/* ============================================
   11. GENERIC PREMIUM SPINNER — REUSABLE
   ============================================ */

/* Generic premium spinner that can be used anywhere in the app */
.aio-spinner {
  width: 44px !important;
  height: 44px !important;
  border-width: 3px !important;
  border-style: solid !important;
  /* Dim track */
  border-color: rgba(20, 124, 187, 0.12) !important;
  /* Leading arc: brand blue */
  border-left-color: #147cbb !important;
  /* Trailing accent: brand orange */
  border-top-color: #fca424 !important;
  border-radius: 50% !important;
  animation: aio-spin-outer 0.72s cubic-bezier(0.5, 0.15, 0.5, 0.85) infinite !important;
  position: relative !important;
  display: inline-block !important;
}

/* Inner counter-rotating accent ring */
.aio-spinner::after {
  content: '';
  position: absolute;
  inset: 7px;
  border-radius: 50%;
  border: 2px solid transparent;
  border-top-color: rgba(20, 124, 187, 0.35);
  border-bottom-color: rgba(252, 164, 36, 0.35);
  animation: aio-spin-inner 1.1s cubic-bezier(0.5, 0.15, 0.5, 0.85) infinite reverse;
}

/* Smaller inline variant (for buttons and inline text) */
.aio-spinner.aio-spinner-sm {
  width: 20px !important;
  height: 20px !important;
  border-width: 2px !important;
}

.aio-spinner.aio-spinner-sm::after {
  inset: 4px;
  border-width: 1px !important;
}

/* Centered loading message wrapper */
.aio-loading-message {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
}

.aio-loading-message .aio-spinner {
  flex-shrink: 0;
}
