/*
 * WPU E-Resources — Public Modal Viewport Safety Patch V36.2
 * Keeps Bootstrap and custom public dialogs fully readable inside the visible
 * browser viewport, including short screens, mobile devices, and soft keyboards.
 */
:root{
  --wpu-modal-viewport-height:100dvh;
  --wpu-modal-edge-gap:clamp(8px,2vh,20px);
  --wpu-modal-total-gap:clamp(16px,4vh,40px);
}

html body .modal{
  overscroll-behavior:contain;
}

/* Preserve the custom resource-preview artwork; it has its own safe rules below. */
html body .modal:not(.wpu-rp-reference-modal) .modal-dialog{
  max-height:calc(var(--wpu-modal-viewport-height,100dvh) - var(--wpu-modal-total-gap))!important;
  margin:var(--wpu-modal-edge-gap) auto!important;
  display:flex!important;
  align-items:stretch!important;
  min-height:0!important;
}
html body .modal:not(.wpu-rp-reference-modal) .modal-dialog.modal-dialog-centered{
  min-height:0!important;
  height:calc(var(--wpu-modal-viewport-height,100dvh) - var(--wpu-modal-total-gap))!important;
  align-items:center!important;
}
html body .modal:not(.wpu-rp-reference-modal) .modal-content{
  display:flex!important;
  flex-direction:column!important;
  width:100%!important;
  min-height:0!important;
  max-height:calc(var(--wpu-modal-viewport-height,100dvh) - var(--wpu-modal-total-gap))!important;
  overflow:hidden!important;
}
html body .modal:not(.wpu-rp-reference-modal) .modal-content > form,
html body .modal:not(.wpu-rp-reference-modal) form.modal-content{
  display:flex!important;
  flex-direction:column!important;
  min-height:0!important;
  max-height:100%!important;
  overflow:hidden!important;
}
html body .modal:not(.wpu-rp-reference-modal) .modal-header,
html body .modal:not(.wpu-rp-reference-modal) .modal-footer{
  flex:0 0 auto!important;
  position:relative;
  z-index:2;
}
html body .modal:not(.wpu-rp-reference-modal) .modal-body{
  flex:1 1 auto!important;
  min-height:0!important;
  overflow-x:hidden!important;
  overflow-y:auto!important;
  overscroll-behavior:contain!important;
  -webkit-overflow-scrolling:touch;
  scrollbar-gutter:stable;
  overflow-wrap:anywhere;
}
html body .modal:not(.wpu-rp-reference-modal) .modal-footer{
  display:flex;
  flex-wrap:wrap!important;
  gap:.625rem!important;
  background:#fff;
}
html body .modal:not(.wpu-rp-reference-modal) .modal-footer > *{
  margin:0!important;
  white-space:normal;
}

/* Access Notice: balanced two-column actions without clipped labels. */
html body #wpuAccessNoticeModal .modal-dialog{max-width:760px!important;}
html body #wpuAccessNoticeModal .modal-footer{
  display:grid!important;
  grid-template-columns:repeat(2,minmax(0,1fr));
  align-items:stretch;
}
html body #wpuAccessNoticeModal .modal-footer .btn{
  width:100%;
  min-width:0;
  min-height:48px;
  display:inline-flex;
  align-items:center;
  justify-content:center;
  line-height:1.25;
}

/* The custom first-visit dialog must also keep actions visible. */
html body .wpu-first-modal{
  overflow:hidden!important;
  padding:var(--wpu-modal-edge-gap)!important;
}
html body .wpu-first-modal-card{
  display:flex!important;
  flex-direction:column!important;
  min-height:0!important;
  max-height:calc(var(--wpu-modal-viewport-height,100dvh) - var(--wpu-modal-total-gap))!important;
  overflow:hidden!important;
}
html body .wpu-first-modal-head,
html body .wpu-first-modal-note,
html body .wpu-first-modal-actions,
html body .wpu-first-modal-topbar,
html body .wpu-first-modal-card > .alert{
  flex:0 0 auto;
}
html body .wpu-first-modal-body{
  flex:1 1 auto;
  min-height:0;
  overflow-x:hidden;
  overflow-y:auto;
  overscroll-behavior:contain;
  -webkit-overflow-scrolling:touch;
}
html body.wpu-custom-modal-open{overflow:hidden!important;}

/* Resource preview keeps its design but uses the actual visual viewport height. */
html body #wpuResourcePreviewModal.wpu-rp-reference-modal .modal-dialog{
  max-height:calc(var(--wpu-modal-viewport-height,100dvh) - 12px)!important;
}
html body #wpuResourcePreviewModal.wpu-rp-reference-modal .modal-body{
  max-height:calc(var(--wpu-modal-viewport-height,100dvh) - 12px)!important;
  overscroll-behavior:contain!important;
  -webkit-overflow-scrolling:touch;
}

@media(max-width:575.98px){
  :root{--wpu-modal-edge-gap:6px;--wpu-modal-total-gap:12px;}
  html body .modal:not(.wpu-rp-reference-modal){padding:0!important;}
  html body .modal:not(.wpu-rp-reference-modal) .modal-dialog{
    width:calc(100% - 12px)!important;
    max-width:none!important;
    margin:6px!important;
  }
  html body .modal:not(.wpu-rp-reference-modal) .modal-content{
    border-radius:18px!important;
  }
  html body .modal:not(.wpu-rp-reference-modal) .modal-header{
    padding:14px 15px!important;
  }
  html body .modal:not(.wpu-rp-reference-modal) .modal-body{
    padding:15px!important;
  }
  html body .modal:not(.wpu-rp-reference-modal) .modal-footer,
  html body #wpuAccessNoticeModal .modal-footer{
    display:grid!important;
    grid-template-columns:1fr!important;
    padding:12px 15px calc(12px + env(safe-area-inset-bottom))!important;
  }
  html body .modal:not(.wpu-rp-reference-modal) .modal-footer > .btn,
  html body .modal:not(.wpu-rp-reference-modal) .modal-footer > a.btn{
    width:100%!important;
    min-width:0!important;
  }
  html body .wpu-first-modal-head{padding:16px 15px 9px!important;}
  html body .wpu-first-modal-body{padding:8px 15px!important;}
  html body .wpu-first-modal-note{margin:10px 15px 0!important;}
  html body .wpu-first-modal-actions{padding:12px 15px calc(15px + env(safe-area-inset-bottom))!important;}
}

@media(max-height:520px){
  :root{--wpu-modal-edge-gap:4px;--wpu-modal-total-gap:8px;}
  html body .modal:not(.wpu-rp-reference-modal) .modal-dialog{margin:4px auto!important;}
  html body .modal:not(.wpu-rp-reference-modal) .modal-header{padding-top:10px!important;padding-bottom:10px!important;}
  html body .modal:not(.wpu-rp-reference-modal) .modal-body{padding-top:12px!important;padding-bottom:12px!important;}
  html body .modal:not(.wpu-rp-reference-modal) .modal-footer{padding-top:10px!important;padding-bottom:10px!important;}
}
