/*
 * app-responsive.css — one place that makes every ShulePremier screen behave
 * the same in: desktop browser, tablet, mobile browser / WebView, and the
 * installed PWA (display-mode: standalone).
 *
 * Loaded AFTER each page shell's own <style>, so it can safely refine it.
 * Requires <meta name="viewport" content="width=device-width, initial-scale=1,
 * viewport-fit=cover"> on the page, otherwise env(safe-area-*) stays 0.
 */

:root {
    --sa-top:    env(safe-area-inset-top, 0px);
    --sa-right:  env(safe-area-inset-right, 0px);
    --sa-bottom: env(safe-area-inset-bottom, 0px);
    --sa-left:   env(safe-area-inset-left, 0px);
}

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; max-width: 100%; overflow-x: hidden; }
body { max-width: 100%; -webkit-tap-highlight-color: rgba(26, 71, 42, .12); }

/* Nothing inside the content area may push the page wider than the screen. */
.main-content { min-width: 0; max-width: 100%; }
.main-content img, .main-content video, .main-content canvas, .main-content svg, .main-content iframe { max-width: 100%; }
.main-content img { height: auto; }
.main-content pre { max-width: 100%; overflow-x: auto; }
.table-container, .card, .modal-content { min-width: 0; }

/* ------------------------------------------------------------------
 * MODALS — header + footer stay put, the BODY scrolls.
 *
 * Bootstrap's .modal-dialog-scrollable only works when .modal-header,
 * .modal-body and .modal-footer are DIRECT children of .modal-content.
 * Most pages here wrap them in a <form>, which silently switched the
 * scrolling off (long modals such as "Assign Learning Areas" ran off the
 * bottom of the phone with no way to reach Save). Making the form part of
 * the flex column restores it for every modal in the app.
 * ------------------------------------------------------------------ */
.modal-content > form,
.modal-content > .modal-form {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    max-height: inherit;
    overflow: hidden;
}
.modal-body {
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}
.modal-header, .modal-footer { flex-shrink: 0; }
.modal-header .modal-title { min-width: 0; overflow-wrap: anywhere; }
.modal-footer { gap: .5rem; flex-wrap: wrap; }

/* Never taller than the visible screen (dvh = excludes mobile browser bars) */
.modal-dialog:not(.modal-fullscreen):not([class*="modal-fullscreen-"]) .modal-content {
    max-height: calc(100vh - 1rem);
    max-height: calc(100dvh - 1rem);
}
@media (min-width: 576px) {
    .modal-dialog:not(.modal-fullscreen):not([class*="modal-fullscreen-"]) .modal-content {
        max-height: calc(100vh - 3.5rem);
        max-height: calc(100dvh - 3.5rem);
    }
}

/* Full-screen-on-phones modals: honour notch / home-indicator areas */
@media (max-width: 575.98px) {
    .modal-dialog.modal-fullscreen-sm-down {
        margin: 0;
        max-width: none;
        width: 100%;
        height: 100%;
    }
    .modal-fullscreen-sm-down .modal-content { height: 100%; max-height: none; border: 0; border-radius: 0; }
    .modal-fullscreen-sm-down .modal-header { padding-top: calc(.75rem + var(--sa-top)); }
}
@media (max-width: 767.98px) {
    .modal-fullscreen-md-down .modal-content { max-height: none; }
}
.modal-footer { padding-bottom: calc(.75rem + var(--sa-bottom)); }
.modal { padding-left: var(--sa-left) !important; padding-right: var(--sa-right) !important; }

/* ------------------------------------------------------------------
 * TABLES / DATATABLES
 * ------------------------------------------------------------------ */
/* A table that is not using DataTables' Responsive still scrolls sideways
   inside its own box instead of stretching the page. */
.dataTables_wrapper { max-width: 100%; }
.dataTables_wrapper > .row > .col-sm-12:not([class*="col-md"]) {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
table.dataTable { max-width: 100%; }
td .btn-group, td .btn-group-sm { flex-wrap: nowrap; white-space: nowrap; }

/* Fixed-size A4 previews get their own sideways scroll on narrow screens.
   Never while printing or capturing a PDF, or the sheet would be clipped. */
.doc-scroll { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
body.generating-pdf .doc-scroll { overflow: visible !important; }
@media print { .doc-scroll { overflow: visible !important; } }

/* ------------------------------------------------------------------
 * TABLET (769–1024): slimmer sidebar so content keeps its room
 * ------------------------------------------------------------------ */
@media (min-width: 769px) and (max-width: 1024px) {
    :root { --sidebar-w: 224px; }
    .main-content { padding: 18px; }
    #mainSidebar .sidebar-menu a { font-size: 13px; }
}

/* ------------------------------------------------------------------
 * PHONES / MOBILE WEBVIEW / PWA (≤768)
 * ------------------------------------------------------------------ */
@media (max-width: 768px) {
    /* Drawer + hamburger sit clear of the status bar / notch */
    #mainSidebar {
        height: 100vh;
        height: 100dvh;
        padding-top: var(--sa-top);
        padding-bottom: var(--sa-bottom);
        padding-left: var(--sa-left);
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }
    .sidebar-close-btn { top: calc(14px + var(--sa-top)); }
    .mobile-toggle {
        top: calc(12px + var(--sa-top));
        left: calc(12px + var(--sa-left));
        width: 44px;
        height: 44px;
    }
    .main-content {
        padding-top: calc(66px + var(--sa-top));
        padding-right: calc(14px + var(--sa-right));
        padding-bottom: calc(70px + var(--sa-bottom));
        padding-left: calc(14px + var(--sa-left));
    }
    .main-content.no-sidebar { padding-top: calc(16px + var(--sa-top)); }

    /* iOS zooms the whole page when a field under 16px gets focus */
    .form-control, .form-select, .form-control-sm, .form-select-sm,
    input[type="text"], input[type="search"], input[type="email"], input[type="tel"],
    input[type="number"], input[type="password"], input[type="date"], textarea, select {
        font-size: 16px;
    }
    /* Comfortable touch targets */
    .btn { min-height: 38px; }
    .btn-sm, .btn-group-sm > .btn { min-height: 36px; min-width: 36px; padding: .35rem .6rem; font-size: .8rem; }
    .form-check-input { width: 1.2em; height: 1.2em; }
    .page-link { min-width: 38px; min-height: 38px; display: inline-flex; align-items: center; justify-content: center; }

    .top-bar { gap: 6px; }
    .page-title { min-width: 0; overflow-wrap: anywhere; }

    /* DataTables chrome stacks and fills the width */
    div.dataTables_wrapper div.dataTables_length,
    div.dataTables_wrapper div.dataTables_filter,
    div.dataTables_wrapper div.dataTables_info,
    div.dataTables_wrapper div.dataTables_paginate { text-align: left; width: 100%; }
    div.dataTables_wrapper div.dataTables_filter label { display: flex; align-items: center; gap: .5rem; width: 100%; margin: 0; }
    div.dataTables_wrapper div.dataTables_filter input { flex: 1 1 auto; width: auto !important; margin-left: 0 !important; }
    div.dataTables_wrapper div.dataTables_info { white-space: normal; padding-top: .25rem; }
    div.dataTables_wrapper div.dataTables_paginate ul.pagination { flex-wrap: wrap; justify-content: center; }

    /* Row-actions column stays reachable while the table scrolls sideways */
    .table-responsive > table:not(.dataTable) td.sticky-actions,
    .table-scroll > table:not(.dataTable) td.sticky-actions,
    .table-responsive > table:not(.dataTable) th.sticky-actions,
    .table-scroll > table:not(.dataTable) th.sticky-actions {
        position: sticky;
        right: 0;
        z-index: 2;
        background: #fff;
        box-shadow: -6px 0 8px -6px rgba(0, 0, 0, .25);
    }
    .table-responsive > table:not(.dataTable) th.sticky-actions,
    .table-scroll > table:not(.dataTable) th.sticky-actions { background: var(--primary, #1a472a); }
}

@media (max-width: 575.98px) {
    /* Date / term strip is redundant on a phone — keep the page title only */
    .top-bar .d-flex > small:nth-child(-n+2) { display: none; }
    .stat-card { padding: 16px; }
    .table-container { padding: 14px; }
    .alert { font-size: 14px; }
}

/* ------------------------------------------------------------------
 * INSTALLED PWA (standalone / fullscreen / minimal-ui)
 * No browser chrome, so keep content clear of system bars and don't let
 * rubber-band scrolling or text selection fight the app feel.
 * ------------------------------------------------------------------ */
@media (display-mode: standalone), (display-mode: fullscreen), (display-mode: minimal-ui) {
    body { overscroll-behavior-y: contain; }
    .btn, .sidebar-menu a, .sb-group-toggle, .mobile-toggle { -webkit-user-select: none; user-select: none; }
    @media (min-width: 769px) {
        .main-content { padding-top: calc(24px + var(--sa-top)); }
    }
}

/* Landscape phones: the hamburger/top gap is wasted space */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
    .main-content { padding-top: calc(58px + var(--sa-top)); }
    .modal-dialog:not(.modal-fullscreen):not([class*="modal-fullscreen-"]) .modal-content { max-height: calc(100dvh - .5rem); }
}

/* Respect users who ask for less motion */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
}

@media print {
    .mobile-toggle, .sidebar-backdrop { display: none !important; }
}
