/* ============================================================
   AFEMAI DYNAMICS DIASPORA — GLOBAL RESPONSIVE LAYER
   ------------------------------------------------------------
   Loaded after Bootstrap and before each layout's inline <style>,
   so page-specific tuning still wins, while this layer replaces
   Bootstrap's fixed type/spacing scale with a fluid one.

   Everything here scales with the viewport using clamp(), so the
   UI reads correctly from 320px phones through 4K desktops with
   no per-breakpoint duplication.
   ============================================================ */

/* ------------------------------------------------------------
   1. FLUID SCALE TOKENS
   ------------------------------------------------------------
   clamp(MIN, PREFERRED, MAX):
     MIN  — never smaller than this (small phones stay legible)
     MAX  — never larger than this (large desktops stay sane)
   ------------------------------------------------------------ */
:root {
    /* Body / UI text */
    --fs-3xs: clamp(0.62rem, 0.58rem + 0.20vw, 0.72rem);
    --fs-2xs: clamp(0.68rem, 0.64rem + 0.22vw, 0.78rem);
    --fs-xs: clamp(0.74rem, 0.70rem + 0.25vw, 0.84rem);
    --fs-sm: clamp(0.82rem, 0.78rem + 0.25vw, 0.92rem);
    --fs-base: clamp(0.90rem, 0.86rem + 0.28vw, 1.02rem);
    --fs-md: clamp(0.98rem, 0.92rem + 0.32vw, 1.12rem);
    --fs-lg: clamp(1.06rem, 0.98rem + 0.42vw, 1.26rem);

    /* Headings */
    --fs-h6: clamp(0.90rem, 0.84rem + 0.30vw, 1.02rem);
    --fs-h5: clamp(1.00rem, 0.92rem + 0.40vw, 1.20rem);
    --fs-h4: clamp(1.12rem, 1.00rem + 0.58vw, 1.45rem);
    --fs-h3: clamp(1.28rem, 1.10rem + 0.85vw, 1.80rem);
    --fs-h2: clamp(1.50rem, 1.20rem + 1.40vw, 2.30rem);
    --fs-h1: clamp(1.75rem, 1.30rem + 2.10vw, 3.00rem);

    /* Display / hero */
    --fs-display: clamp(2.00rem, 1.30rem + 3.20vw, 4.00rem);

    /* Fluid gutters & padding */
    --pad-card: clamp(0.9rem, 0.6rem + 1.2vw, 1.5rem);
    --pad-section: clamp(2rem, 1rem + 4vw, 5rem);
    --gap-fluid: clamp(0.5rem, 0.3rem + 0.8vw, 1rem);

    /* Neutral backdrop shown behind images while they load / if they break */
    --img-placeholder: #f1ece5;
}

/* ------------------------------------------------------------
   2. OVERFLOW & BOX SAFETY
   ------------------------------------------------------------
   The single most common cause of "the mobile layout is broken"
   is one wide child forcing a horizontal scrollbar on <body>.
   ------------------------------------------------------------ */
html,
body {
    max-width: 100%;
    overflow-x: hidden;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

/* Media never exceeds its container */
img,
svg,
video,
canvas,
iframe,
embed,
object {
    max-width: 100%;
}

/* Let embedded maps/videos shrink */
iframe {
    max-width: 100%;
    border: 0;
}

/* Long unbroken strings (emails, URLs, reference codes) are a classic
   source of mobile overflow — wrap them instead of pushing the page wide. */
p,
li,
dd,
dt,
td,
th,
h1,
h2,
h3,
h4,
h5,
h6,
.card-title,
.card-text,
.breadcrumb-item {
    overflow-wrap: break-word;
    word-wrap: break-word;
}

/* Explicit opt-in for hard-breaking hostile strings */
.break-anywhere {
    overflow-wrap: anywhere;
    word-break: break-word;
}

/* ------------------------------------------------------------
   3. FLUID TYPOGRAPHY
   ------------------------------------------------------------ */
body {
    font-size: var(--fs-base);
    line-height: 1.6;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

h1,
.h1 {
    font-size: var(--fs-h1);
    line-height: 1.18;
}

h2,
.h2 {
    font-size: var(--fs-h2);
    line-height: 1.22;
}

h3,
.h3 {
    font-size: var(--fs-h3);
    line-height: 1.28;
}

h4,
.h4 {
    font-size: var(--fs-h4);
    line-height: 1.32;
}

h5,
.h5 {
    font-size: var(--fs-h5);
    line-height: 1.38;
}

h6,
.h6 {
    font-size: var(--fs-h6);
    line-height: 1.42;
}

.display-1 { font-size: clamp(2.4rem, 1.4rem + 4.6vw, 5.5rem); line-height: 1.08; }
.display-2 { font-size: clamp(2.2rem, 1.3rem + 4.2vw, 5.0rem); line-height: 1.10; }
.display-3 { font-size: clamp(2.0rem, 1.2rem + 3.8vw, 4.5rem); line-height: 1.12; }
.display-4 { font-size: clamp(1.8rem, 1.1rem + 3.4vw, 3.5rem); line-height: 1.14; }
.display-5 { font-size: clamp(1.6rem, 1.1rem + 2.6vw, 3.0rem); line-height: 1.16; }
.display-6 { font-size: clamp(1.4rem, 1.0rem + 2.0vw, 2.5rem); line-height: 1.20; }

.lead {
    font-size: var(--fs-md);
    line-height: 1.65;
}

small,
.small {
    font-size: var(--fs-xs);
}

.fs-1 { font-size: var(--fs-h1) !important; }
.fs-2 { font-size: var(--fs-h2) !important; }
.fs-3 { font-size: var(--fs-h3) !important; }
.fs-4 { font-size: var(--fs-h4) !important; }
.fs-5 { font-size: var(--fs-h5) !important; }
.fs-6 { font-size: var(--fs-h6) !important; }

/* Explicit fluid helpers for one-off use in templates */
.fs-fluid-3xs { font-size: var(--fs-3xs) !important; }
.fs-fluid-2xs { font-size: var(--fs-2xs) !important; }
.fs-fluid-xs  { font-size: var(--fs-xs) !important; }
.fs-fluid-sm  { font-size: var(--fs-sm) !important; }
.fs-fluid-base{ font-size: var(--fs-base) !important; }
.fs-fluid-md  { font-size: var(--fs-md) !important; }
.fs-fluid-lg  { font-size: var(--fs-lg) !important; }

/* Stat / metric numbers on dashboards */
.stat-number,
.stat-value,
.metric-value,
.counter,
.card-stat-value {
    font-size: clamp(1.35rem, 1.0rem + 1.7vw, 2.4rem);
    line-height: 1.15;
}

/* ------------------------------------------------------------
   4. IMAGE CARDS
   ------------------------------------------------------------
   Fixed pixel heights (height: 200px) do not adapt: on a narrow
   phone column the image becomes a tall crop, on a wide desktop
   column it becomes a thin letterbox. aspect-ratio keeps the
   frame proportional to whatever width the grid gives it.
   ------------------------------------------------------------ */

/* Core: fill the frame, crop overflow, never distort. */
.img-cover {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center;
    display: block;
    background-color: var(--img-placeholder);
}

/* Fit the whole image inside the frame (logos, certificates, documents) */
.img-contain {
    width: 100%;
    height: 100%;
    object-fit: contain;
    object-position: center;
    display: block;
    background-color: var(--img-placeholder);
}

/* Card banner images — proportional, so they scale with the column.
   Replaces inline `height: XXXpx; object-fit: cover;`. */
.card-img-cover {
    width: 100%;
    aspect-ratio: 16 / 10;
    object-fit: cover;
    object-position: center;
    display: block;
    background-color: var(--img-placeholder);
}

.card-img-cover-wide { aspect-ratio: 16 / 9; }
.card-img-cover-tall { aspect-ratio: 4 / 3; }
.card-img-cover-square { aspect-ratio: 1 / 1; }
.card-img-cover-portrait { aspect-ratio: 3 / 4; }

/* Photo galleries: portrait-ish frames anchored to the top so faces
   are not cropped out of head-and-shoulders shots. */
.card-img-portrait-top {
    width: 100%;
    aspect-ratio: 4 / 5;
    object-fit: cover;
    object-position: top center;
    display: block;
    background-color: var(--img-placeholder);
}

/* A framed box that keeps its ratio regardless of what is inside
   (image, icon placeholder, gradient) — used for card headers where
   some records have a cover image and some do not, so both branches
   occupy exactly the same space and the grid stays even. */
.img-frame {
    position: relative;
    width: 100%;
    aspect-ratio: 16 / 10;
    overflow: hidden;
    background-color: var(--img-placeholder);
}

.img-frame-wide { aspect-ratio: 16 / 9; }
.img-frame-tall { aspect-ratio: 4 / 3; }
.img-frame-square { aspect-ratio: 1 / 1; }
.img-frame-portrait { aspect-ratio: 4 / 5; }

.img-frame > img,
.img-frame > .img-frame-fill {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center;
}

.img-frame > .img-frame-fill {
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Placeholder icon inside an empty frame scales with the frame */
.img-frame-fill i {
    font-size: clamp(1.6rem, 1.0rem + 2.6vw, 3rem);
}

/* Thumbnails inside table cells — small, fixed ratio, never distorted */
.img-thumb {
    width: clamp(48px, 8vw, 80px);
    aspect-ratio: 4 / 3;
    object-fit: cover;
    object-position: center;
    border-radius: 6px;
    display: block;
    background-color: var(--img-placeholder);
    flex-shrink: 0;
}

.img-thumb-square {
    aspect-ratio: 1 / 1;
    width: clamp(44px, 7vw, 64px);
}

/* Image preview boxes on create/edit forms */
.img-preview {
    width: 100%;
    max-height: clamp(140px, 28vw, 240px);
    object-fit: contain;
    object-position: center;
    border-radius: 10px;
    background-color: var(--img-placeholder);
}

/* Lightbox / modal images: bounded by the viewport, never overflow it */
.modal-body img.img-fluid,
.img-modal {
    max-width: 100%;
    max-height: 75vh;
    object-fit: contain;
    margin-inline: auto;
    display: block;
}

/* Bootstrap's own card image at the top of a card */
.card-img-top {
    background-color: var(--img-placeholder);
}

/* Substituted by js/image-fallback.js when a source 404s. The class is a hook
   for styling and for spotting missing uploads in the browser inspector — the
   replacement image itself is sized by whichever frame class it already had.
   Give an image a `data-fallback="..."` attribute to choose its own
   replacement (e.g. a generated avatar for a person). */
.img-fallback {
    background-color: var(--img-placeholder);
}

/* ------------------------------------------------------------
   5. AVATARS
   ------------------------------------------------------------
   Sized with clamp() so they shrink on phones without becoming
   unrecognisable, and never stretch out of round.
   ------------------------------------------------------------ */
.avatar,
.avatar-xs,
.avatar-sm,
.avatar-md,
.avatar-lg,
.avatar-xl,
.avatar-2xl {
    /* Small avatars fill their circle: at 24-64px a letterboxed photo is
       unreadable, so a centred crop is the right trade. `center`, not
       `top center` — anchoring to the top pushed faces off-centre on
       anything that was not a head-and-shoulders shot. */
    object-fit: cover;
    object-position: center;
    flex-shrink: 0;
    display: inline-block;
    background-color: var(--img-placeholder);
    border-radius: 50%;
}

.avatar-xs  { width: clamp(24px, 4.5vw, 30px);  height: clamp(24px, 4.5vw, 30px); }
.avatar-sm  { width: clamp(30px, 5.5vw, 38px);  height: clamp(30px, 5.5vw, 38px); }
.avatar,
.avatar-md  { width: clamp(36px, 7vw, 46px);    height: clamp(36px, 7vw, 46px); }
.avatar-lg  { width: clamp(48px, 9vw, 64px);    height: clamp(48px, 9vw, 64px); }
.avatar-xl  { width: clamp(64px, 14vw, 96px);   height: clamp(64px, 14vw, 96px); }
.avatar-2xl { width: clamp(88px, 22vw, 150px);  height: clamp(88px, 22vw, 150px); }

/* Any round image keeps its circle even if the source is not square */
.rounded-circle {
    object-fit: cover;
}

/* Large profile displays (a member's own photo, a profile header) show the
   whole picture rather than a crop — at this size the framing of the upload
   is part of what the member chose to show. Declared after .rounded-circle
   so it wins on the round profile headers that carry both classes. */
.avatar-xl,
.avatar-2xl,
.img-contain-center {
    object-fit: contain;
    object-position: center;
    background-color: var(--img-placeholder);
}

/* Note: a leftover `width="40"` attribute on an avatar is a presentational
   hint and loses to the author rules above, so the fluid size still wins. */

/* ------------------------------------------------------------
   6. TABLES
   ------------------------------------------------------------
   Data tables cannot reflow to 320px, so they scroll horizontally
   inside their own container — the page itself must never scroll.
   ------------------------------------------------------------ */
.table-responsive {
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}

.table {
    font-size: var(--fs-sm);
    vertical-align: middle;
}

.table > :not(caption) > * > * {
    padding: clamp(0.45rem, 0.3rem + 0.5vw, 0.85rem) clamp(0.4rem, 0.25rem + 0.5vw, 0.85rem);
}

.table th {
    font-size: var(--fs-xs);
    white-space: nowrap;
}

/* Action button clusters in the last column stay on one row */
.table .btn-group,
.table .btn-group-sm {
    flex-wrap: nowrap;
}

.table .btn {
    white-space: nowrap;
}

/* Hint that a table is scrollable rather than clipped */
@media (max-width: 767.98px) {
    .table-responsive {
        border-radius: 8px;
    }

    .table-responsive > .table {
        margin-bottom: 0;
    }
}

/* ------------------------------------------------------------
   7. FORMS
   ------------------------------------------------------------ */
.form-control,
.form-select,
.input-group-text {
    font-size: var(--fs-sm);
}

.form-label {
    font-size: var(--fs-xs);
    font-weight: 600;
}

.form-text,
.invalid-feedback,
.valid-feedback {
    font-size: var(--fs-3xs);
}

/* iOS Safari zooms the page when a focused input is under 16px.
   Force 16px on touch-sized viewports to keep the layout still. */
@media (max-width: 767.98px) {
    .form-control,
    .form-select,
    textarea,
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    input[type="url"],
    input[type="search"],
    input[type="date"],
    input[type="datetime-local"],
    input[type="time"] {
        font-size: 16px;
    }
}

/* Select2 inherits the site scale instead of its own 14px default */
.select2-container .select2-selection--single,
.select2-container .select2-selection--multiple,
.select2-container .select2-search__field,
.select2-results__option {
    font-size: var(--fs-sm);
}

.select2-container {
    max-width: 100%;
}

/* Stack input groups that would otherwise squeeze on narrow screens */
@media (max-width: 575.98px) {
    .input-group-stack-sm {
        flex-direction: column;
    }

    .input-group-stack-sm > * {
        width: 100%;
        border-radius: var(--bs-border-radius, 0.375rem) !important;
    }

    .input-group-stack-sm > * + * {
        margin-top: 0.5rem;
        margin-left: 0 !important;
    }
}

/* ------------------------------------------------------------
   8. BUTTONS & BADGES
   ------------------------------------------------------------ */
.btn {
    font-size: var(--fs-sm);
    padding: clamp(0.35rem, 0.25rem + 0.35vw, 0.55rem) clamp(0.7rem, 0.5rem + 0.7vw, 1.2rem);
}

.btn-sm {
    font-size: var(--fs-2xs);
    padding: clamp(0.22rem, 0.16rem + 0.25vw, 0.35rem) clamp(0.45rem, 0.3rem + 0.5vw, 0.75rem);
}

.btn-lg {
    font-size: var(--fs-md);
    padding: clamp(0.5rem, 0.35rem + 0.5vw, 0.8rem) clamp(1rem, 0.7rem + 1.2vw, 1.8rem);
}

.badge {
    font-size: var(--fs-3xs);
    padding: 0.35em 0.6em;
    white-space: nowrap;
}

/* Toolbars of buttons wrap onto a second row instead of overflowing */
.btn-toolbar,
.page-actions,
.card-actions {
    flex-wrap: wrap;
    gap: var(--gap-fluid);
}

/* Full-width primary actions on phones — easier to hit, no overflow */
@media (max-width: 575.98px) {
    .btn-block-sm,
    .btn-block-sm .btn {
        width: 100%;
    }

    .btn-group-stack-sm {
        display: flex;
        flex-direction: column;
        width: 100%;
        gap: 0.5rem;
    }

    .btn-group-stack-sm > .btn {
        width: 100%;
        border-radius: 50px !important;
    }
}

/* Touch targets: keep icon-only buttons tappable (44px WCAG guidance) */
@media (hover: none) and (pointer: coarse) {
    .btn-icon,
    .btn-sm.btn-icon {
        min-width: 38px;
        min-height: 38px;
    }
}

/* ------------------------------------------------------------
   9. CARDS & PANELS
   ------------------------------------------------------------ */
.card-body,
.admin-card-body {
    padding: var(--pad-card);
}

.card-title {
    font-size: var(--fs-h5);
}

.card-text {
    font-size: var(--fs-sm);
}

.card-header {
    padding: var(--pad-card);
}

.card-header h1, .card-header h2, .card-header h3,
.card-header h4, .card-header h5, .card-header h6 {
    margin-bottom: 0;
}

/* Header rows that put a title on the left and actions on the right
   should stack rather than squash on small screens. */
@media (max-width: 575.98px) {
    .stack-sm {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: var(--gap-fluid);
    }

    .stack-sm > * {
        width: 100%;
    }

    .stack-sm .text-end {
        text-align: left !important;
    }
}

/* ------------------------------------------------------------
   10. MODALS, DROPDOWNS & OFFCANVAS
   ------------------------------------------------------------ */
.modal-title {
    font-size: var(--fs-h5);
}

.modal-body {
    font-size: var(--fs-sm);
}

@media (max-width: 575.98px) {
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }

    .modal-body {
        padding: var(--pad-card);
    }

    .modal-footer {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .modal-footer > .btn {
        flex: 1 1 auto;
    }
}

/* Dropdown panels declared with an inline pixel width (notifications,
   messages) must not exceed the viewport on small phones. */
.dropdown-menu {
    font-size: var(--fs-sm);
    max-width: calc(100vw - 1.5rem);
}

@media (max-width: 400px) {
    .dropdown-menu[style*="width"] {
        width: calc(100vw - 1.5rem) !important;
    }
}

.dropdown-item {
    font-size: var(--fs-sm);
    white-space: normal;
}

/* ------------------------------------------------------------
   11. NAV, TABS & PAGINATION
   ------------------------------------------------------------ */
.nav-tabs,
.nav-pills {
    flex-wrap: wrap;
    gap: 0.25rem;
}

.nav-link {
    font-size: var(--fs-sm);
}

/* Tab strips that would wrap awkwardly scroll sideways on phones */
@media (max-width: 575.98px) {
    .nav-scroll-sm {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    .nav-scroll-sm::-webkit-scrollbar {
        display: none;
    }

    .nav-scroll-sm .nav-link {
        white-space: nowrap;
    }
}

.pagination {
    flex-wrap: wrap;
    gap: 0.2rem;
    justify-content: center;
}

.page-link {
    font-size: var(--fs-xs);
    padding: 0.35rem 0.6rem;
}

.breadcrumb {
    font-size: var(--fs-xs);
    flex-wrap: wrap;
}

/* ------------------------------------------------------------
   12. TIMELINE / LIST GROUPS / ALERTS
   ------------------------------------------------------------ */
.list-group-item {
    font-size: var(--fs-sm);
    padding: clamp(0.6rem, 0.4rem + 0.6vw, 1rem);
}

.alert {
    font-size: var(--fs-sm);
    padding: clamp(0.7rem, 0.5rem + 0.7vw, 1.1rem);
}

.alert h4, .alert h5, .alert h6 {
    font-size: var(--fs-h6);
}

/* ------------------------------------------------------------
   13. SECTION RHYTHM
   ------------------------------------------------------------ */
.section-padding-fluid {
    padding-block: var(--pad-section);
}

/* Bootstrap containers get a slightly tighter gutter on small phones
   so cards do not touch the screen edge but still use the width. */
@media (max-width: 400px) {
    .container,
    .container-fluid,
    .container-sm,
    .container-md,
    .container-lg,
    .container-xl {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }
}

/* Charts must resize with their container, not overflow it */
canvas {
    max-width: 100%;
}

.chart-container {
    position: relative;
    width: 100%;
    height: clamp(220px, 40vw, 380px);
}

/* ------------------------------------------------------------
   14. PRINT
   ------------------------------------------------------------ */
@media print {
    .no-print,
    .btn,
    .navbar,
    .sidebar,
    .pagination {
        display: none !important;
    }

    body {
        background: #fff;
        font-size: 12pt;
    }

    .table-responsive {
        overflow: visible;
    }
}

/* ------------------------------------------------------------
   15. MOTION PREFERENCES
   ------------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}
