/* BillTrack - mobile layer (tablet and phone only). Navigation and layout follow the File 1 design:
 * a sticky, sideways-scrolling menu bar across the top, logo-only branding, tighter phone spacing.
 * Colours stay on File 2's dark theme.
 *
 * EVERY rule below sits inside a max-width: 991.98px media query, the same breakpoint the app
 * already uses to switch the sidebar into a drawer. Nothing here applies on desktop, so the
 * desktop design in style.css is untouched. Loaded after style.css on every page.
 *
 * Root cause of the broken menu (fixed in section 1): style.css line ~1405 gives .app-shell
 * "position: relative; z-index: 1". That makes the shell its own stacking context, so the
 * sidebar's z-index (1040) only counts INSIDE the shell. The backdrop (1035) and the sticky
 * topbar (1030) are outside it, so both paint on top of the open drawer and swallow taps.
 */

@media (max-width: 991.98px) {

  /* 1. Layers ----------------------------------------------------------------------------- */
  .app-shell { position: static; z-index: auto; display: block; min-height: auto; }
  .main-content { position: relative; z-index: 1; margin-left: 0; width: 100%; }
  .topbar { z-index: 1030; margin-left: 0; min-height: 62px; padding: 8px 14px; }

  /* 2. Menu bar: always visible, sticks to the top, scrolls sideways (File 1 design) --------- */
  .sidebar {
    position: sticky; top: 0; left: auto; right: auto; bottom: auto;
    z-index: 1040;
    width: 100%; height: auto; min-height: 0;
    padding: calc(8px + env(safe-area-inset-top, 0px)) calc(10px + env(safe-area-inset-right, 0px)) 8px calc(10px + env(safe-area-inset-left, 0px));
    display: flex; flex-direction: row; align-items: center; gap: 6px;
    overflow-x: auto; overflow-y: hidden; white-space: nowrap;
    transform: none !important;
    box-shadow: 0 5px 18px rgba(0, 0, 0, .35);
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  .sidebar-brand, .sidebar-footer, .sidebar-close-btn { display: none; }

  .sidebar .nav-item-link {
    flex: 0 0 auto;
    min-height: 44px;
    padding: 9px 12px;
    font-size: 12px;
    gap: 7px;
  }

  /* Make Calculator button shorter on phone UI */
  .sidebar .nav-item-link.calc-trigger {
    width: auto;
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .sidebar .nav-item-link i { font-size: 17px; }
  .sidebar .nav-item-link.active::before { display: none; }

  .sidebar-user {
    flex: 0 0 auto;
    margin: 0 0 0 4px;
    padding: 0 0 0 9px;
    border-top: 0;
    border-left: 1px solid rgba(255, 255, 255, .2);
    gap: 6px;
  }

  .sidebar-user-avatar { width: 30px; height: 30px; }
  .sidebar-user-name { display: none; }
  .sidebar-logout-btn { margin-left: 0; min-width: 44px; min-height: 44px; }

  html.sidebar-collapsed .sidebar { transform: none; }
  html.sidebar-collapsed .topbar,
  html.sidebar-collapsed .main-content { margin-left: 0; }

  /* The old slide-out menu pieces are not used any more. */
  .sidebar-toggle-btn, .sidebar-backdrop, .sidebar-backdrop.open, .bt-bottomnav { display: none !important; }

  /* 3. Top bar ---------------------------------------------------------------------------- */
  .topbar { padding-left: calc(14px + env(safe-area-inset-left, 0px)); padding-right: calc(14px + env(safe-area-inset-right, 0px)); }
  .topbar-brand { display: flex; margin: 0 auto 0 0; }
  .topbar-brand .brand-mark { width: 48px; height: 48px; }
  .topbar-icon-btn { min-width: 44px; min-height: 44px; border-radius: 12px; }

  /* 5. Touch behaviour and sizing -------------------------------------------------------- */
  a, button, .btn, .nav-item-link, .range-toggle button, [role="button"], label, select, summary { touch-action: manipulation; }
  a, button, .btn { -webkit-tap-highlight-color: rgba(105, 213, 161, .18); }
  .btn { min-height: 44px; }
  .btn-sm { min-height: 38px; }
  .btn-ghost-icon { min-width: 44px; min-height: 44px; }
  .form-control, .form-select { min-height: 48px; font-size: 16px; }       /* 16px stops iOS zoom-on-focus */
  textarea.form-control { min-height: 96px; }
  input[type="date"].form-control, input[type="month"].form-control { -webkit-appearance: none; appearance: none; }
  .form-check-input { width: 1.35em; height: 1.35em; }
  .form-label { margin-bottom: .35rem; }

  /* 6. Layout: no sideways scrolling ---------------------------------------------------- */
  .main-content { overflow-x: clip; max-width: 100%; }
  .main-content img, .main-content canvas, .main-content svg { max-width: 100%; }
  .main-content { padding-left: max(.85rem, env(safe-area-inset-left, 0px)); padding-right: max(.85rem, env(safe-area-inset-right, 0px)); }

  /* 7. Dashboard filters ---------------------------------------------------------------- */
  .dash-filters .range-toggle {
    display: flex; flex-wrap: nowrap; width: 100%;
    overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; scroll-snap-type: x proximity;
  }
  .dash-filters .range-toggle::-webkit-scrollbar { display: none; }
  .dash-filters .range-toggle button { flex: 0 0 auto; min-height: 40px; scroll-snap-align: start; white-space: nowrap; }
  .dash-month-nav { width: 100%; display: flex; align-items: center; gap: 8px; }
  .dash-month-nav .form-control { flex: 1 1 auto; min-width: 0; }
  .dash-nav-btn { min-width: 44px; min-height: 44px; }
  .dash-cat-wrap { width: 100%; }
  .dash-cat-btn { width: 100%; justify-content: space-between; }
  .dash-cat-menu { max-width: calc(100vw - 1.7rem); }
  .dash-custom .d-flex > div { flex: 1 1 100%; }
  .dash-custom .dash-arrow { display: none; }
  .dash-custom .btn { flex: 1 1 calc(50% - 4px); }

  /* 8. Cards, lists, charts ------------------------------------------------------------- */
  .card-surface { border-radius: 16px; }
  .metric-card .metric-value { font-size: 20px; overflow-wrap: anywhere; }
  .bill-row { flex-wrap: wrap; row-gap: 8px; padding-top: 14px; padding-bottom: 14px; }
  .bill-row .bill-actions { flex-wrap: wrap; gap: 6px; }
  canvas { max-width: 100%; height: auto; }

  /* 9. Modals become comfortable sheets ------------------------------------------------- */
  .modal-dialog { margin: .75rem; max-width: none; }
  .modal-content { max-height: calc(100vh - 1.5rem); max-height: calc(100dvh - 1.5rem); overflow-y: auto; -webkit-overflow-scrolling: touch; border-radius: 18px; }
  .modal-footer { padding-bottom: calc(.75rem + env(safe-area-inset-bottom, 0px)); }
  .modal-footer .btn { flex: 1 1 auto; }

  /* 10. Calculator window: keep inside the screen ------------ */
  .calc-widget { max-width: calc(100vw - 16px); max-height: calc(100vh - 140px); overflow-y: auto; }
}

/* Phones ---------------------------------------------------------------------------------- */
@media (max-width: 575.98px) {
  .page-heading { font-size: 22px; }
  .page-subtext { font-size: 13.5px; }
  .sidebar { padding-top: calc(7px + env(safe-area-inset-top, 0px)); padding-bottom: 7px; gap: 4px; }
  .sidebar .nav-item-link { padding: 8px 10px; font-size: 11.5px; }
  .sidebar-user { padding-left: 7px; }
  .main-content { padding-top: 1rem; padding-bottom: 2rem; }
  .finance-hero { grid-template-columns: minmax(0, 1fr); min-height: 0; padding: 22px 18px; margin-bottom: 16px; }
  .finance-hero h2 { font-size: 28px; }
  .money-stage { min-height: 170px; transform: scale(.88); transform-origin: top center; }
  .page-heading { overflow-wrap: anywhere; }
  .notif-panel { max-height: calc(100vh - 140px); overflow-y: auto; }
}

@media (max-width: 359.98px) {
  .metric-card .metric-value { font-size: 18px; }
}

/* Auth pages (login, sign-up, verify): safe areas and keyboard-friendly spacing ---------- */
@media (max-width: 991.98px) {
  .auth-shell { padding-left: max(.85rem, env(safe-area-inset-left, 0px)); padding-right: max(.85rem, env(safe-area-inset-right, 0px)); padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0px)); }
  .auth-card .btn { min-height: 48px; }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar, .sidebar-backdrop { transition: none !important; }
}


/* ===== Section order on phones and tablets only (desktop keeps its two-column layout) =====
   The two columns are "unwrapped" so every card becomes its own item of the row and can be ordered freely. */
@media (max-width: 991.98px) {
  .reorder-row > .reorder-col { display: contents !important; }
  .reorder-row .reorder-item {
    flex: 0 0 auto;
    width: calc(100% - var(--bs-gutter-x, 1rem));
    max-width: 100%;
    margin: var(--bs-gutter-y, 1rem) calc(var(--bs-gutter-x, 1rem) * .5) 0 !important;
  }
  /* Home: Spending overview, ... , Calendar (second to last), Recent bills (last) */
  .ro-spending { order: 1; }
  .ro-calendar { order: 2; }
  .ro-recent   { order: 3; }
  /* Forecast: Unusual bill warnings now sits where Forecast for each bill was, and the other way round */
  .ro-fc-chart   { order: 1; }
  .ro-fc-daily   { order: 2; }
  .ro-fc-unusual { order: 3; }
  .ro-fc-each    { order: 4; }
  .ro-fc-how     { order: 5; }
}


/* BillTrack supplied icons and Payment History mobile layout */
@media (max-width: 767.98px) {
  .nav-item-link .nav-image-icon { width: 22px; height: 22px; min-width: 22px; flex-basis: 22px; }
  .metric-icon .dashboard-image-icon { width: 23px; height: 23px; }
  .auth-visual-feature .auth-feature-icon { width: 38px; height: 38px; min-width: 38px; flex-basis: 38px; }
  .payment-history-row {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    column-gap: 12px; row-gap: 7px;
    align-items: start;
  }
  .payment-history-item { grid-column: 1 / -1; }
  .payment-history-amount { grid-column: 1; }
  .payment-history-paid { grid-column: 2; text-align: left; }
  .payment-history-due { grid-column: 1 / -1; white-space: normal; }
}
