/*
 * Foundry VTT v14 - iOS / Mobile Join Screen
 *
 * Responsive override for Foundry's stock Join Game screen.
 * Preserves Foundry templates, branding, artwork, and styling.
 *
 * Intended to be injected only on /join by Nginx.
 */

@media screen and (max-width: 700px) {

    /* =========================================================
       MOBILE DESIGN TOKENS
       ========================================================= */

    body.join {
        --max-width: 100%;

        /* Consistent mobile sizing */
        --mobile-control-height: 52px;
        --mobile-control-font-size: 16px;
        --mobile-card-gap: 12px;
        --mobile-form-gap: 12px;
        --mobile-card-padding: 16px;
        --mobile-side-padding: 12px;

        box-sizing: border-box;

        width: 100%;
        min-width: 0;
        min-height: 100dvh;

        margin: 0;

        padding:
            max(12px, env(safe-area-inset-top))
            max(var(--mobile-side-padding), env(safe-area-inset-right))
            max(16px, env(safe-area-inset-bottom))
            max(var(--mobile-side-padding), env(safe-area-inset-left));

        overflow-x: hidden;
        overflow-y: auto;

        gap: var(--mobile-card-gap);

        justify-content: flex-start;
        align-items: stretch;

        -webkit-text-size-adjust: 100%;
    }

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


    /* =========================================================
       BACKGROUND
       ========================================================= */

    body.join #main-background {
        background-position: center center;
        background-size: cover;
    }


    /* =========================================================
       WORLD / FOUNDRY HEADER
       ========================================================= */

    body.join #main-header {
        flex: 0 0 auto;

        width: 100%;
        min-width: 0;

        height: auto;
        min-height: 104px;

        padding: 10px 8px;

        background-position: center center;
        background-size: contain;
    }

    body.join #main-header h1 {
        max-width: 100%;

        margin: 0;

        font-size: clamp(24px, 8vw, 40px);
        line-height: 1.1;

        text-align: center;
        overflow-wrap: anywhere;

        filter: drop-shadow(0 0 4px black);
        text-shadow: 1px 1px 4px #000;
    }


    /* =========================================================
       JOIN APPLICATION

       Mobile order:
         1. Join Game
         2. Game Details
         3. World Description
         4. Return to Setup
       ========================================================= */

    body.join #join-game {
        display: flex !important;
        flex: 0 0 auto;
        flex-direction: column;

        width: 100%;
        max-width: 100%;
        min-width: 0;

        gap: var(--mobile-card-gap);

        margin: 0;
    }

    /*
     * Remove Foundry's desktop grid placement.
     */

    body.join #join-game > * {
        width: 100%;
        max-width: 100%;
        min-width: 0;

        grid-column: auto !important;
        grid-row: auto !important;
    }


    /* =========================================================
       MOBILE CARD ORDER
       ========================================================= */

    body.join #join-game-form {
        order: 1;
    }

    body.join #join-game-details {
        order: 2;
    }

    body.join #join-game-world {
        order: 3;

        grid-column: auto !important;
        grid-row: auto !important;
    }

    body.join #join-game-setup {
        order: 4;
    }


    /* =========================================================
       FOUNDRY CARDS
       ========================================================= */

    body.join #join-game .application.framed {
        position: relative;

        width: 100%;
        max-width: none;
        min-width: 0;

        margin: 0;
        padding: var(--mobile-card-padding);
    }

    body.join #join-game h2.divider {
        margin: 0 0 12px;

        font-size: 20px;
        line-height: 1.2;

        text-align: center;
    }


    /* =========================================================
       JOIN GAME FORM
       ========================================================= */

    body.join #join-game-form {
        padding: var(--mobile-card-padding);
    }

    body.join #join-game-form .form-group {
        display: flex;
        align-items: center;

        width: 100%;
        min-width: 0;

        gap: 8px;

        margin: 0 0 var(--mobile-form-gap);
    }


    /* ---------------------------------------------------------
       USER / PASSWORD ICONS
       --------------------------------------------------------- */

    body.join #join-game-form .form-group > label.icon {
        flex: 0 0 36px;

        width: 36px;
        min-width: 36px;

        margin: 0;

        font-size: 20px;
        line-height: 1;

        text-align: center;
    }


    /* ---------------------------------------------------------
       FIELD CONTAINER
       --------------------------------------------------------- */

    body.join #join-game-form .form-fields {
        flex: 1 1 auto;

        width: auto;
        min-width: 0;
    }


    /* =========================================================
       PRIMARY iOS CONTROLS

       Username, password, and Join Game all use the exact
       same control height.
       ========================================================= */

    body.join #join-game-form select,
    body.join #join-game-form input,
    body.join #join-game-form .form-footer button {
        box-sizing: border-box;

        height: var(--mobile-control-height);
        min-height: var(--mobile-control-height);
        max-height: var(--mobile-control-height);

        font-size: var(--mobile-control-font-size);
        line-height: 1.2;
    }


    /* ---------------------------------------------------------
       USERNAME / PASSWORD
       --------------------------------------------------------- */

    body.join #join-game-form select,
    body.join #join-game-form input {
        display: block;

        width: 100%;
        max-width: 100%;
        min-width: 0;

        margin: 0;
        padding: 0 12px;
    }

    body.join #join-game-form select {
        text-overflow: ellipsis;
        white-space: nowrap;
    }


    /* =========================================================
       JOIN BUTTON
       ========================================================= */

    body.join #join-game-form .form-footer {
        width: 100%;

        margin: 4px 0 0;
    }

    body.join #join-game-form .form-footer button {
        width: 100%;

        margin: 0;
        padding: 0 16px;

        font-weight: 600;
    }

    body.join #join-game-form .form-footer button i {
        margin-right: 6px;
    }


    /* =========================================================
       GAME DETAILS
       ========================================================= */

    body.join #join-game-details .form-group {
        display: flex;
        align-items: center;

        width: 100%;
        min-width: 0;

        gap: 8px;

        margin: 0 0 8px;
    }

    body.join #join-game-details .form-group:last-child {
        margin-bottom: 0;
    }

    body.join #join-game-details .form-group > label {
        flex: 1 1 auto;

        min-width: 0;

        font-size: 14px;
        line-height: 1.3;
    }

    body.join #join-game-details .form-fields {
        flex: 0 0 auto;

        text-align: right;
        white-space: nowrap;
    }

    body.join #join-game-details .form-fields > span {
        flex: none;
    }

    body.join #next-tz {
        display: block;

        margin-top: 2px;

        font-size: 12px;
        line-height: 1.2;

        opacity: 0.75;
    }


    /* =========================================================
       WORLD DESCRIPTION
       ========================================================= */

    body.join #join-game-world {
        min-height: 0;
        height: auto;
        overflow: visible;
    }

    body.join #world-description {
        width: 100%;
        max-width: 100%;

        height: auto;
        max-height: none;

        overflow: visible;
        overflow-warp anywhere;

    }

    body.join #world-description img,
    body.join #world-description video,
    body.join #world-description iframe {
        display: block;

        max-width: 100%;
        height: auto;
    }

    body.join #world-description table {
        width: 100%;
        max-width: 100%;
    }


    /* =========================================================
       RETURN TO SETUP

       Secondary action, but still use a comfortable touch
       target and the same visual control height.
       ========================================================= */

    body.join #join-game-setup {
        margin-top: 2px;
    }

    body.join #join-game-setup .form-group {
        width: 100%;
        min-width: 0;
    }

    body.join #join-game-setup .form-fields {
        width: 100%;
        min-width: 0;
    }

    body.join #join-game-setup input,
    body.join #join-game-setup .form-footer button {
        box-sizing: border-box;

        height: var(--mobile-control-height);
        min-height: var(--mobile-control-height);
        max-height: var(--mobile-control-height);

        font-size: var(--mobile-control-font-size);
    }

    body.join #join-game-setup input {
        width: 100%;
        max-width: 100%;

        padding: 0 12px;
    }

    body.join #join-game-setup .form-footer {
        width: 100%;
    }

    body.join #join-game-setup .form-footer button {
        width: 100%;

        margin: 0;
        padding: 0 16px;
    }


    /* =========================================================
       AUTOCOMPLETE
       ========================================================= */

    body.join #autocomplete {
        width: 100%;

        max-width: calc(
            100vw -
            max(24px, env(safe-area-inset-left)) -
            max(24px, env(safe-area-inset-right))
        );
    }


    /* =========================================================
       FOUNDRY WATERMARK / VERSION
       ========================================================= */

    body.join #watermark {
        flex: 0 0 auto;

        width: 100%;
        max-width: 100%;

        margin-top: 2px;

        padding-bottom: env(safe-area-inset-bottom);
    }

    body.join #watermark #software-version {
        font-size: 13px;
        line-height: 1.25;
    }


    /* =========================================================
       MINIMAL JOIN THEME
       ========================================================= */

    body.join.join-theme-minimal {
        --max-width: 100%;
    }

    body.join.join-theme-minimal #main-header {
        flex: 0 0 auto;

        min-height: 88px;
    }

    body.join.join-theme-minimal #join-game {
        display: flex !important;
        flex: 0 0 auto;
        flex-direction: column;
    }


    /* =========================================================
       LANDSCAPE PHONE

       Conserve vertical space while retaining the same
       touch-target dimensions.
       ========================================================= */

    @media (orientation: landscape) and (max-height: 500px) {

        body.join {
            padding-top: max(8px, env(safe-area-inset-top));

            gap: 8px;
        }

        body.join #main-header {
            min-height: 60px;

            padding: 4px 8px;
        }

        body.join #main-header h1 {
            font-size: clamp(22px, 5vw, 32px);
        }

        body.join #join-game {
            gap: 8px;
        }

    }
}
