/* Viewport-fitting responsive layout for TopChess Analysis (/app/).
 * Eliminates vertical scrolling on desktop, tablet, and mobile viewports
 * while keeping board, evaluation bar, player headers, and analysis panel usable.
 */

:root {
    --tc-analysis-top: 88px;
    --tc-analysis-board-col-h: 340px;
}

body[data-mobile-page="analysis"] {
    padding-top: var(--tc-analysis-top) !important;
    overflow-x: hidden;
}

body[data-mobile-page="analysis"] main {
    margin: 6px 12px 8px !important;
    padding: 0 !important;
}

/* Sync evaluation bar visual alignment and crisp sizing */
body[data-mobile-page="analysis"] [data-sentry-component="EvaluationBar"] {
    flex-shrink: 0;
    width: 26px !important;
    min-width: 26px !important;
}

body[data-mobile-page="analysis"] [data-sentry-component="EvaluationBar"] .MuiTypography-root {
    font-size: 0.7rem !important;
    line-height: 1.2 !important;
}

/* ============================================================
   Desktop & Tablet Landscape (>= 901px): Side-by-side 100dvh fit
   ============================================================ */
@media (min-width: 901px) {
    body[data-mobile-page="analysis"] {
        overflow-y: hidden;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="GameAnalysis"] {
        display: grid !important;
        grid-template-columns: auto minmax(330px, 1fr);
        align-items: stretch !important;
        justify-content: center !important;
        gap: 16px !important;
        max-width: 1680px;
        margin: 0 auto !important;
        height: calc(100dvh - var(--tc-analysis-top) - 16px);
    }

    /* Board column: square board + 2 compact PlayerHeaders + eval bar fit inside available height */
    body[data-mobile-page="analysis"] [data-sentry-component="Board"] {
        grid-column: 1;
        grid-row: 1;
        align-self: center;
        width: min(
            calc(100dvh - var(--tc-analysis-top) - 58px),
            calc(100vw - 400px),
            820px
        ) !important;
        max-width: 100%;
        flex-wrap: nowrap !important;
        align-items: center !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="Board"] > :not([data-sentry-component="EvaluationBar"]) {
        row-gap: 6px !important;
        padding-left: 10px !important;
        min-width: 0;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PlayerHeader"] {
        min-height: 32px;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PlayerHeader"] .MuiAvatar-root {
        width: 30px !important;
        height: 30px !important;
        font-size: 0.85rem !important;
    }

    /* Right analysis panel: exact viewport height, internal scroll for long move lists */
    body[data-mobile-page="analysis"] [data-sentry-component="GameAnalysis"] > [data-sentry-element="Grid"]:has([data-sentry-component="PanelToolBar"]) {
        grid-column: 2;
        grid-row: 1;
        width: 100% !important;
        max-width: 100% !important;
        height: calc(100dvh - var(--tc-analysis-top) - 16px) !important;
        max-height: calc(100dvh - var(--tc-analysis-top) - 16px) !important;
        padding: 12px 14px 10px !important;
        row-gap: 8px !important;
        overflow: hidden !important;
        box-sizing: border-box;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PanelHeader"] {
        row-gap: 8px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PanelHeader"] h5 {
        font-size: 1.2rem !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="GameAnalysis"] > [data-sentry-element="Grid"]:has([data-sentry-component="PanelToolBar"]) hr.MuiDivider-root {
        margin-top: 8px !important;
        margin-bottom: 6px !important;
    }

    /* Allow tab body (engine lines, move list, eval graph) to scroll internally without pushing toolbar out */
    body[data-mobile-page="analysis"] [role="tabpanel"]:not([hidden]) {
        flex: 1 1 auto !important;
        min-height: 0 !important;
        overflow-y: auto !important;
        align-content: flex-start !important;
        margin-top: 0 !important;
        margin-bottom: 0 !important;
    }

    /* Keep PanelToolBar always visible at bottom/top of panel */
    body[data-mobile-page="analysis"] [data-sentry-component="GameAnalysis"] > [data-sentry-element="Grid"]:has([data-sentry-component="PanelToolBar"]) > :has(> [data-sentry-component="PanelToolBar"]),
    body[data-mobile-page="analysis"] [data-sentry-component="PanelToolBar"] {
        flex-shrink: 0;
    }

    /* Position Engine Settings FAB neatly in top-right of the analysis card (>=1200px, where toolbar is at bottom) */
    body[data-mobile-page="analysis"] .MuiFab-root {
        position: fixed !important;
        top: calc(var(--tc-analysis-top) + 14px) !important;
        right: max(22px, calc((100vw - 1680px) / 2 + 16px)) !important;
        bottom: auto !important;
        z-index: 20 !important;
        min-height: 34px !important;
        height: 34px !important;
        padding: 0 12px !important;
        font-size: 0.72rem !important;
    }
}

/* Tablet landscape / compact desktop (901px–1199px): MUI puts toolbar at TOP, so park FAB in bottom-right corner */
@media (min-width: 901px) and (max-width: 1199px) {
    body[data-mobile-page="analysis"] [data-sentry-component="Board"] {
        width: min(
            calc(100dvh - var(--tc-analysis-top) - 58px),
            calc(100vw - 380px)
        ) !important;
    }

    body[data-mobile-page="analysis"] .MuiFab-root {
        top: auto !important;
        bottom: 16px !important;
        right: 22px !important;
        min-width: 38px !important;
        width: 38px !important;
        height: 38px !important;
        padding: 0 !important;
        font-size: 0 !important;
        gap: 0 !important;
    }
}

/* Narrow lg desktop (1200px–1260px): compact icon-only FAB so it never crowds Game Analysis title */
@media (min-width: 1200px) and (max-width: 1260px) {
    body[data-mobile-page="analysis"] .MuiFab-root {
        min-width: 36px !important;
        width: 36px !important;
        padding: 0 !important;
        font-size: 0 !important;
        gap: 0 !important;
    }
}

/* Short desktop screens (e.g. 1280x720, 1366x768): slightly tighter header spacing */
@media (min-width: 901px) and (max-height: 780px) {
    body[data-mobile-page="analysis"] main {
        margin: 4px 10px 6px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="GameAnalysis"] {
        height: calc(100dvh - var(--tc-analysis-top) - 10px);
        gap: 12px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="Board"] {
        width: min(
            calc(100dvh - var(--tc-analysis-top) - 46px),
            calc(100vw - 390px)
        ) !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PlayerHeader"] {
        min-height: 26px;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PlayerHeader"] .MuiAvatar-root {
        width: 24px !important;
        height: 24px !important;
        font-size: 0.75rem !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="GameAnalysis"] > [data-sentry-element="Grid"]:has([data-sentry-component="PanelToolBar"]) {
        height: calc(100dvh - var(--tc-analysis-top) - 10px) !important;
        max-height: calc(100dvh - var(--tc-analysis-top) - 10px) !important;
        padding: 8px 12px 6px !important;
        row-gap: 6px !important;
    }
}

/* ============================================================
   Mobile Portrait (<= 900px): Fit board + compact panel in 100dvh
   ============================================================ */
@media (max-width: 900px) {
    body[data-mobile-page="analysis"] main {
        margin: 4px 8px 6px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="GameAnalysis"] {
        gap: 6px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="Board"] {
        width: min(
            100%,
            calc(100dvh - var(--tc-analysis-top) - 320px)
        ) !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="Board"] > :not([data-sentry-component="EvaluationBar"]) {
        padding-left: 6px !important;
        row-gap: 3px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="EvaluationBar"] {
        width: 20px !important;
        min-width: 20px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="EvaluationBar"] .MuiTypography-root {
        font-size: 0.62rem !important;
    }

    /* Constrain panel to remaining viewport height with internal scrolling */
    body[data-mobile-page="analysis"] [data-sentry-component="GameAnalysis"] > [data-sentry-element="Grid"]:has([data-sentry-component="PanelToolBar"]) {
        height: auto !important;
        max-height: max(180px, calc(100dvh - var(--tc-analysis-top) - var(--tc-analysis-board-col-h) - 18px)) !important;
        overflow-y: auto !important;
        padding: 6px 8px !important;
        row-gap: 4px !important;
        box-sizing: border-box;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PanelHeader"] h5 {
        font-size: 0.95rem !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PanelToolBar"] .MuiIconButton-root {
        min-width: 38px !important;
        min-height: 38px !important;
        padding: 5px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PanelHeader"] button {
        min-height: 38px !important;
        padding-top: 4px !important;
        padding-bottom: 4px !important;
    }
}

/* Very short portrait phones (e.g. 375x667 iPhone SE) */
@media (max-width: 900px) and (max-height: 700px) {
    body[data-mobile-page="analysis"] [data-sentry-component="Board"] {
        width: min(
            100%,
            calc(100dvh - var(--tc-analysis-top) - 315px)
        ) !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PlayerHeader"] {
        min-height: 22px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PlayerHeader"] .MuiAvatar-root {
        width: 20px !important;
        height: 20px !important;
        font-size: 11px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PanelHeader"] h5 {
        font-size: 0.88rem !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PanelHeader"] button {
        min-height: 34px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="PanelToolBar"] .MuiIconButton-root {
        min-width: 34px !important;
        min-height: 34px !important;
        padding: 4px !important;
    }
}

/* ============================================================
   Mobile Landscape (<= 900px & max-height 500px): Side-by-side
   ============================================================ */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
    body[data-mobile-page="analysis"] {
        overflow-y: hidden;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="GameAnalysis"] {
        grid-template-columns: 40px auto minmax(200px, 1fr) !important;
        align-items: start !important;
        gap: 6px !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="Board"] {
        width: min(
            100%,
            calc(100dvh - var(--tc-analysis-top) - 62px)
        ) !important;
    }

    body[data-mobile-page="analysis"] [data-sentry-component="GameAnalysis"] > [data-sentry-element="Grid"]:has([data-sentry-component="PanelToolBar"]) {
        grid-column: 3 !important;
        grid-row: 1 !important;
        height: calc(100dvh - var(--tc-analysis-top) - 10px) !important;
        max-height: calc(100dvh - var(--tc-analysis-top) - 10px) !important;
        overflow-y: auto !important;
    }

    body[data-mobile-page="analysis"] .MuiFab-root {
        min-width: 40px !important;
        width: 40px !important;
        height: 40px !important;
        min-height: 40px !important;
    }
}
