/* ==============================================================
   GAMEPLAY UI SHELL — layout, phases, battle, VN overlays
   ============================================================== */

.game-layout {
    display: grid;
    grid-template-rows: auto auto 1fr;
    grid-template-areas:
        "header"
        "hud"
        "main";
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
    width: 100%;
    max-width: 1400px;
    margin: 0 auto;
    padding: 0.65rem 0.85rem max(0.65rem, env(safe-area-inset-bottom));
    gap: 0.55rem;
    box-sizing: border-box;
    position: relative;
    z-index: 10;
}

.game-layout.game-layout--no-hud {
    grid-template-rows: auto 1fr;
    grid-template-areas:
        "header"
        "main";
}

.game-layout .game-header {
    grid-area: header;
    flex-shrink: 0;
}

.game-layout .hud-wrapper {
    grid-area: hud;
    flex-shrink: 0;
    min-width: 0;
}

.game-layout .game-main {
    grid-area: main;
    min-height: 0;
    min-width: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Compact in-game header */
.game-header--play {
    padding: 0.45rem 0.75rem !important;
    gap: 0.4rem !important;
    flex-wrap: nowrap;
}

.game-header--play .game-header-brand {
    flex: 0 1 auto;
    min-width: 0;
    max-width: 42%;
}

.game-header--play .game-title {
    font-size: clamp(0.88rem, 2.2vw, 1.2rem);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.game-header--play .game-subtitle {
    display: none;
}

.game-header-toolbar {
    display: none;
}

.game-header--play .game-header-actions--wide {
    flex: 1 1 auto;
    min-width: 0;
    justify-content: flex-end;
}

/* Phase root — fills game-main, anchors VN panels */
.game-phase-container {
    flex: 1 1 auto;
    min-height: 0;
    min-width: 0;
    width: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

.game-phase-container--overlay {
    overflow: visible;
    pointer-events: none;
}

.game-phase-container--overlay > * {
    pointer-events: auto;
}

/* HUD — compact stat strip */
.hud-container {
    border-radius: 12px;
}

.hud-prisoners-suffix {
    font-weight: 600;
}

/* ==============================================================
   BATTLE SCREEN
   ============================================================== */

.battle-screen-wrapper {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    width: 100%;
}

.battle-screen-card {
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    grid-template-areas:
        "header"
        "arena"
        "logs"
        "actions";
    gap: 0.65rem;
    padding: 1rem 1.15rem !important;
    border-radius: 16px;
    overflow: hidden;
}

.battle-screen-card .battle-header {
    grid-area: header;
}

.battle-screen-card .battle-arena {
    grid-area: arena;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    align-content: start;
}

.battle-screen-card .battle-logs-container {
    grid-area: logs;
}

.battle-screen-card .actions-grid {
    grid-area: actions;
    margin-top: 0;
}

.battle-header-title {
    color: var(--color-danger);
    font-size: clamp(1rem, 2.5vw, 1.3rem);
    margin: 0;
    line-height: 1.25;
}

.battle-header-message {
    font-size: 0.85rem;
    color: var(--color-warning);
    font-weight: 600;
    line-height: 1.35;
}

.battle-arena--compact {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    gap: 0.65rem;
    align-items: stretch;
}

/* ==============================================================
   CHOICE PANEL — anchored to phase container, not raw viewport
   ============================================================== */

.vn-choice-container {
    position: absolute;
    left: 50%;
    bottom: 0;
    transform: translateX(-50%);
    width: min(850px, 100%);
    max-height: min(52vh, 420px);
    z-index: 6000;
    display: flex;
    flex-direction: column;
    pointer-events: auto;
}

.vn-choice-card-wrapper {
    max-height: inherit;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 1.35rem 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    background: rgba(30, 41, 59, 0.97);
    border: 1px solid var(--border-glow);
    border-radius: 18px 18px 14px 14px;
    backdrop-filter: blur(24px);
    box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.65), 0 0 24px rgba(56, 189, 248, 0.15);
}

.vn-choice-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 0.65rem;
    flex-wrap: wrap;
    border-bottom: 1px solid var(--border-light);
    padding-bottom: 0.65rem;
}

.vn-choice-heading {
    color: var(--accent-primary);
    font-size: clamp(1.05rem, 2.5vw, 1.4rem);
    display: flex;
    align-items: center;
    gap: 0.45rem;
    margin: 0;
    line-height: 1.3;
}

.vn-choice-lead {
    font-size: clamp(0.95rem, 2vw, 1.1rem);
    color: var(--text-main);
    line-height: 1.5;
    margin: 0;
}

.choice-cards-grid {
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
    margin-top: 0.15rem;
}

.choice-interactive-card {
    min-height: 52px;
}

/* ==============================================================
   DIALOGUE — portrait + text coordination
   ============================================================== */

.dialogue-panel-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 5000;
    overflow: hidden;
}

.vn-dialogue-wrapper.mode-bottom-dialogue {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: min(950px, 100%);
    max-height: min(46vh, 380px);
    z-index: 5100;
}

.vn-portrait-frame {
    bottom: clamp(120px, 22vh, 200px);
}

/* ==============================================================
   TABLET
   ============================================================== */
@media (max-width: 1024px) {
    .battle-arena--compact {
        grid-template-columns: 1fr;
    }

    .battle-arena--compact .vs-divider-badge {
        margin: 0 auto;
    }

    .hud-container {
        grid-template-columns: repeat(5, minmax(88px, 1fr));
    }
}

/* ==============================================================
   MOBILE
   ============================================================== */
@media (max-width: 768px) {
    .game-layout {
        padding: 0.45rem 0.55rem max(0.5rem, env(safe-area-inset-bottom));
        gap: 0.4rem;
    }

    .game-header--play {
        flex-direction: column;
        align-items: stretch;
        padding: 0.4rem 0.55rem !important;
    }

    .game-header--play .game-header-brand {
        max-width: 100%;
    }

    .game-header--play .game-header-actions--wide {
        width: 100%;
        justify-content: stretch;
    }

    .game-header--play .game-header-actions--wide .quick-menu-bar {
        width: 100%;
    }

    .hud-container {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        gap: 0.5rem;
        padding: 0.5rem 0.65rem;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
    }

    .hud-container::-webkit-scrollbar {
        display: none;
    }

    .hud-item {
        flex: 0 0 auto;
        min-width: 108px;
    }

    .hud-item--wide {
        min-width: 130px;
    }

    .hud-prisoners-suffix {
        display: none;
    }

    .battle-screen-card {
        grid-template-rows: auto minmax(0, 1fr) auto auto;
        padding: 0.75rem 0.85rem !important;
        gap: 0.5rem;
        overflow: hidden;
    }

    .battle-header h2,
    .battle-header-title {
        font-size: 1.05rem !important;
    }

    .battle-arena--compact .vs-divider-badge {
        width: 36px;
        height: 36px;
        font-size: 0.85rem;
    }

    .fighter-card {
        padding: 0.75rem 1rem;
    }

    .fighter-name {
        font-size: 1rem;
    }

    .battle-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.35rem;
    }

    .actions-grid {
        grid-template-columns: 1fr !important;
        gap: 0.55rem;
    }

    .battle-logs-container {
        height: 64px;
        max-height: 64px;
        min-height: 64px;
        font-size: 0.8rem;
    }

    .vn-choice-container {
        max-height: min(58vh, 460px);
        width: 100%;
    }

    .vn-choice-card-wrapper {
        padding: 1rem 0.85rem;
        border-radius: 16px 16px 12px 12px;
    }

    .choice-interactive-card {
        padding: 0.75rem 0.85rem;
        gap: 0.65rem;
        min-height: 56px;
    }

    .choice-title {
        font-size: 0.95rem;
    }

    .choice-sub {
        font-size: 0.78rem;
    }

    .vn-portrait-frame {
        width: min(42vw, 180px);
        height: min(48vh, 280px);
        bottom: clamp(140px, 28vh, 220px);
        opacity: 0.92;
    }

    .vn-dialogue-wrapper.mode-bottom-dialogue {
        max-height: min(50vh, 340px);
    }

    .vn-dialogue-wrapper.mode-center-narrator {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 94%;
        max-height: min(70vh, 480px);
        overflow-y: auto;
    }

    .vn-nameplate {
        margin-left: 0.5rem;
    }
}

@media (max-width: 480px) {
    .game-header--play .game-title {
        font-size: 0.85rem;
    }

    .battle-logs-container {
        height: 64px !important;
        max-height: 64px !important;
        min-height: 64px !important;
    }

    .actions-grid .btn-fantasy {
        font-size: 0.88rem !important;
        padding: 0.75rem 0.85rem !important;
    }

    .vn-portrait-frame {
        width: min(38vw, 150px);
        height: min(40vh, 220px);
    }

    /* Hide portraits when bottom dialogue would overlap heavily */
    .game-phase-container--dialogue .vn-portrait-frame {
        display: none;
    }
}

@media (max-height: 520px) and (orientation: landscape) {
    .game-layout.game-layout--hud .hud-wrapper {
        display: none;
    }

    .game-layout.game-layout--hud {
        grid-template-rows: auto 1fr;
        grid-template-areas: "header" "main";
    }

    .vn-portrait-frame {
        display: none;
    }

    .vn-choice-container {
        max-height: 70vh;
    }
}
