/*
 * Core Creations responsive safety layer
 *
 * This file deliberately sits after the shared theme styles. It provides a
 * consistent mobile baseline for legacy and current modules without changing
 * their desktop presentation.
 */

:root {
  --app-mobile-hit-size: 2.75rem;
  --app-mobile-gutter: 0.75rem;
}

html {
  max-width: 100%;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body,
main,
#content,
.container,
.container-fluid,
.row,
.card,
.card-body,
.modal-content,
.modal-body,
.tab-content,
.tab-pane {
  min-width: 0;
}

img,
video,
iframe {
  max-width: 100%;
}

img,
video {
  height: auto;
}

canvas,
svg {
  max-width: 100%;
}

pre,
code,
.text-break,
.dropdown-item,
.modal-title {
  overflow-wrap: anywhere;
}

.table-responsive,
.app-responsive-table,
.app-mobile-scroll-region {
  max-width: 100%;
  overscroll-behavior-inline: contain;
  -webkit-overflow-scrolling: touch;
}

/* Decorative landing-page glows stay paint-contained so they cannot create a
   phantom horizontal document width outside the clipped card. */
.landing-page .hero-card {
  contain: paint;
}

/* Grid and flex children commonly need min-width: 0 before their own scroll
   regions can shrink inside a phone-sized viewport. */
:where(
  [class$="-grid"],
  [class*="-grid "],
  [class$="-layout"],
  [class*="-layout "],
  [class$="-shell"],
  [class*="-shell "]
) > * {
  min-width: 0;
}

@media (max-width: 767.98px) {
  :root {
    --app-mobile-gutter: 0.625rem;
  }

  body {
    min-width: 0;
    max-width: 100%;
  }

  main,
  #content,
  .container,
  .container-fluid {
    max-width: 100%;
  }

  .container,
  .container-fluid {
    padding-right: max(var(--app-mobile-gutter), env(safe-area-inset-right, 0px));
    padding-left: max(var(--app-mobile-gutter), env(safe-area-inset-left, 0px));
  }

  .row {
    --bs-gutter-x: 1rem;
  }

  /* Prevent iOS from zooming form fields and keep every control inside its
     parent, including old pages with fixed-width inputs. */
  :is(
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
    select,
    textarea,
    .form-control,
    .form-select,
    .input-group
  ) {
    min-width: 0;
    max-width: 100%;
    font-size: 1rem;
  }

  textarea {
    resize: vertical;
  }

  :is(.btn, .nav-link, .dropdown-item, .list-group-item-action) {
    min-height: var(--app-mobile-hit-size);
  }

  :is(.btn, button, a, input, select, textarea) {
    touch-action: manipulation;
  }

  /* Dense page actions stay in one swipeable row. JavaScript adds the same
     class to legacy action groups that contain several controls. */
  :is(.unified-page-actions, .app-mobile-action-strip) {
    display: flex;
    flex-wrap: nowrap !important;
    justify-content: flex-start !important;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: visible;
    padding: 0.125rem 0 0.3rem;
    scroll-snap-type: inline proximity;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  :is(.unified-page-actions, .app-mobile-action-strip)::-webkit-scrollbar {
    display: none;
  }

  :is(.unified-page-actions, .app-mobile-action-strip) > * {
    flex: 0 0 auto;
    margin-right: 0 !important;
    margin-left: 0 !important;
    scroll-snap-align: start;
  }

  :is(.unified-page-actions, .app-mobile-action-strip) .dropdown-menu {
    max-width: min(20rem, calc(100vw - 1rem));
  }

  .app-mobile-icon-only {
    width: var(--app-mobile-hit-size) !important;
    min-width: var(--app-mobile-hit-size) !important;
    max-width: var(--app-mobile-hit-size) !important;
    min-height: var(--app-mobile-hit-size) !important;
    padding-right: 0.55rem !important;
    padding-left: 0.55rem !important;
    gap: 0 !important;
  }

  .app-mobile-icon-only :is(.app-mobile-action-label, .action-label) {
    display: none !important;
  }

  .app-mobile-icon-only.dropdown-toggle::after {
    display: none;
  }

  /* Tabs and pagination remain one row and can be swiped instead of wrapping
     into tall, uneven stacks. */
  :is(.nav-tabs, .nav-pills, .pagination) {
    flex-wrap: nowrap;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  :is(.nav-tabs, .nav-pills, .pagination)::-webkit-scrollbar {
    display: none;
  }

  :is(.nav-tabs, .nav-pills) > *,
  .pagination > * {
    flex: 0 0 auto;
  }

  /* Legacy tables that are direct page/card children gain a local scroll
     region; page-level horizontal scrolling remains disabled. */
  :is(main, #content, .card-body, .modal-body, .tab-pane) > table.table:not(.dataTable) {
    display: block;
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
  }

  .table-responsive {
    overflow-x: auto;
  }

  /* Keep dialogs, menus and overlays fully inside the dynamic phone viewport. */
  .modal-dialog {
    width: auto;
    max-width: calc(100vw - 1rem);
    max-height: calc(100dvh - 1rem);
    margin: 0.5rem;
  }

  .modal-content {
    max-height: calc(100dvh - 1rem);
    overflow: hidden;
  }

  .modal-body {
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .modal-footer {
    flex-wrap: nowrap !important;
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  .modal-footer::-webkit-scrollbar {
    display: none;
  }

  .modal-footer > :is(.btn, button) {
    flex: 1 0 auto !important;
    width: auto !important;
    min-width: max-content;
  }

  .dropdown-menu,
  [role="menu"],
  .popover {
    max-width: calc(100vw - 1rem);
    max-height: calc(100dvh - 1rem);
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .offcanvas {
    --bs-offcanvas-width: min(22rem, calc(100vw - 1rem));
    max-width: calc(100vw - 1rem);
  }

  .toast,
  .app-alert-toast {
    right: max(0.5rem, env(safe-area-inset-right, 0px));
    left: max(0.5rem, env(safe-area-inset-left, 0px));
    width: auto;
    max-width: none;
  }

  /* Four dashboard calendar filters fit cleanly in one row rather than a 2x2
     button block. */
  .calendar-card .calendar-filters {
    flex-wrap: nowrap;
    justify-content: center;
    overflow-x: auto;
  }

  .calendar-card .calendar-filters .btn {
    flex: 1 0 auto;
    min-width: 3.65rem;
  }

  /* Business Expenses: the wide transaction table scrolls inside its card;
     it no longer forces the entire overview grid past the phone edge. */
  body.expenses-page :is(.expenses-overview-grid, .expenses-recent-panel, .expenses-surface, .expenses-table-wrap) {
    min-width: 0;
    max-width: 100%;
  }

  body.expenses-page .expenses-table-wrap {
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
  }

  body.expenses-page .expenses-surface-header {
    min-width: 0;
  }

  body.expenses-page .expenses-toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
  }

  body.expenses-page .expenses-toolbar button {
    min-width: 0;
    width: auto;
  }
}

@media (max-width: 374.98px) {
  :root {
    --app-mobile-gutter: 0.5rem;
  }

  .calendar-card .calendar-filters .btn {
    min-width: 3.4rem;
    padding-right: 0.45rem;
    padding-left: 0.45rem;
  }
}

@media (hover: none) and (pointer: coarse) {
  .card:hover {
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  :is(.unified-page-actions, .app-mobile-action-strip, .nav-tabs, .nav-pills, .pagination) {
    scroll-behavior: auto;
  }
}
