/* Accounting review fixes — shared mobile / modal / advisor / layout */

/* ---- View Voucher modal: scrollable on short / landscape viewports ---- */
#view-voucher-modal.modal,
#view-voucher-modal.acct-is-0339,
#view-voucher-modal.pv-view-voucher-modal {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  align-items: flex-start;
}
#view-voucher-modal .modal-dialog,
#view-voucher-modal .acct-is-0340 {
  max-height: calc(100vh - 1rem);
  margin: 0.5rem auto;
  overflow-y: auto;
}
#view-voucher-modal .modal-content {
  max-height: calc(100vh - 1rem);
}
#view-voucher-modal .modal-body,
#view-voucher-modal .acct-is-0345 {
  overflow-y: auto !important;
  max-height: min(70vh, calc(100vh - 8rem));
  -webkit-overflow-scrolling: touch;
}
@media (max-height: 500px) {
  #view-voucher-modal .modal-dialog,
  #view-voucher-modal .acct-is-0340 {
    max-height: 100vh;
    margin: 0.25rem auto;
  }
  #view-voucher-modal .modal-body,
  #view-voucher-modal .acct-is-0345 {
    max-height: calc(100vh - 6rem);
  }
}

/* ---- Add Note modal: fit short viewports ---- */
#bknote-modal.acct-is-0339 {
  align-items: flex-start;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
#bknote-modal .acct-is-0340 {
  max-height: min(92vh, 720px);
  overflow-y: auto;
  margin: 1rem auto;
}

/* ---- Loading indicator (Banking + lazy panels) ---- */
#banking .acct-panel-loading,
.content-section.active > .acct-panel-loading {
  min-height: 120px;
  overflow: visible;
  padding: 2rem 1rem;
  font-size: 1rem;
  font-weight: 600;
  color: #334155;
}
#banking .acct-panel-loading .fa-spinner,
.content-section.active > .acct-panel-loading .fa-spinner {
  display: none;
}
#banking .acct-panel-loading::before,
.content-section.active > .acct-panel-loading:not(:empty)::before {
  content: none;
}

/* ---- Financial Advisor: do not obscure content on mobile ---- */
@media (max-width: 1024px) {
  #ai-insights-widget.acct-is-0105 {
    position: fixed;
    left: 12px;
    right: 12px;
    bottom: 12px;
    width: auto;
    max-width: none;
    max-height: 42vh;
    border-radius: 14px;
    z-index: 700;
    transform: translateY(calc(100% - 52px));
    transition: transform 0.2s ease;
  }
  #ai-insights-widget.acct-is-0105.acct-advisor-expanded,
  #ai-insights-widget.acct-is-0105:focus-within {
    transform: translateY(0);
  }
  #ai-insights-widget.acct-is-0105 .acct-is-0106,
  #ai-insights-widget.acct-is-0105 .card-header,
  #ai-insights-widget.acct-is-0105 [class*="header"] {
    cursor: pointer;
  }
  .main-content,
  #main-content,
  .content-section.active {
    padding-bottom: 72px !important;
  }
  #dashboard #dashboard-outer-grid {
    padding-bottom: 88px;
  }
}

/* ---- Shared modal scroll / footer safety ---- */
.modal.show .modal-dialog,
.modal.fade.show .modal-dialog {
  max-width: min(960px, calc(100vw - 1rem));
  margin: 0.75rem auto;
  max-height: calc(100dvh - 1.5rem);
}
.modal.show .modal-dialog-scrollable,
.modal.fade.show .modal-dialog-scrollable,
.modal-dialog-scrollable {
  max-height: calc(100dvh - 1.5rem) !important;
  display: flex;
}
.modal-dialog-scrollable .modal-content {
  max-height: calc(100dvh - 1.5rem) !important;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-dialog-scrollable .modal-body {
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  flex: 1 1 auto;
  min-height: 0;
}
.modal-dialog-scrollable .modal-header,
.modal-dialog-scrollable .modal-footer {
  flex: 0 0 auto;
}
.modal-footer {
  flex-wrap: wrap;
  gap: 0.5rem;
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
}
.modal-footer .btn {
  min-height: 40px;
  white-space: normal;
}

/* Custom overlays used by Accounting (invoice/payment/bank/bookkeeping) */
.modal-overlay,
#payment-modal,
#create-invoice-modal,
#connect-bank-modal,
#transfer-modal,
#new-transfer-modal,
#bkp-modal,
#schedule-alerts-modal,
#acct-completion-modal,
[id$="-modal"].modal-overlay {
  align-items: flex-start !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  padding: 12px 12px 24px !important;
  box-sizing: border-box;
}
.modal-overlay > .modal-content,
#payment-modal > .modal-content,
#create-invoice-modal > .modal-content,
#bkp-modal .acct-is-0340,
.acct-is-0339 .acct-is-0340,
.acct-is-0340 {
  max-height: min(90dvh, calc(100dvh - 24px));
  overflow: hidden;
  display: flex;
  flex-direction: column;
  margin-top: 12px;
  margin-bottom: 24px;
}
.modal-overlay > .modal-content > *:not(.modal-header):not(.modal-footer),
#bkp-modal .acct-is-0340 > .modal-body,
.acct-is-0340 .modal-body,
.acct-is-0341 {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  max-height: none;
  flex: 1 1 auto;
  min-height: 0;
}

/* Prevent modal open bleed / flash */
.modal.fade:not(.show) {
  opacity: 0;
  pointer-events: none;
}
.modal.fade .modal-dialog {
  transform: none;
}

@media (max-width: 844px) {
  /* Portrait + landscape phones */
  .modal-footer .btn {
    flex: 1 1 calc(50% - 0.5rem);
    min-width: 7.5rem;
  }

  /* Sales Time Period icon padding */
  #sales-by-region .card-actions .btn i,
  #sales-trends .card-actions .btn i,
  #sales-by-region [class*="period"] i,
  #sales-trends [class*="period"] i,
  #sales-by-region .btn-group .btn,
  #sales-trends .btn-group .btn {
    padding-left: 0.85rem !important;
    padding-right: 0.85rem !important;
  }
  #sales-by-region .btn i,
  #sales-trends .btn i {
    margin-left: 0.15rem;
    margin-right: 0.35rem;
  }

  /* Credit Notes action icons stay horizontal (portrait + landscape).
     Real table is rendered by accounting-credit-notes-boot.js (.action-buttons). */
  #credit-notes .table-responsive,
  #credit-notes .table-wrap,
  #credit-notes #credit-notes-table,
  #credit-notes {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  #credit-notes td.acct-cn-actions,
  #credit-notes #credit-notes-table td:last-child {
    display: table-cell !important;
    width: 1%;
    min-width: 9.5rem;
    vertical-align: middle;
    white-space: nowrap !important;
  }
  #credit-notes .acct-cn-action-row,
  #credit-notes .action-buttons,
  #credit-notes td:last-child .action-buttons,
  #credit-notes .btn-group {
    display: inline-flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    white-space: nowrap !important;
    gap: 0.2rem;
    align-items: center;
    justify-content: flex-start;
    min-width: max-content;
    width: max-content;
    max-width: none;
  }
  #credit-notes .acct-cn-action-row .btn,
  #credit-notes .action-buttons .btn,
  #credit-notes td:last-child .btn {
    flex: 0 0 auto !important;
    width: auto !important;
    min-width: 2rem;
    padding: 0.15rem 0.35rem !important;
    line-height: 1.1;
  }

  /* Aging / Bookkeeping KPI cards stack in portrait */
  #aging-reports .row > [class*="col-"],
  #bookkeeping-overview .row > [class*="col-"],
  #bk-periods .row > [class*="col-"],
  #bookkeeping .row.g-3 > [class*="col-"],
  #aging-reports .stats-grid > *,
  #bookkeeping-overview .stats-grid > * {
    flex: 0 0 100%;
    max-width: 100%;
  }

  /* Bookkeeping submenu icon/label spacing (landscape phones included) */
  .accounting-sidebar .submenu a i,
  .accounting-sidebar .submenu a .fas,
  .accounting-sidebar .submenu a .far {
    margin-right: 0.65rem !important;
    width: 1.1rem;
    text-align: center;
  }

  /* Remove unintended blur flicker on submenu / loading */
  .accounting-sidebar .submenu,
  .accounting-sidebar .submenu a,
  #vendor-bills,
  #vendor-bills .submenu,
  #fixed-assets,
  #assets-register,
  #asset-disposal,
  #depreciation,
  #depreciation-books,
  .content-section .acct-panel-loading {
    filter: none !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }

  /* AP Automation search: icon + expandable on portrait */
  #ap-automation .d-flex.gap-2 {
    flex-wrap: wrap;
    align-items: center;
  }
  #ap-bills-search {
    order: 3;
    flex: 1 1 100%;
    min-width: 0;
    display: none;
  }
  #ap-automation.acct-search-open #ap-bills-search,
  #ap-bills-search:focus {
    display: block;
  }
  #ap-automation .acct-mobile-search-toggle {
    display: inline-flex !important;
  }
  #ap-automation .btn.btn-sm.btn-primary {
    padding-left: 0.65rem;
    padding-right: 0.65rem;
  }
  #ap-automation .btn.btn-sm.btn-primary {
    font-size: 0;
  }
  #ap-automation .btn.btn-sm.btn-primary i {
    font-size: 0.95rem;
  }

  /* Generic Refresh label → icon only on mobile */
  .content-section .btn .fa-sync-alt + *,
  .content-section button.btn .fa-sync-alt {
    margin-right: 0;
  }
  .content-section .btn:has(> .fa-sync-alt),
  .content-section .btn:has(> i.fa-sync-alt) {
    font-size: 0;
    padding-left: 0.7rem;
    padding-right: 0.7rem;
  }
  .content-section .btn:has(> .fa-sync-alt) > i,
  .content-section .btn:has(> i.fa-sync-alt) > i {
    font-size: 0.95rem;
  }
  #ai-insights-refresh-link {
    font-size: 0;
  }
  #ai-insights-refresh-link i {
    font-size: 0.9rem;
    margin: 0;
  }
}

@media (min-width: 845px) {
  #ap-automation .acct-mobile-search-toggle {
    display: none !important;
  }
}

/* Landscape phones: keep modals scrollable */
@media (max-height: 500px) and (orientation: landscape) {
  .modal.show .modal-dialog,
  .modal-dialog-scrollable {
    max-height: calc(100dvh - 0.75rem) !important;
    margin-top: 0.35rem;
    margin-bottom: 0.35rem;
  }
  .modal-dialog-scrollable .modal-content {
    max-height: calc(100dvh - 0.75rem) !important;
  }
  .modal-overlay,
  #bkp-modal,
  #payment-modal,
  #create-invoice-modal {
    align-items: flex-start !important;
  }
}

/* Mobile drawer backdrop affordance */
@media (max-width: 1024px) {
  body.acct-drawer-open::before {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.35);
    z-index: 9990;
  }
  #accountingSidebar.open {
    z-index: 10000;
  }
}
