/* =============================================================================
   Casa Vergara — Responsive modals & pop-ups
   Makes every modal, toast, dropdown and lightbox fit any device: small phones
   (320px), foldables, phones in landscape, tablets, laptops and big monitors.
   Loaded LAST on every page. Delete this file (+ its <link>/<script> tags) to
   go back to the previous behaviour.

   Works with modal-responsive.js, which keeps --cvr-vh in sync with the *visible*
   viewport so a modal is never hidden behind the on-screen keyboard or the
   mobile browser toolbar.
   ========================================================================== */

:root {
  --cvr-vh: 100vh;                       /* fallback, replaced by JS / dvh below */
  --cvr-gap: 1rem;                       /* space between a modal and the screen edge */
  --cvr-safe-t: env(safe-area-inset-top, 0px);
  --cvr-safe-r: env(safe-area-inset-right, 0px);
  --cvr-safe-b: env(safe-area-inset-bottom, 0px);
  --cvr-safe-l: env(safe-area-inset-left, 0px);
}
@supports (height: 100dvh) {
  :root { --cvr-vh: 100dvh; }
}

/* Stop the page behind a modal from scrolling / bouncing */
body.modal-open { overscroll-behavior: none; }
.modal { overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }

/* ---------- Base: every modal is a flex column that never exceeds the screen - */
html body .modal-dialog {
  width: auto;
  margin: var(--cvr-gap) auto;
  max-width: min(var(--bs-modal-width, 500px), calc(100vw - (var(--cvr-gap) * 2) - var(--cvr-safe-l) - var(--cvr-safe-r)));
}
html body .modal-dialog.modal-lg  { --bs-modal-width: 800px; }
html body .modal-dialog.modal-xl  { --bs-modal-width: 1140px; }
html body .modal-dialog.modal-sm  { --bs-modal-width: 340px; }

html body .modal:not(.mosaic-lightbox):not(.about-lightbox) .modal-content {
  display: flex;
  flex-direction: column;
  max-height: calc(var(--cvr-vh) - (var(--cvr-gap) * 2) - var(--cvr-safe-t) - var(--cvr-safe-b));
  min-width: 0;
}
html body .modal .modal-header,
html body .modal .modal-footer { flex: 0 0 auto; }
html body .modal .modal-body {
  flex: 1 1 auto;
  min-height: 0;                         /* lets the body shrink and scroll inside */
  overflow-y: auto;
  overflow-x: hidden;
  overflow-wrap: anywhere;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
/* Bootstrap's scrollable dialogs use height:100% — keep them inside our limit */
html body .modal-dialog-scrollable { height: auto; max-height: none; }
html body .modal-dialog-scrollable .modal-content { max-height: calc(var(--cvr-vh) - (var(--cvr-gap) * 2) - var(--cvr-safe-t) - var(--cvr-safe-b)); }
html body .modal-dialog-centered { min-height: calc(var(--cvr-vh) - (var(--cvr-gap) * 2)); }

/* Things that overflow sideways inside a modal scroll on their own */
html body .modal .modal-body .table-responsive,
html body .modal .modal-body .cvr-table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
html body .modal .modal-body table:not(.table-responsive table) { max-width: 100%; }
html body .modal .modal-body img,
html body .modal .modal-body video,
html body .modal .modal-body iframe,
html body .modal .modal-body canvas { max-width: 100%; height: auto; }
html body .modal .modal-body pre { white-space: pre-wrap; word-break: break-word; }
html body .modal .modal-body .row { --bs-gutter-x: 1rem; }

/* Long titles wrap or truncate instead of pushing the close button off-screen */
html body .modal .modal-title { min-width: 0; overflow-wrap: anywhere; }
html body .modal .modal-header .btn-close { flex: 0 0 auto; }

/* Inputs: 16px+ on touch screens so iOS Safari doesn't zoom the page on focus */
@media (pointer: coarse) {
  html body .modal .form-control,
  html body .modal .form-select,
  html body .modal textarea,
  html body .modal input:not([type="checkbox"]):not([type="radio"]):not([type="range"]) { font-size: max(16px, 1rem); }
  html body .modal .btn { min-height: 44px; }
  html body .modal .btn-close { min-width: 40px; min-height: 40px; }
}

/* ---------- Large tablets / small laptops (≤ 991px) ------------------------- */
@media (max-width: 991.98px) {
  html body .modal-dialog.modal-xl,
  html body .modal-dialog.modal-lg { --bs-modal-width: 92vw; }
}

/* ---------- Tablets portrait & big phones (≤ 767px) ------------------------- */
@media (max-width: 767.98px) {
  :root { --cvr-gap: 0.75rem; }
  html body .modal-dialog.modal-xl,
  html body .modal-dialog.modal-lg,
  html body .modal-dialog { --bs-modal-width: 100vw; }
  /* two-column forms inside modals stack nicely */
  html body .modal .modal-body .row > [class*="col-md-"],
  html body .modal .modal-body .row > [class*="col-lg-"] { width: 100%; flex: 0 0 100%; }
}

/* ---------- Phones (≤ 575px): bottom-sheet style ----------------------------- */
@media (max-width: 575.98px) {
  :root { --cvr-gap: 0.5rem; }

  html body .modal-dialog,
  html body .modal-dialog.modal-sm,
  html body .modal-dialog.modal-lg,
  html body .modal-dialog.modal-xl {
    max-width: none;
    width: auto;
    margin: var(--cvr-gap) calc(var(--cvr-gap) + var(--cvr-safe-r)) var(--cvr-gap) calc(var(--cvr-gap) + var(--cvr-safe-l));
  }
  /* dialogs sit at the bottom edge like a native sheet, easy to reach with a thumb */
  html body .modal-dialog:not(.modal-fullscreen) {
    display: flex;
    align-items: flex-end;
    min-height: calc(var(--cvr-vh) - (var(--cvr-gap) * 2));
  }
  html body .modal-dialog:not(.modal-fullscreen) .modal-content {
    width: 100%;
    border-radius: 1.35rem !important;
    margin-bottom: var(--cvr-safe-b);
  }
  /* compact confirm / logout dialogs stay centred, they look better that way */
  html body .modal.cvr-modal--confirm .modal-dialog,
  html body .modal .modal-dialog.modal-sm { align-items: center; }

  html body .modal:not(.mosaic-lightbox):not(.about-lightbox) .modal-header { padding: 0.95rem 1rem 0.8rem; gap: 0.6rem; }
  html body .modal .modal-title { font-size: 1.08rem; gap: 0.55rem; }
  html body .modal .modal-title > i.bi:first-child { width: 2.1rem; height: 2.1rem; font-size: 0.95rem; border-radius: 0.7rem; }
  html body .modal .cvr-modal-icon { width: 2.3rem; height: 2.3rem; font-size: 1.05rem; }
  html body .modal:not(.mosaic-lightbox):not(.about-lightbox) .modal-body { padding: 1rem; }
  html body .modal-footer {
    flex-wrap: wrap;
    gap: 0.5rem;
    padding: 0.75rem 1rem calc(0.75rem + var(--cvr-safe-b) * 0.5);
    position: sticky;
    bottom: 0;
    background: var(--cvm-surface, var(--bs-body-bg, #fff));
  }
  html body .modal-footer > * { margin: 0; }
  html body .modal-footer > .btn,
  html body .modal-footer > button,
  html body .modal-footer > a.btn { flex: 1 1 calc(50% - 0.5rem); min-width: 0; }
  html body .modal-footer > .me-auto,
  html body .modal-footer > .w-100,
  html body .modal-footer > div { flex: 1 1 100%; }

  /* real full-screen modals keep the whole screen but respect notches */
  html body .modal-fullscreen .modal-content {
    border-radius: 0 !important;
    max-height: none;
    height: var(--cvr-vh);
    padding-top: var(--cvr-safe-t);
  }
  .cvr-modal--confirm .modal-body { padding: 1.5rem 1.15rem 1rem; }
}

/* ---------- Very small phones (≤ 359px, e.g. Galaxy Fold cover screen) ------- */
@media (max-width: 359.98px) {
  html body .modal-footer > .btn,
  html body .modal-footer > button,
  html body .modal-footer > a.btn { flex: 1 1 100%; }
  html body .modal .modal-title { font-size: 1rem; }
  html body .modal:not(.mosaic-lightbox):not(.about-lightbox) .modal-body { padding: 0.85rem; }
}

/* ---------- Phones / small tablets in LANDSCAPE (short screens) -------------- */
@media (max-height: 520px) and (orientation: landscape) {
  :root { --cvr-gap: 0.4rem; }
  html body .modal-dialog:not(.modal-fullscreen) { align-items: center; margin-left: auto; margin-right: auto; max-width: min(var(--bs-modal-width, 700px), 94vw); }
  html body .modal:not(.mosaic-lightbox):not(.about-lightbox) .modal-header { padding: 0.55rem 0.9rem; }
  html body .modal:not(.mosaic-lightbox):not(.about-lightbox) .modal-body { padding: 0.7rem 0.9rem; }
  html body .modal-footer { padding: 0.5rem 0.9rem; position: static; flex-wrap: nowrap; }
  html body .modal-footer > .btn { flex: 0 1 auto; }
  html body .modal .modal-title { font-size: 1rem; }
  html body .modal .modal-title > i.bi:first-child,
  html body .modal .cvr-modal-icon { display: none; }
  .cvr-modal-hero { display: none; }
}

/* ---------- Big monitors: don't let modals look lost ------------------------- */
@media (min-width: 1600px) {
  html body .modal-dialog.modal-xl { --bs-modal-width: 1240px; }
}

/* ---------- Image lightboxes -------------------------------------------------- */
html body .mosaic-lightbox .modal-dialog,
html body .about-lightbox .modal-dialog { max-width: min(1100px, 100vw - 1rem); margin-left: auto; margin-right: auto; }
html body .mosaic-lightbox img,
html body .about-lightbox img,
.mosaic-lightbox-img { max-width: 100%; max-height: calc(var(--cvr-vh) - 8rem); width: auto; height: auto; object-fit: contain; }
.cvr-lightbox-close { top: calc(0.5rem + var(--cvr-safe-t)); right: calc(0.5rem + var(--cvr-safe-r)); }

/* ---------- Toasts / notification pop-ups ------------------------------------
   Always top-right, directly UNDER the page header (admin top bar or site
   header) so they never cover the logo, menu or notification buttons — same for
   guest, customer, staff and admin. --cvr-toast-top is kept up to date by
   modal-responsive.js (falls back to 5rem). */
html body .cvr-toast-container {
  position: fixed;
  top: calc(var(--cvr-toast-top, 5rem) + var(--cvr-safe-t));
  right: calc(1rem + var(--cvr-safe-r));
  left: auto;
  bottom: auto;
  width: min(24rem, calc(100vw - 2rem - var(--cvr-safe-l) - var(--cvr-safe-r)));
  max-width: none;
  padding: 0;
  z-index: 2000;
  pointer-events: none;
}
html body .cvr-toast-container .notification-toast,
html body .cvr-toast-container .cvr-toast { width: 100%; max-width: 100%; margin-left: 0; margin-right: 0; }
@media (max-width: 575.98px) {
  html body .cvr-toast-container {
    left: calc(0.6rem + var(--cvr-safe-l));
    right: calc(0.6rem + var(--cvr-safe-r));
    width: auto;
  }
}

/* ---------- Dropdowns, notification panel, popovers --------------------------- */
html body .dropdown-menu { max-width: calc(100vw - 1rem); overflow-wrap: anywhere; }
html body .nav-notif-menu,
html body .nav-profile-menu { max-width: min(24rem, calc(100vw - 1rem)); }
html body .nav-notif-list { max-height: min(60vh, calc(var(--cvr-vh) - 12rem)); overflow-y: auto; -webkit-overflow-scrolling: touch; }
@media (max-width: 575.98px) {
  /* the notification panel becomes a clean full-width card under the header */
  html body .nav-notif-menu.show,
  html body .nav-notif-dropdown .dropdown-menu.show {
    position: fixed !important;
    top: calc(4.25rem + var(--cvr-safe-t)) !important;
    left: 0.5rem !important;
    right: 0.5rem !important;
    width: auto !important;
    max-width: none;
    transform: none !important;
    inset-inline: 0.5rem !important;
  }
}
html body .popover { max-width: min(276px, calc(100vw - 1rem)); }
html body .tooltip { max-width: calc(100vw - 1rem); }

/* ---------- Offcanvas / side panels ------------------------------------------- */
html body .offcanvas { max-width: 100vw; padding-bottom: var(--cvr-safe-b); }

/* ---------- Floating live-chat window ------------------------------------------ */
@media (max-width: 575.98px) {
  .cvr-live-chat-dock .cvr-chat-panel {
    width: calc(100vw - 1rem) !important;
    max-width: none !important;
    right: 0.5rem !important;
    left: 0.5rem !important;
    max-height: calc(var(--cvr-vh) - 5rem) !important;
  }
}

/* ---------- Dark mode: keep sticky footers matching the card ------------------- */
html[data-theme="dark"] body .modal-footer { background: var(--cvm-surface, #15212c); }

/* ---------- Respect "reduce motion" ---------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  html body .modal.fade .modal-dialog { transition: none !important; }
}
