/* RapidForge baseline shell + form contracts.
   ------------------------------------------------------------------------------------------------
   One canonical, theme-agnostic layer for behaviours the framework baseline must guarantee on EVERY
   generated project, in both languages, at every viewport. It carries no colours of its own beyond the
   design tokens already published by the theme layer, so switching theme profiles never changes it and
   it is not part of the theme redesign surface.

   Sections:
     1. Sticky-footer shell     — the footer follows the content, and the viewport bottom on short pages.
     2. Generic modal body      — arbitrary Add/Edit partials flow normally instead of being centred.
     3. Bidi technical values   — mixed-direction values (codes, file names, sizes, dates, extensions)
                                  keep their logical order inside an Arabic (RTL) page.
     4. Required-field marker   — a required control is visibly AND programmatically marked.
     5. Themed file input       — the real <input type="file"> keeps the app's visual system + focus ring.
     6. Generic Add/Edit modal  — md 680 / lg 860 px dialog; form columns follow the dialog (container query).
     7. Date picker day states  — selected / today / hover / other-month / disabled always distinct and readable.
     8. Back to top             — 44 px reading-end circle, shown after scrolling, hidden under a modal.
     9. Direction (RF-2)        — Bootstrap RTL build, select arrow at the reading end, switch label gap.
    10. Skip link + main (RF-FOLLOWUP-3) — keyboard jump to the one <main> landmark.
    11. Text contrast (RF-FINAL a11y) — outline buttons, language chip and admin footer reach WCAG AA in any profile.
   ------------------------------------------------------------------------------------------------ */

/* ---- 1. Sticky-footer shell ---------------------------------------------------------------------
   _Layout.Base already builds the right box tree (body.d-flex.flex-column > #bodyContent.flex-fill +
   #footerContent) but never gave the body a height to distribute, so on a short page flex-fill had
   nothing to fill and the footer stopped wherever the content stopped — mid-viewport. A min-height on
   the shell is all that is missing: long pages still grow past it and scroll normally, and the footer
   is never taken out of flow, so it can never overlay content. */
body.d-flex.flex-column {
    min-height: 100vh;
}

/* Where the browser supports it, prefer the dynamic viewport unit so mobile browser chrome collapsing
   does not leave a strip of blank page under the footer. */
@supports (min-height: 100dvh) {
    body.d-flex.flex-column {
        min-height: 100dvh;
    }
}

/* #bodyContent already carries Bootstrap's .flex-fill. State the growth explicitly so the contract does
   not depend on a utility class surviving a future layout edit, and allow it to shrink below its
   content box so long pages still scroll rather than forcing the shell wider. */
body.d-flex.flex-column > #bodyContent {
    flex: 1 1 auto;
    min-height: 0;
}

body.d-flex.flex-column > #footerContent {
    flex: 0 0 auto;
}

/* ---- 2. Generic modal body ----------------------------------------------------------------------
   #genericModalBody hosts arbitrary Add/Edit partials loaded over AJAX. The shared .ModalBodyCss
   centring helper (correct for the short single-message operation modals) turned it into a centred flex
   ROW, so a partial's sibling blocks became side-by-side flex items at EVERY width — which is what
   starved the TUsers form column next to the attachments card at 390 px and left the two cards
   vertically mis-aligned at desktop. The generic body flows normally; the partial owns its own grid. */
#genericModalBody {
    display: block;
}

/* ---- 3. Bidi: technical values ------------------------------------------------------------------
   Permission codes, file names, extension lists, byte sizes, dates and content types are technical
   values with a fixed logical order. Inside an Arabic page the bidi algorithm re-orders their neutral
   characters (". " and ",") against the surrounding RTL run, so ".pdf,.docx" reads "pdf,.docx." and
   "193 B" reads "B 193". Isolating the value and stating its direction fixes the rendering WITHOUT
   touching what is stored: no direction marks are ever written to the database or posted back. */
.rf-ltr {
    direction: ltr;
    unicode-bidi: isolate;
}

/* Inline technical values must not drag the whole line's alignment with them: they stay inline and
   inherit the surrounding block's logical alignment. */
span.rf-ltr,
code.rf-ltr,
bdi.rf-ltr {
    display: inline;
    text-align: start;
}

/* An LTR value inside a form control: the control keeps its logical placement in the page, only its
   text runs LTR. Used for the extension list, the permission code and other typed technical values. */
input.rf-ltr,
textarea.rf-ltr {
    direction: ltr;
    text-align: start;
    unicode-bidi: normal; /* a form control is already its own bidi paragraph */
}

/* ---- 3b. Keyboard focus on the shell controls ---------------------------------------------------
   Inputs and buttons already paint a focus ring. The two shell controls that did not —
   the sidebar toggle and the language switch — are reachable by keyboard, so they must show where the
   focus is. :focus-visible only, so a mouse click does not leave a ring behind. */
#menu-bar:focus-visible,
.box-icon:focus-visible,
button.box-icon:focus-visible {
    outline: 2px solid var(--primary-base1, #b08d2e);
    outline-offset: 2px;
    border-radius: var(--rf-radius-sm, 6px);
}

/* Sidebar / navigation links (RF-FINAL-CLOSURE, 2026-10-12). They had no focus style of their own and relied on the
   browser default ring. The ring is drawn in the link's own text colour: that colour already meets AA text contrast against
   the link's background in every state (rest, hover, active) and theme, so the ring is always at least 3:1 (WCAG 1.4.11).
   Inset, because the sidebar scrolls and an outer ring would be clipped at its edges. */
#sideMenu a.item-link:focus-visible,
#sideMenu .submenu a.item-link:focus-visible,
.rf-sec-nav a:focus-visible {
    outline: 2px solid currentColor;
    outline-offset: -3px;
    border-radius: var(--rf-radius-sm, 6px);
}

/* ---- 4. Required-field marker -------------------------------------------------------------------
   Colour is never the only signal: the marker is a character plus an accessible name, and the control
   itself carries aria-required, so assistive technology does not depend on the glyph at all. */
.rf-required-mark {
    margin-inline-start: .2rem;
    font-weight: 700;
    line-height: 1;
    color: var(--rf-color-danger, #dc3545);
}

/* ---- 5. Themed file input -----------------------------------------------------------------------
   The REAL <input type="file"> is kept — no fake picker, no hidden input, no JavaScript shim — so the
   control stays keyboard-reachable and announces normally. Bootstrap styles the control box but leaves
   ::file-selector-button on its own default palette, which is what broke out of the theme. */
.form-control[type="file"] {
    background-color: var(--input-bg, #fff);
    color: var(--input-fg, inherit);
    border: 1px solid var(--input-border, rgba(0, 0, 0, .18));
    border-radius: var(--rf-radius-sm, 6px);
}

.form-control[type="file"]::file-selector-button {
    background-color: var(--cardHeader-bg, rgba(0, 0, 0, .05));
    color: var(--cardHeader-fg, inherit);
    border: 0;
    border-inline-end: 1px solid var(--input-border, rgba(0, 0, 0, .18));
    border-radius: 0;
    padding: .375rem .75rem;
    margin-inline-start: -.75rem;
    margin-inline-end: .75rem;
    cursor: pointer;
    transition: background-color .15s ease-in-out;
}

.form-control[type="file"]:hover::file-selector-button {
    background-color: var(--card-hover-bg, rgba(0, 0, 0, .09));
}

/* The focus ring must be visible on the control itself — a file input is operated from the keyboard and
   the native button does not always paint one inside a themed container. Reuses the same focus token
   the themed text inputs use, so the whole form focuses consistently. */
.form-control[type="file"]:focus,
.form-control[type="file"]:focus-visible {
    border-color: var(--primary-base1, #0d6efd);
    box-shadow: var(--btn-focus-ring, 0 0 0 .2rem rgba(13, 110, 253, .25));
    outline: none;
}

/* ---- 6. Generic Add/Edit modal: size and form grid (RF-UI-GENERATION-01) ------------------------------
   openGenericModal() picks sm / md / lg / xl per entity (auto from the field count, or the entity's
   ui.modalSize). "md" added no class, so the dialog was Bootstrap's 500 px while the generated forms lay their
   fields out in VIEWPORT columns (col-lg-6 / col-lg-4): on a desktop two or three columns were squeezed into
   500 px, long labels broke onto two lines and lookup selects were cut (SchoolPilot SP-12B G-9).
   The dialog sizes are now md 680 px / lg 860 px (sm and xl keep their scale), always inside the phone width,
   and the form columns follow the DIALOG width through a container query instead of the viewport. The rules
   are scoped to #genericModal, so every other modal keeps Bootstrap's sizes. */
@media (min-width: 576px) {
    #genericModalDialog.modal-dialog {
        max-width: min(680px, calc(100vw - 1.5rem));
    }

    #genericModalDialog.modal-dialog.modal-sm {
        max-width: min(420px, calc(100vw - 1.5rem));
    }

    #genericModalDialog.modal-dialog.modal-lg {
        max-width: min(860px, calc(100vw - 1.5rem));
    }

    #genericModalDialog.modal-dialog.modal-xl {
        max-width: min(1140px, calc(100vw - 1.5rem));
    }
}

@media (max-width: 575.98px) {
    #genericModalDialog.modal-dialog:not(.modal-fullscreen) {
        margin: .75rem;
    }
}

#genericModalBody {
    container-type: inline-size;
}

/* The generated form ships its own bordered card; inside the dialog that drew a second frame. */
#genericModalBody > .card,
#genericModalBody > .card > .card-body {
    border: 0;
    box-shadow: none;
    background: transparent;
    border-radius: 0;
}

/* Labels: never truncated; a genuinely long (e.g. Arabic) label wraps naturally between words. */
#genericModalBody .form-label {
    line-height: 1.35;
    overflow-wrap: break-word;
}

/* Narrow dialog (phones, sm dialogs): one field per row. */
@container (max-width: 520px) {
    #genericModalBody .row > [class*="col-lg-"],
    #genericModalBody .row > [class*="col-md-"] {
        flex: 0 0 auto;
        width: 100%;
        max-width: 100%;
    }
}

/* Medium dialog: at most two fields per row (a col-lg-4 / col-lg-3 grid would leave ~200 px per field). */
@container (min-width: 521px) {
    #genericModalBody .row > .col-lg-4,
    #genericModalBody .row > .col-lg-3 {
        flex: 0 0 auto;
        width: 50%;
        max-width: 50%;
    }
}

/* Wide dialog: the generated grid as designed. */
@container (min-width: 760px) {
    #genericModalBody .row > .col-lg-4 {
        width: 33.3333%;
        max-width: 33.3333%;
    }

    #genericModalBody .row > .col-lg-3 {
        width: 25%;
        max-width: 25%;
    }
}

/* Lookup (relationship) selects carry long human-readable names: a full row while the dialog is not wide, and a
   long chosen value wraps onto a second line instead of being cut with an ellipsis. */
@container (max-width: 759px) {
    #genericModalBody .row > [class*="col-"]:has(> select.select2),
    #genericModalBody .row > [class*="col-"]:has(> .select2-container) {
        width: 100%;
        max-width: 100%;
    }
}

#genericModalBody .select2-container .select2-selection--single {
    height: auto;
    min-height: 38px;
    display: flex;
    align-items: center;
}

    #genericModalBody .select2-container .select2-selection--single .select2-selection__rendered {
        white-space: normal;
        overflow: visible;
        text-overflow: clip;
        line-height: 1.4;
    }

/* Save / Cancel: one end-aligned group (Cancel, then Save) instead of two buttons pushed to the far edges by
   mx-5; Save is the dialog's primary (filled) action. Theme tokens only. */
#genericModalBody form .row > .mt-4.d-flex.justify-content-between {
    justify-content: flex-end !important;
    gap: .6rem;
    padding-block-start: 1rem;
    border-block-start: 1px solid var(--card-border);
}

    #genericModalBody form .row > .mt-4.d-flex.justify-content-between > .btn-icon-box {
        margin: 0 !important;
        order: 2;
    }

        #genericModalBody form .row > .mt-4.d-flex.justify-content-between > .btn-icon-box:has(> button[type="submit"]) {
            order: 3;
            background: var(--primary-base1);
            border-color: var(--primary-border1, var(--primary-base1));
        }

            #genericModalBody form .row > .mt-4.d-flex.justify-content-between > .btn-icon-box:has(> button[type="submit"]) > button,
            #genericModalBody form .row > .mt-4.d-flex.justify-content-between > .btn-icon-box:has(> button[type="submit"]) > button > i {
                color: var(--cardHeader-title-fg, #ffffff);
            }

/* ---- 7. Date picker (jQuery UI) day states (RF-UI-GENERATION-01) -------------------------------------
   The theme's generic in-flow link rule (body.<area>-area a:not(...) { color: var(--link-fg) }, specificity
   0,4,2) also painted the calendar's day LINKS, so the selected day (accent background) showed accent-coloured
   text on the accent: unreadable (SchoolPilot SP-12B G-8). Every day state is set here explicitly, with
   !important so no link or vendor theme rule can repaint it:
     normal - page text on the surface;   hover - row-hover surface;   today - accent ring, bold;
     selected - accent fill with the theme's ON-ACCENT text token (>= 4.5:1 in every shipped profile);
     other month - muted;   disabled - faded.
   No weekend columns are styled: which days are a weekend depends on the organisation and the locale. The
   header is a light band (no accent fill), so jQuery UI's arrow sprites stay visible on every theme. */
.ui-datepicker {
    padding: .5rem;
    width: 18.5rem;
    font-family: inherit;
    background: var(--card-bg);
    color: var(--card-fg);
    border: 1px solid var(--card-border) !important;
    border-radius: 12px;
    box-shadow: 0 12px 32px rgba(var(--shadow-rgb, 15,23,42), .18);
}

    .ui-datepicker .ui-datepicker-header {
        background: transparent;
        color: var(--page-fg);
        border: 0;
        border-block-end: 1px solid var(--card-border);
        border-radius: 0;
        padding: .35rem 0 .5rem;
    }

    .ui-datepicker .ui-datepicker-title {
        color: var(--page-fg);
        font-weight: 700;
    }

        .ui-datepicker .ui-datepicker-title select {
            color: var(--input-fg, var(--page-fg));
            background: var(--input-bg, var(--card-bg));
            border: 1px solid var(--input-border);
            border-radius: 6px;
            padding: .1rem .25rem;
            font-size: .85rem;
        }

    .ui-datepicker .ui-datepicker-prev,
    .ui-datepicker .ui-datepicker-next {
        cursor: pointer;
        border-radius: 50%;
    }

        .ui-datepicker .ui-datepicker-prev:hover,
        .ui-datepicker .ui-datepicker-next:hover {
            background: var(--table-row-hover-bg);
            border: 0;
        }

    .ui-datepicker table.ui-datepicker-calendar {
        margin: .35rem 0 0;
        border-collapse: separate;
        border-spacing: 2px;
    }

    .ui-datepicker .ui-datepicker-calendar th {
        color: var(--muted-fg);
        font-weight: 700;
        font-size: .75rem;
        padding: .3rem 0;
    }

    .ui-datepicker .ui-datepicker-calendar td {
        padding: 0;
    }

body .ui-datepicker .ui-datepicker-calendar td a.ui-state-default,
body .ui-datepicker .ui-datepicker-calendar td span.ui-state-default {
    display: block;
    text-align: center;
    padding: .38rem 0;
    border-radius: 8px;
    border: 1px solid transparent !important;
    background: transparent !important;
    color: var(--page-fg) !important;
    font-weight: 600;
    font-size: .85rem;
    line-height: 1.2;
}

body .ui-datepicker .ui-datepicker-calendar td a.ui-state-default:hover,
body .ui-datepicker .ui-datepicker-calendar td a.ui-state-hover {
    background: var(--table-row-hover-bg) !important;
    color: var(--page-fg) !important;
    border-color: var(--input-border) !important;
}

body .ui-datepicker .ui-datepicker-calendar td.ui-datepicker-today a.ui-state-default,
body .ui-datepicker .ui-datepicker-calendar td a.ui-state-highlight {
    background: transparent !important;
    color: var(--page-fg) !important;
    border: 2px solid var(--primary-base1) !important;
    font-weight: 800;
}

body .ui-datepicker .ui-datepicker-calendar td a.ui-state-active,
body .ui-datepicker .ui-datepicker-calendar td.ui-datepicker-current-day a.ui-state-default {
    background: var(--primary-base1) !important;
    color: var(--cardHeader-title-fg, #ffffff) !important;
    border-color: var(--primary-border1, var(--primary-base1)) !important;
    font-weight: 800;
}

body .ui-datepicker .ui-datepicker-calendar td.ui-datepicker-other-month a.ui-state-default {
    color: var(--muted-fg) !important;
    opacity: .6;
}

body .ui-datepicker .ui-datepicker-calendar td.ui-state-disabled,
body .ui-datepicker .ui-datepicker-calendar td.ui-state-disabled span.ui-state-default {
    opacity: .35;
}

/* A date picker opened from a field inside a modal stays above the dialog. */
.modal-open .ui-datepicker {
    z-index: 1065 !important;
}

/* ---- 8. Back to top (RF-UI-GENERATION-01) -------------------------------------------------------------
   Was a 35 px solid accent square with a 24 px boxed arrow, rendered visible ("active") even at the top of the
   page (SchoolPilot SP-12B G-11). Now a 44 px light circle (touch size) on the READING END (right in LTR, left
   in RTL) with a centred fa-arrow-up-to-line glyph; main.js shows it only after scrolling; hover fills it with
   the accent, :active presses it, :focus-visible shows the theme focus ring; it hides while a modal is open. */
.back-to-top {
    width: 44px;
    height: 44px;
    bottom: 24px;
    border-radius: 50%;
    background: var(--card-bg);
    color: var(--primary-base1);
    border: 1px solid var(--card-border);
    box-shadow: 0 6px 18px rgba(var(--shadow-rgb, 15,23,42), .16);
    font-size: 1rem;
    line-height: 1;
    transition: opacity .2s ease, visibility .2s ease, background-color .15s ease, color .15s ease, transform .15s ease;
}

    .back-to-top > i {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: auto;
        height: auto;
        line-height: 1;
        color: inherit;
        font-size: 1rem;
    }

    .back-to-top:hover {
        background: var(--primary-base1);
        color: var(--cardHeader-title-fg, #ffffff);
        border-color: var(--primary-base1);
        transform: translateY(-2px);
    }

    .back-to-top:active {
        transform: translateY(0);
        background: var(--primary-hover1, var(--primary-base1));
    }

    .back-to-top:focus-visible {
        outline: none;
        box-shadow: var(--btn-focus-ring);
    }

body[dir="ltr"] .back-to-top {
    right: 24px;
    left: auto;
}

body[dir="rtl"] .back-to-top {
    left: 24px;
    right: auto;
}

@media (max-width: 768px) {
    .back-to-top {
        bottom: 16px;
    }

    body[dir="ltr"] .back-to-top {
        right: 16px;
    }

    body[dir="rtl"] .back-to-top {
        left: 16px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .back-to-top,
    .back-to-top:hover {
        transition: none;
        transform: none;
    }
}

.modal-open .back-to-top {
    opacity: 0 !important;
    visibility: hidden !important;
}

/* ---- 9. Direction (RF-2, 2026-10-11) --------------------------------------------------------------
   The layout loads Bootstrap's RTL build for an RTL culture, so Bootstrap's own components follow the reading
   direction. Site.css styles .form-select with equal 0.5rem padding and pins the arrow to the right, which lets a
   long option run under the arrow and, in Arabic, puts the arrow at the reading start. The arrow belongs at the
   reading END with room reserved for it, in both directions. */
.form-select {
    padding-inline-end: 2rem;
}

html[dir="ltr"] .form-select {
    background-position: right 0.5rem center;
}

html[dir="rtl"] .form-select {
    background-position: left 0.5rem center;
}

/* Site.css scales every checkbox to 1.75x with a transform, which does not move the layout box, so the visible
   control spills over the 0.5em gap Bootstrap leaves before the label: a switch (2em wide) then covers the start of
   its label in both directions. A switch keeps its natural size; a scaled checkbox gets the extra gap it needs. The
   gap is a logical property, so it sits between the box and the label in either direction. */
.form-switch .form-check-input {
    transform: none;
}

.form-check:not(.form-switch) > .form-check-input + .form-check-label {
    padding-inline-start: 0.35em;
}

/* ---- 10. Skip link + main landmark (RF-FOLLOWUP-3, 2026-10-11) ------------------------------------
   _Layout.Base renders a skip link as the first focusable element and ONE <main id="mainContent">. The link is hidden
   until it receives keyboard focus (Bootstrap .visually-hidden-focusable), then appears at the reading-start corner
   above the header. <main> takes focus programmatically (tabindex="-1"); it is a container, not a control, so it
   draws no focus ring of its own. Colours are the theme's tokens (system colours only as a fallback). */
.rf-skip-link:focus,
.rf-skip-link:focus-visible {
    position: fixed;
    top: 0.5rem;
    inset-inline-start: 0.5rem;
    z-index: 2000;
    padding: 0.625rem 1rem;
    background: var(--card-bg, Canvas);
    color: var(--page-fg, CanvasText);
    border: 2px solid currentColor;
    border-radius: var(--theme-radius-sm, 0.375rem);
    box-shadow: var(--theme-shadow-sm, none);
    text-decoration: underline;
}

#mainContent:focus {
    outline: none;
}

/* ---- 11. Text contrast of template controls (RF-FINAL a11y, 2026-10-11) ----------------------------------------
   Found by the RF-FINAL accessibility scan (axe-core, WCAG 2.1 AA) on the default profile: the outline-primary buttons and
   the language chip drew the brand primary (FleetNavyGold #C9A227) as TEXT on white / cream (2.2-2.4:1), the admin footer
   drew white text on it (2.4:1), and the grey outline button fell just under 4.5:1 on tinted table rows. Instead of new
   colours, each control re-scopes the theme's OWN tokens to a stronger shade derived from them (the technique the RF-3 dark
   layer uses for the grey button): text, border and hover stay one consistent hue, any profile keeps its identity, and a
   profile whose primary is already dark (CorporateBlue) only gets darker. Light mode only where the dark layer already
   passes; the footer in both modes. Checked: 62% primary + black >= 5.0:1 on white, cream and tinted rows; white on it
   >= 5.6:1; 80% grey + black >= 6.0:1. */
:root {
    --rf-primary-strong: color-mix(in srgb, var(--primary-base1) 62%, black);
    --rf-neutral-strong: color-mix(in srgb, var(--neutral-gray-base) 80%, black);
}

html:not([data-theme="dark"]) .btn-outline-primary,
html:not([data-theme="dark"]) .icon-white-gold {
    --primary-base1: var(--rf-primary-strong);
    --primary-border1: var(--rf-primary-strong);
}

html:not([data-theme="dark"]) .btn-outline-secondary {
    --neutral-gray-base: var(--rf-neutral-strong);
    --bs-btn-color: var(--rf-neutral-strong);
}

#footerContent .footer.bg-fast-primary1 {
    background-color: var(--rf-primary-strong) !important;
}

/* Dark mode (RF-FINAL-CLOSURE, 2026-10-12; axe-core in System / dark): the outline-danger button drew the danger red as TEXT
   on the dark surface, under 4.5:1. At rest its text is lightened from the theme's own token (60% red + white, about 5.8:1 on
   the dark surfaces); hover / focus / active keep white text on the full red, which already passes. */
html[data-theme="dark"] .btn-outline-danger:not(:hover):not(:focus-visible):not(:active):not(.active) {
    color: color-mix(in srgb, var(--danger-red-base) 60%, white) !important;
}
