/* ==========================================================================
   viewport.css - responsive LAYOUT layer for the auth server (loaded LAST,
   after style.css / responsive.css / site.css).

   Owns layout on small and short screens: header height, page gutters, form
   column width, QR size and touch targets. Colours, fonts and the desktop
   look stay in style.css / responsive.css / site.css.
   style.css, responsive.css and Bootstrap utilities (mb-4, px-4, py-2 ...)
   use !important in places, so rules here use an `html body` prefix and
   !important only where they have to beat one of those.

   Breakpoints (only these, min-/max- syntax):
     width   phone    max-width: 599.98px
             tablet   600px - 1023.98px
             desktop  min-width: 1024px
     height  short    max-height: 800px   (Tesla 700-784, MBUX 660-780, 1366x768)
             xshort   max-height: 560px   (MBUX 380-510, phone landscape)
             xxshort  max-height: 420px   (MBUX 380-440 with toolbars)
     touch   (pointer: coarse) -> phones, tablets and car screens

   "Layout scope" = (max-width: 1023.98px), (max-height: 800px).
   Desktop at >= 1024 x >= 801 is intentionally left untouched (apart from
   the coarse-pointer tap-target minimums, which only apply to touch screens).

   Tokens:
     --vl-app-h        usable viewport height: 100vh -> 100dvh.
     --vl-header-h     height of the single-row compact header.
     --vl-gutter       space between the viewport edge and page content on
                       phones.
     --vl-form-w       max width of the form column below 1024px.
     --vl-qr-w         max width of the login column when the QR text and
                       code sit side by side (wide-and-short screens).
     --vl-qr-scale     scale factor of the 310px QR box. The QR library
                       renders a 300x300 <svg> without a viewBox, so it is
                       scaled with a transform instead of width/height
                       (which would crop it).
     --vl-touch        minimum hit area on touch screens and in cars.
     --vl-lang-tap     width of each TR / EN tap target.
     --vl-lang-w       header space reserved for the TR / EN switch (the
                       two taps, the " / " and ~24px slack for text scaling).

   Old car browsers (Tesla legacy ~ Chromium 79): no flexbox gap, inset, :is,
   :where, :has, container queries, media range syntax, nesting or
   aspect-ratio in this file. dvh only with a vh fallback.
   ========================================================================== */


/* --------------------------------------------------------------------------
   1. Tokens
   -------------------------------------------------------------------------- */

:root {
    --vl-app-h: 100vh;
    --vl-header-h: 52px;
    --vl-gutter: 16px;
    --vl-form-w: 560px;
    --vl-qr-w: 760px;
    --vl-qr-scale: 1;
    --vl-touch: 44px;
    --vl-lang-tap: 32px;
    --vl-lang-w: calc(2 * var(--vl-lang-tap) + 40px);
}

@supports (height: 100dvh) {
    :root {
        --vl-app-h: 100dvh;
    }
}

@media (pointer: coarse) {
    :root {
        --vl-lang-tap: var(--vl-touch);
    }
}

/* QR steps: tabs + QR must fit between the header and the bottom edge. */
@media (max-height: 560px) {
    :root {
        --vl-header-h: 48px;
        --vl-qr-scale: 0.8;   /* 248px box, 240px code */
    }
}

@media (max-height: 420px) {
    :root {
        --vl-qr-scale: 0.7;   /* 217px box, 210px code */
    }
}

@media (max-height: 340px) {
    :root {
        --vl-qr-scale: 0.6;   /* 186px box, 180px code */
    }
}


/* --------------------------------------------------------------------------
   2. Header: one compact row, TR / EN inside the navbar row
   -------------------------------------------------------------------------- */

@media (max-width: 1023.98px), (max-height: 800px) {
    html body .header {
        padding-bottom: 0;
    }

    html body .header > div {
        position: relative;
    }

    /* The language row is lifted into the navbar row, at its right edge. */
    html body .header .header-top {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        z-index: 2;
        height: var(--vl-header-h);
        margin: 0 auto;
        padding: 0 calc(var(--bs-gutter-x, 1.5rem) * 0.5) !important;
        white-space: nowrap;
        pointer-events: none;
    }

    html body .header .header-top > .lang-text {
        margin: 0;
        padding: 0;
        white-space: nowrap;
        pointer-events: auto;
    }

    html body .header .header-top > .lang-text > .lang-text {
        display: inline-block;
        min-width: var(--vl-lang-tap);
        line-height: var(--vl-header-h);
        margin: 0;
        padding: 0;
        text-align: center;
    }

    html body .header .navbar {
        padding-top: 0 !important;
        padding-bottom: 0 !important;
    }

    html body .header .navbar > .container {
        min-height: var(--vl-header-h);
        padding-right: calc(var(--bs-gutter-x, 1.5rem) * 0.5 + var(--vl-lang-w));
    }

    /* The brand fixes the first row's height, also while the menu is open. */
    html body .header .navbar-brand {
        display: flex;
        align-items: center;
        min-height: var(--vl-header-h);
        padding-top: 0;
        padding-bottom: 0;
    }

    html body .header .navbar .logo {
        width: auto !important;
        height: 36px;
        margin-top: 0;
    }

    /* Collapsed menu: stays inside the viewport and scrolls. */
    html body .header .navbar-collapse {
        max-height: calc(100vh - var(--vl-header-h));
        max-height: calc(var(--vl-app-h) - var(--vl-header-h));
        overflow-y: auto;
    }

    /* Bootstrap animates the height with overflow hidden; no scrollbar flash. */
    html body .header .navbar-collapse.collapsing {
        overflow: hidden;
    }
}

@media (pointer: coarse) {
    html body .header .navbar-toggler {
        min-width: var(--vl-touch);
        min-height: var(--vl-touch);
    }

    html body .header .navbar-nav .nav-link {
        min-height: var(--vl-touch);
        padding-top: 11px;
        padding-bottom: 11px;
    }
}


/* --------------------------------------------------------------------------
   3. Page card and form column
   -------------------------------------------------------------------------- */

@media (max-width: 1023.98px), (max-height: 800px) {
    html body .login-container {
        margin-top: 8px;
        padding-bottom: 8px;
    }

    /* Duplicate page logo (the header already shows it): smaller. */
    html body .login-container > .row > .col-12:first-child {
        margin-top: 4px !important;
        margin-bottom: 4px !important;
    }

    html body .login-container .logo {
        padding: 8px 0;
    }

    html body #myTabContent > .tab-pane {
        margin-bottom: 8px !important;
    }

    /* Bare pages (AccessDenied, Error, Privacy) render straight into .wrapper. */
    html body .wrapper > h1,
    html body .wrapper > h2,
    html body .wrapper > h3,
    html body .wrapper > p,
    html body .wrapper > header {
        padding-left: var(--vl-gutter);
        padding-right: var(--vl-gutter);
        overflow-wrap: break-word;
    }
}

/* Login.cshtml nests the tab-content column inside the tab column (a stray
   `</<div>` swallows the closing tag), so it is 8/12 of the tab column.
   Below 1024px and in the side-by-side layout it fills its parent instead;
   short desktops keep the original width. */
@media (max-width: 1023.98px), (min-width: 600px) and (max-height: 560px) {
    html body .login-container .custom-col .custom-col {
        width: 100%;
        max-width: none;
        padding-left: 0;
        padding-right: 0;
    }
}

@media (max-height: 800px) {
    html body .login-container > .row > .col-12:first-child {
        display: none;
    }
}

@media (max-width: 1023.98px) {
    html body .login-container > .row > .custom-col {
        flex: 0 0 auto;
        width: 100% !important;
        max-width: var(--vl-form-w);
    }

    html body .profile-detail .row.w-50 {
        width: 100% !important;
    }

    html body .profile-detail p {
        overflow-wrap: break-word;
    }

    html body #myTab .nav-link {
        font-size: 0.9rem !important;
    }
}

@media (max-width: 599.98px) {
    /* Content sits --vl-gutter from the viewport edges:
       8px card margin + 8px column padding. */
    html body .login-container {
        --bs-gutter-x: 16px;
        width: auto;
        max-width: none;   /* Bootstrap caps .container at 540px from 576px */
        margin-left: 8px;
        margin-right: 8px;
    }

    html body .login-container > .row {
        --bs-gutter-x: 16px;
    }

    /* The 304px reCAPTCHA checkbox must fit a 328px column. */
    html body .recaptcha-box {
        padding-right: 0;
    }

    /* OTP modal: six boxes shrink to the dialog width. */
    html body .verification-inputs .custom-input {
        flex: 1 1 0;
        width: auto;
        min-width: 0;
        max-width: 62px;
        height: 56px;
        padding: 4px;
    }
}


/* --------------------------------------------------------------------------
   4. Login tabs and QR code
   -------------------------------------------------------------------------- */

@media (max-width: 1023.98px), (max-height: 800px) {
    html body #myTab .nav-link {
        padding: 0.5rem 0.25rem;
        line-height: 1.2;
    }

    html body #divLoginByQR .login-paragraph {
        margin-top: 4px;
        margin-bottom: 0;
    }

    html body #divLoginByQR > .d-flex {
        flex: none;
    }

    html body #divQR {
        flex: none;
        width: calc(310px * var(--vl-qr-scale));
        height: calc(310px * var(--vl-qr-scale));
        margin-top: 12px !important;
    }

    html body #divQR #imgQR {
        width: 310px;
        height: 310px;
        transform: scale(var(--vl-qr-scale));
        transform-origin: 0 0;
    }

    html body #imgQR svg,
    html body #imgQR canvas {
        display: block;
    }
}

/* Wide and short (MBUX, phone landscape): instructions left, QR right. */
@media (min-width: 600px) and (max-height: 560px) {
    html body .login-container {
        margin-top: 6px;
    }

    html body .login-container > .row > .vl-login-col {
        flex: 0 0 auto;
        width: 100% !important;
        max-width: var(--vl-qr-w);
    }

    /* Only the tab bar and the QR tab use the wider column. */
    html body #home-tab-pane,
    html body #profile-tab-pane {
        max-width: var(--vl-form-w);
        margin-left: auto;
        margin-right: auto;
    }

    /* TempData alerts above the tabs: one compact line or two. */
    html body .vl-login-col > .alert {
        margin-bottom: 6px !important;
        padding: 4px 12px;
        font-size: 0.8rem;
        line-height: 1.25;
    }

    html body #qr-tab-pane {
        padding-top: 6px !important;
        padding-bottom: 0 !important;
    }

    html body #divLoginByQR {
        display: flex;
        align-items: center;
        text-align: left;
    }

    html body #divLoginByQR .login-paragraph {
        flex: 1 1 auto;
        margin: 0 24px 0 8px;
    }

    html body #divQR {
        margin-top: 0 !important;
    }
}


/* Tab labels never wrap: a wrapped label ("Sign in with QR Code" at
   1024-1199px wide and on phones) made its pill taller than the others.
   Below 1200px the tabs size to their labels instead of equal thirds, so a
   long label borrows width from the short ones; phones also scale the font
   with the viewport. The ellipsis is only a last resort for longer
   translations. At >= 1200px the equal thirds already fit, so nothing moves. */
html body #myTab .nav-link {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

@media (max-width: 1199.98px) {
    html body #myTab > .nav-item {
        flex: 1 1 auto;
        min-width: 0;
    }
}

@media (max-width: 599.98px) {
    html body #myTab .nav-link {
        font-size: clamp(12px, 3.6vw, 0.9rem) !important;
    }
}


/* --------------------------------------------------------------------------
   5. Touch targets (phones, tablets, cars)
   -------------------------------------------------------------------------- */

@media (pointer: coarse) {
    html body #myTab .nav-link {
        min-height: var(--vl-touch);
    }

    html body .form-control,
    html body .form-select,
    html body .input-group-text,
    html body .custom-active-btn,
    html body .custom-inactive-btn,
    html body .modal-btn,
    html body .btn {
        min-height: var(--vl-touch);
    }

    html body .help {
        display: inline-block;
        min-height: var(--vl-touch);
        line-height: var(--vl-touch);
    }
}
