/* =========================================================
   Consignment Loading - Responsive / Mobile Optimizations
   Breakpoints:
     - 480px: small phones
     - 576px: phones
     - 768px: tablets (enhances style.css 768px rules)
   ========================================================= */

/* =========================================================
   Loader: less intrusive, elegant loading experience
   ========================================================= */
#global-loader {
  background: rgba(255, 255, 255, 0.75) !important;
  backdrop-filter: blur(6px) !important;
  z-index: 999 !important;
}
#global-loader .spinner {
  border-color: rgba(29, 111, 224, 0.15) !important;
  border-top-color: var(--blue, #1d6fe0) !important;
  border-width: 3px !important;
  width: 36px !important;
  height: 36px !important;
}

/* =========================================================
   Stacking Context Fixes
   Typeahead MUST appear above Select2 dropdowns.
   ========================================================= */

/* Typeahead: prevent wrapper from creating a low stacking context.
   position:relative without z-index does NOT create stacking context,
   so the child dropdown's z-index competes at root level. */
.typeahead-wrapper {
    position: relative;
    z-index: auto;
}
.typeahead-dropdown {
    z-index: 1100 !important;
}

/* Select2 dropdown: always below typeahead suggestions */
.select2-container--open .select2-dropdown {
    z-index: 1055 !important;
}
.modal .select2-container--open .select2-dropdown {
    z-index: 1055 !important;
}

/* =========================================================
   Modals: consistent, mobile-friendly styling
   ========================================================= */
.custom-modal {
  border: none;
  border-radius: 12px;
}
.modal-header {
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  border-radius: 12px 12px 0 0;
}
.modal-footer {
  border-top: 1px solid var(--border);
  background: var(--bg);
  border-radius: 0 0 12px 12px;
}
.modal .form-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--navy-700);
  margin-bottom: 4px;
}
.modal .form-control {
  font-size: 14px;
  padding: 8px 10px;
  border-radius: 8px;
}
.modal .btn-sm {
  padding: 7px 16px;
  font-size: 13px;
  min-height: 36px;
}
.modal .input-with-btn .btn-icon {
  min-width: 38px;
  height: 38px;
}

/* Select2 inside modals */
.modal .select2-container--default .select2-selection--single {
  height: 38px;
  border-radius: 8px;
}
.modal .select2-container--default .select2-selection--single .select2-selection__rendered {
  line-height: 36px;
  font-size: 13px;
  padding-left: 10px;
}
.modal .select2-container--default .select2-selection--single .select2-selection__arrow {
  height: 36px;
}

/* ── Small phones (<= 480px) ── */
@media (max-width: 480px) {
  .app-header {
    padding: 0 6px;
  }
  .app-header .brand {
    margin-right: 4px;
    font-size: 11px;
    gap: 4px;
  }
  .app-header .brand .logo {
    width: 24px;
    height: 24px;
    font-size: 10px;
  }
  .app-header .brand span {
    display: none;
  }
  .app-header nav a {
    padding: 4px 5px;
    font-size: 10px;
  }
  main {
    padding: 6px 6px;
  }

  .data-table {
    min-width: 460px;
  }
  .data-table th,
  .data-table td {
    font-size: 9px;
    padding: 3px 4px;
  }

  .page-header h1 {
    font-size: 15px;
  }
  .card {
    padding: 8px;
    margin-bottom: 10px;
  }
  .card-title {
    font-size: 13px;
  }

  .d-flex.justify-content-between.align-items-center.mb-3.flex-wrap.gap-2 > .d-flex.align-items-center.gap-2 {
    flex-wrap: wrap;
    gap: 4px;
  }
  #fromDate, #toDate {
    font-size: 11px;
    padding: 3px 4px;
    min-width: 0;
    flex: 1 1 40%;
  }
  .d-flex.align-items-center.gap-2 span.text-muted {
    display: none;
  }

  .searchbar-wrap .searchbar-group .btn {
    padding: 3px 5px;
    font-size: 10px;
  }

  .pagination .page-link {
    padding: 3px 6px;
    font-size: 10px;
  }

  .modal-dialog {
    margin: 4px;
  }
  .modal-content {
    border-radius: 10px;
  }
  .modal-body {
    padding: 12px !important;
  }
  .modal-header, .modal-footer {
    padding: 8px 12px !important;
  }
  .modal .form-control {
    font-size: 13px;
    padding: 7px 8px;
    min-height: 36px;
  }
  .modal .btn-sm {
    padding: 6px 12px;
    font-size: 12px;
    min-height: 34px;
  }

  /* Select2 inside modal on very small screens */
  .modal .select2-container--default .select2-selection--single {
    height: 34px;
  }
  .modal .select2-container--default .select2-selection--single .select2-selection__rendered {
    line-height: 32px;
    font-size: 12px;
    padding-left: 8px;
    padding-right: 14px !important;
  }
  .modal .select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 32px;
  }

  /* Vehicle/driver select2 rendered text */
  .vehicles-section .select2-selection--single .select2-selection__rendered,
  .input-with-btn .select2-selection--single .select2-selection__rendered {
    font-size: 10px;
    padding-right: 14px !important;
    padding-left: 4px !important;
  }
  .vehicles-section .input-with-btn .btn-icon,
  .items-section .input-with-btn .btn-icon {
    min-width: 30px;
    height: 30px;
    font-size: 11px;
    padding: 2px 5px;
  }
  .vehicles-section .input-with-btn .select2-container--default .select2-selection--single,
  .items-section .input-with-btn .select2-container--default .select2-selection--single {
    height: 30px !important;
    font-size: 10px;
  }
  .vehicles-section .input-with-btn .select2-container--default .select2-selection--single .select2-selection__rendered,
  .items-section .input-with-btn .select2-container--default .select2-selection--single .select2-selection__rendered {
    line-height: 28px;
  }
  .vehicles-section .input-with-btn .select2-container--default .select2-selection--single .select2-selection__arrow,
  .items-section .input-with-btn .select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 28px;
  }
}

/* ── Phones (<= 576px) ── */
@media (max-width: 576px) {
  .app-header {
    padding: 0 10px;
  }
  .app-header .brand {
    margin-right: 8px;
    font-size: 13px;
    gap: 6px;
  }
  .app-header .brand .logo {
    width: 28px;
    height: 28px;
    font-size: 11px;
  }
  .app-header .brand span {
    display: none;
  }
  .app-header nav {
    gap: 2px;
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .app-header nav::-webkit-scrollbar {
    display: none;
  }
  .app-header nav a {
    padding: 6px 8px;
    font-size: 12px;
    white-space: nowrap;
  }

  main {
    padding: 10px 10px;
  }

  .page-header {
    gap: 8px;
  }
  .page-header h1 {
    font-size: 16px;
  }
  .page-header .btn-primary {
    width: 100%;
    justify-content: center;
  }

  .d-flex.justify-content-between.align-items-center.mb-3.flex-wrap.gap-2 {
    flex-direction: column;
    align-items: stretch !important;
    gap: 8px !important;
  }
  #fromDate, #toDate {
    flex: 1;
    font-size: 12px;
    padding: 4px 6px;
    min-width: 0;
    max-width: 100%;
  }
  .d-flex.align-items-center.gap-2 {
    width: 100%;
    flex-wrap: nowrap;
  }
  .d-flex.align-items-center.gap-2 span.text-muted {
    flex-shrink: 0;
  }
  #clearDates {
    padding: 4px 8px;
    font-size: 11px;
    flex-shrink: 0;
  }

  .card {
    padding: 12px 10px;
    margin-bottom: 12px;
  }

  .data-table {
    min-width: 580px;
  }
  .data-table th,
  .data-table td {
    font-size: 10px;
    padding: 4px 5px;
    white-space: nowrap;
  }
  .data-table td:nth-child(5),
  .data-table td:nth-child(6),
  .data-table td:nth-child(7) {
    white-space: normal;
    min-width: 100px;
  }

  /* ── Card-style table: transforms rows into stacked cards ── */
  .card .data-table,
  .settings-main-content .data-table {
    min-width: 0;
  }
  .card .data-table thead,
  .settings-main-content .data-table thead {
    display: none;
  }
  .card .data-table tbody,
  .card .data-table tr,
  .card .data-table td,
  .settings-main-content .data-table tbody,
  .settings-main-content .data-table tr,
  .settings-main-content .data-table td {
    display: block;
  }
  .card .data-table tr,
  .settings-main-content .data-table tr {
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px;
    margin-bottom: 12px;
    background: var(--surface);
    box-shadow: var(--shadow);
  }
  .card .data-table td,
  .settings-main-content .data-table td {
    border: none;
    padding: 5px 0;
    white-space: normal;
    font-size: 12px;
    text-align: left;
    width: auto !important;
    min-width: 0 !important;
  }
  .card .data-table td::before,
  .settings-main-content .data-table td::before {
    content: attr(data-label);
    display: inline-block;
    font-weight: 600;
    color: var(--navy-700);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    margin-right: 6px;
    min-width: 70px;
  }
  .card .data-table td:last-child::before,
  .settings-main-content .data-table td:last-child::before {
    display: none;
  }
  .card .data-table td:last-child,
  .settings-main-content .data-table td:last-child {
    padding-top: 8px;
    border-top: 1px solid var(--border);
    margin-top: 6px;
  }
  .card .data-table td[colspan],
  .settings-main-content .data-table td[colspan] {
    text-align: center;
    padding: 16px;
  }
  .card .data-table td[colspan]::before,
  .settings-main-content .data-table td[colspan]::before {
    display: none;
  }
  .card .data-table .d-flex.justify-content-center,
  .settings-main-content .data-table .d-flex.justify-content-center {
    justify-content: flex-start !important;
  }
  .card .data-table .dropdown-menu,
  .settings-main-content .data-table .dropdown-menu {
    left: 0;
    right: auto;
  }

  .form-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  .form-group.horizontal {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }
  .form-group.horizontal label {
    width: 100%;
    font-size: 11px;
  }
  .form-control {
    font-size: 13px;
  }
  .btn {
    font-size: 12px;
    padding: 6px 10px;
  }
  .input-with-btn .form-control,
  .input-with-btn .select2-container--default .select2-selection--single {
    height: 34px !important;
    font-size: 12px;
  }
  .input-with-btn .btn-icon {
    height: 34px;
    font-size: 12px;
    padding: 4px 8px;
    min-width: 34px;
  }

  .two-column-layout {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  .left-column, .right-column {
    padding: 10px;
  }

  .loading-page .loading-card {
    padding: 12px;
  }
  .loading-page .loading-actions {
    flex-direction: column;
    gap: 8px;
  }
  .loading-page .loading-actions .btn {
    width: 100%;
    justify-content: center;
    min-height: 42px;
    min-width: 0;
  }

  .date-inline {
    flex-direction: column;
    gap: 6px;
  }
  .date-inline input {
    width: 100%;
  }

  .searchbar-wrap .searchbar-group {
    max-width: 100%;
  }
  .searchbar-wrap .searchbar-group .btn {
    padding: 4px 8px;
    font-size: 11px;
    min-width: auto;
  }
  .searchbar-wrap .searchbar-group #searchBtn {
    padding: 4px 8px;
  }
  .searchbar-wrap .form-control {
    font-size: 13px;
  }

  .settings-wrapper {
    flex-direction: column;
    gap: 10px;
  }
  .secondary-sidebar {
    width: 100%;
    position: static;
    padding: 8px;
    overflow: hidden;
  }
  .secondary-sidebar .sidebar-header h5 {
    font-size: 13px;
    margin-bottom: 6px;
  }
  .secondary-sidebar .nav {
    flex-direction: row !important;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 4px;
    padding-bottom: 4px;
  }
  .secondary-sidebar .nav::-webkit-scrollbar {
    display: none;
  }
  .secondary-sidebar .nav-item {
    flex-shrink: 0;
  }
  .secondary-sidebar .nav-link {
    padding: 6px 12px;
    font-size: 12px;
    white-space: nowrap;
    margin-bottom: 0;
    border: 1px solid var(--border);
    background: var(--bg);
  }
  .secondary-sidebar .nav-link.active {
    background: var(--blue);
    color: white;
    border-color: var(--blue);
  }
  .secondary-sidebar .nav-link:hover:not(.active) {
    background: var(--blue-50);
    border-color: var(--blue);
  }
  .settings-main-content {
    padding: 10px;
  }

  .pagination {
    flex-wrap: wrap;
    gap: 2px;
  }
  .page-link {
    padding: 4px 7px;
    font-size: 11px;
  }
  #paginationWrapper {
    flex-direction: column;
    gap: 8px;
    align-items: flex-start !important;
  }
  #paginationWrapper small {
    font-size: 11px;
  }

  .modal-dialog {
    margin: 8px;
  }
  .modal-content {
    border-radius: 12px;
  }
  .modal-body {
    padding: 14px !important;
  }
  .modal-header {
    padding: 10px 14px !important;
    border-radius: 12px 12px 0 0;
  }
  .modal-footer {
    padding: 10px 14px !important;
    border-radius: 0 0 12px 12px;
  }
  .modal-title {
    font-size: 15px;
  }
  .modal .form-control {
    font-size: 14px;
    padding: 9px 10px;
    min-height: 40px;
  }
  .modal .btn-sm {
    padding: 8px 16px;
    font-size: 13px;
    min-height: 38px;
  }
  .modal .form-label {
    font-size: 12px;
  }

  /* Modal select2 consistency */
  .modal .select2-container--default .select2-selection--single {
    height: 40px;
  }
  .modal .select2-container--default .select2-selection--single .select2-selection__rendered {
    line-height: 38px;
    font-size: 13px;
    padding-left: 10px;
  }
  .modal .select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 38px;
  }
  .modal .input-with-btn {
    flex-wrap: nowrap;
  }
  .modal .input-with-btn .btn-icon {
    height: 40px;
    min-width: 40px;
  }

  .typeahead-wrapper .typeahead-input {
    font-size: 13px;
  }

  .items-list {
    max-height: 200px;
  }
  .item-row {
    grid-template-columns: 1fr;
    gap: 6px;
    padding: 8px;
  }
  .item-row .remove-row,
  .item-row .btn {
    grid-column: 1;
    justify-self: stretch;
    width: 100%;
  }
  .items-section .data-table, .vehicles-section .data-table {
    min-width: 320px;
  }

  .right-actions {
    position: static;
    box-shadow: none;
    border-top: 1px solid var(--border);
    margin-top: 12px;
    padding-top: 12px;
  }
  .right-actions .btn-primary {
    width: 100%;
  }
  .right-actions.loading-actions .btn {
    flex: none;
  }

  /* Loading page: section cards thinner border on mobile */
  .loading-page .left-column.loading-card,
  .loading-page .items-section.loading-card,
  .loading-page .vehicles-section.loading-card {
    border-left-width: 3px;
    padding: 14px;
  }
  /* Prevent double padding from .right-column wrapper + child sections */
  .loading-page .right-column {
    padding: 0;
    background: transparent;
    border: none;
  }
  /* Restore horizontal padding for actions now that .right-column has none */
  .loading-page .right-actions {
    padding-left: 14px;
    padding-right: 14px;
  }
  .loading-page .loading-card {
    margin-bottom: 14px;
  }
  .loading-page .section-heading {
    font-size: 15px;
  }

  /* Loading page: bigger touch targets for vehicle table remove btn */
  .loading-page .data-table .remove-row {
    min-width: 44px;
    min-height: 44px;
    font-size: 16px;
    padding: 8px 14px;
  }
  .loading-page .data-table td {
    vertical-align: middle;
  }

  /* Loading page: form elements full-width comfortable spacing */
  .loading-page .form-group.horizontal {
    margin-bottom: 14px;
  }
  .loading-page .form-group.horizontal label {
    font-size: 13px;
    margin-bottom: 4px;
  }
  .loading-page .form-control,
  .loading-page .input-with-btn .form-control {
    min-height: 44px;
    font-size: 15px;
  }
  .loading-page .input-with-btn .btn-icon {
    height: 44px;
    min-width: 44px;
    font-size: 18px;
  }

  /* Loading page: add buttons full width */
  #addItemBtn,
  #addVehicleBtn {
    min-height: 46px;
    font-size: 15px;
  }

  /* Loading page: action buttons stacked */
  .loading-page .loading-actions {
    flex-direction: column;
    gap: 10px;
  }
  .loading-page .loading-actions .btn {
    min-height: 50px;
    font-size: 16px;
  }

  /* Loading page: total box */
  .loading-page .vehicle-total-section {
    padding: 12px;
  }
  .loading-page .total-row {
    font-size: 14px;
  }
  .loading-page .total-row strong {
    font-size: 16px;
  }

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

  /* Select2 selected text: show as much as possible */
  .select2-selection--single .select2-selection__rendered {
    padding-right: 16px !important;
    padding-left: 6px !important;
    font-size: 11px;
  }
  .select2-container--default .select2-selection--single {
    min-height: 32px;
  }
  .select2-container--default .select2-selection--single .select2-selection__rendered {
    line-height: 30px;
  }
  .select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 30px;
  }

  /* Vehicle/driver select2 inside input-with-btn: ensure text visible */
  .vehicles-section .input-with-btn .select2-container--default .select2-selection--single,
  .items-section .input-with-btn .select2-container--default .select2-selection--single {
    height: 32px !important;
    font-size: 11px;
    border-radius: 6px 0 0 6px !important;
  }
  .vehicles-section .input-with-btn .select2-container--default .select2-selection--single .select2-selection__rendered,
  .items-section .input-with-btn .select2-container--default .select2-selection--single .select2-selection__rendered {
    line-height: 30px;
    font-size: 11px;
    padding-right: 14px !important;
    padding-left: 6px !important;
  }
  .vehicles-section .input-with-btn .select2-container--default .select2-selection--single .select2-selection__arrow,
  .items-section .input-with-btn .select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 30px;
  }
  .vehicles-section .input-with-btn .btn-icon,
  .items-section .input-with-btn .btn-icon {
    height: 32px;
    min-width: 32px;
    font-size: 12px;
    padding: 3px 7px;
  }

  /* Dashboard table: improve horizontal scroll visibility */
  .table-wrapper {
    margin: 0 -10px;
    padding: 0 10px 60px;
    width: calc(100% + 20px);
    -webkit-overflow-scrolling: touch;
  }
  .data-table th:last-child,
  .data-table td:last-child {
    width: 80px;
  }

  .swal2-popup {
    margin: 10px;
    padding: 16px;
    font-size: 13px;
  }

  .app-footer {
    display: none;
  }
  body {
    padding-bottom: 0;
    overflow-x: hidden;
  }

  .vehicles-section .table-wrapper,
  .items-section .table-wrapper {
    max-height: none;
  }

  /* Unit dropdown: responsive touch target */
  .loading-page .data-table select.unit {
    width: 100%;
    min-height: 44px;
    font-size: 15px;
    padding: 6px 10px;
    border-radius: 6px;
    border: 1.5px solid var(--border, #e2e8f0);
    background: var(--surface, #fff);
    cursor: pointer;
  }

  /* Loading page: force layout collapse on mobile */
  .loading-page .two-column-layout {
    grid-template-columns: 1fr !important;
  }
  .loading-page .form-group.horizontal {
    flex-direction: column !important;
    align-items: stretch !important;
  }
  /* Remove artificial min-widths that cause table overflow */
  .loading-page .vehicles-section .data-table td:first-child {
    min-width: 0;
  }
  .loading-page .data-table td:nth-child(1),
  .loading-page .data-table td:nth-child(2),
  .loading-page .data-table td:nth-child(3) {
    min-width: 0;
  }
  /* Constrain table wrappers to prevent overflow */
  .loading-page .table-wrapper {
    width: 100% !important;
    margin: 0 !important;
    overflow-x: auto !important;
  }

  .vehicle-total-section {
    padding: 8px;
  }
  .total-row {
    font-size: 12px;
  }
}

/* ── Tablets (<= 768px): enhance style.css existing rules ── */
@media (max-width: 768px) {
  .settings-wrapper {
    flex-direction: column;
    gap: 12px;
  }
  .secondary-sidebar {
    width: 100%;
    position: static;
    padding: 12px;
    overflow: hidden;
  }
  .secondary-sidebar .sidebar-header h5 {
    font-size: 14px;
    margin-bottom: 8px;
  }
  .secondary-sidebar .sidebar-header {
    width: 100%;
  }
  .secondary-sidebar .nav {
    flex-direction: row !important;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 6px;
    padding-bottom: 6px;
  }
  .secondary-sidebar .nav::-webkit-scrollbar {
    display: none;
  }
  .secondary-sidebar .nav-item {
    flex-shrink: 0;
  }
  .secondary-sidebar .nav-link {
    padding: 8px 16px;
    font-size: 13px;
    margin-bottom: 0;
    white-space: nowrap;
    border: 1px solid var(--border);
    background: var(--bg);
    border-radius: 20px;
  }
  .secondary-sidebar .nav-link.active {
    background: var(--blue);
    color: white;
    border-color: var(--blue);
  }
  .secondary-sidebar .nav-link:hover:not(.active) {
    background: var(--blue-50);
    border-color: var(--blue);
  }
  .settings-main-content {
    padding: 14px;
  }

  .page-header.d-flex.justify-content-between.align-items-center.mb-4 {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 10px;
  }
  .page-header.d-flex.justify-content-between.align-items-center.mb-4 .btn-primary {
    width: 100%;
    justify-content: center;
  }

  /* Loading page: prevent table overflow at tablet sizes too */
  .loading-page .vehicles-section .data-table td:first-child {
    min-width: 0;
  }
  .loading-page .data-table td:nth-child(1),
  .loading-page .data-table td:nth-child(2),
  .loading-page .data-table td:nth-child(3) {
    min-width: 0;
  }
  .loading-page .table-wrapper {
    width: 100% !important;
    margin: 0 !important;
    overflow-x: auto !important;
  }
  body {
    overflow-x: hidden;
  }
}

/* ── Print improvements ── */
@media print {
  .app-header, .app-footer, .no-print, .right-actions, .btn {
    display: none !important;
  }
  main {
    padding: 0 !important;
  }
  body {
    padding: 0 !important;
  }
}
