/* Game: small-screen tuning, from large to small */
@media (max-width: 1100px) {
    .playing .connection {
        display: none;
    }
    /* Keep the floating clock clear of the extra co-op controls. */
    .playing:not(.mobile-layout) .top-actions,
    .playing:not(.mobile-layout) .session-right {
        gap: 4px;
    }
    .playing:not(.mobile-layout) .topbar .icon-button:not(.cheat-button) {
        width: 28px;
    }
}
@media (max-width: 600px) {
    .hero-actions .btn {
        flex: 1;
        padding: 12px 14px;
        font-size: 16px;
    }
    .topbar {
        gap: 4px;
        padding: 4px 6px 8px;
    }
    .top-actions,
    .session-right {
        gap: 3px;
    }
    .connection {
        display: none;
    }
    .icon-button {
        width: 28px;
        height: 28px;
    }
    .cheat-button {
        width: auto;
        padding-inline: 4px;
    }
    .controls-grid {
        grid-template-columns: minmax(0, 1fr);
        gap: 10px;
    }
    .modal-shade {
        padding: 12px;
    }
    .modal {
        max-height: calc(100svh - 24px);
    }
    .modal-body {
        padding: 18px 16px 20px;
    }
    .modal h2 {
        font-size: 22px;
    }
    .lobby-seat {
        gap: 7px;
        padding: 8px;
    }
    .lobby-seat strong {
        max-width: 93px;
        font-size: 11px;
    }
    .big-code {
        font-size: 22px;
        letter-spacing: 3px;
    }
}
/* Short windows: the funnel stays above the board; the side column holds state and tray. */
@media (max-height: 700px) {
    .playing .board-card {
        display: grid;
        grid-template-columns: minmax(0, 1fr) 76px;
        grid-template-rows: 32px 40px 23px minmax(38px, 1fr);
        column-gap: 5px;
    }
    .playing .board-head {
        grid-column: 1 / -1;
    }
    .playing .queue-funnel {
        grid-column: 1;
        grid-row: 2;
        height: 40px;
    }
    .playing .board-shell {
        grid-column: 1;
        grid-row: 3 / -1;
    }
    .playing .power-state {
        grid-column: 2;
        grid-row: 2;
        align-self: start;
    }
    .playing .power-state .combo {
        display: none;
    }
    .playing .tray-label {
        display: none;
    }
    .playing .power-tray {
        grid-column: 2;
        grid-row: 3 / -1;
        grid-template-columns: minmax(0, 1fr);
        grid-auto-rows: 34px;
        gap: 4px;
        align-self: start;
        height: auto;
        padding-top: 2px;
    }
    .playing .power-slot {
        height: 34px;
    }
    .playing .power-slot .power-name,
    .playing .power-slot .hotkey {
        display: none;
    }
}
@media (max-height: 500px) {
    .playing .topbar {
        min-height: 34px;
        padding-block: 2px 7px;
    }
    .playing .topbar .icon-button {
        height: 24px;
    }
    .playing .score-strip {
        flex-basis: 28px;
    }
    .playing .score-cell small {
        display: none;
    }
    .playing .score-cell strong {
        font-size: 14px;
    }
    .playing .boss-panel {
        grid-template-columns: 34px minmax(0, 1fr);
        grid-template-rows: 26px;
        row-gap: 0;
        padding-block: 2px;
    }
    .playing .boss-portrait {
        height: 26px;
    }
    .playing .boss-summary {
        grid-template-rows: 14px 10px;
        row-gap: 1px;
    }
    .playing .boss-heading h2 {
        font-size: 10px;
    }
    .playing .boss-health {
        height: 8px;
    }
    .playing .team-tabs {
        flex-basis: 28px;
    }
    .playing .arena-layout {
        padding: 3px;
    }
    .playing .board-card {
        grid-template-rows: 28px 32px 18px minmax(0, 1fr);
        padding: 4px;
    }
    .playing .board-head {
        height: 24px;
    }
    .playing .queue-funnel {
        height: 32px;
    }
    .playing .power-tray {
        grid-auto-rows: 28px;
        gap: 3px;
    }
    .playing .power-slot {
        height: 28px;
    }
}
@media (max-height: 360px) {
    .playing .app {
        grid-template-rows: auto minmax(0, 1fr) 20px;
    }
    .playing .topbar {
        min-height: 28px;
        padding: 2px 6px 6px;
    }
    .playing .topbar:after {
        height: 3px;
    }
    .playing .topbar .icon-button {
        height: 20px;
    }
    .playing .board-card,
    .playing .board-shell,
    .playing .boss-panel,
    .playing .score-strip {
        border-width: 1px;
    }
    .playing .board-card .board-head {
        margin-bottom: 1px;
    }
    .playing .score-strip {
        flex-basis: 24px;
    }
    .playing .boss-panel {
        grid-template-columns: 24px minmax(0, 1fr);
        grid-template-rows: 18px;
        padding-block: 1px;
    }
    .playing .boss-portrait {
        height: 18px;
    }
    .playing .boss-summary {
        grid-template-rows: 10px 7px;
    }
    .playing .boss-heading h2,
    .playing .boss-level,
    .playing .boss-detail {
        font-size: 8px;
        line-height: 1;
    }
    .playing .boss-health {
        height: 6px;
    }
    .playing .boss-contributors {
        display: none;
    }
    .playing .team-tabs {
        flex-basis: 24px;
    }
    /*
     * The funnel flattens into a shallow shelf and takes the header's row; the header
     * shrinks to avatar and score in the side column so the board keeps its height.
     */
    .playing .board-card {
        grid-template-rows: 22px minmax(0, 1fr);
        padding: 2px;
    }
    .playing .queue-funnel {
        grid-row: 1;
        height: 22px;
    }
    .playing .board-shell {
        grid-row: 2 / -1;
    }
    .playing .board-card .board-head {
        grid-column: 2;
        grid-row: 1;
        height: 22px;
        margin-bottom: 0;
    }
    .playing .board-head .avatar {
        width: 20px;
        height: 20px;
    }
    .playing .board-head .player-name,
    .playing .board-head .player-state,
    .playing .power-state {
        display: none;
    }
    .playing .power-tray {
        grid-row: 2 / -1;
        grid-auto-rows: 24px;
        gap: 2px;
        padding-top: 3px;
    }
    .playing .power-slot {
        height: 24px;
    }
}

@media (max-width: 900px), (pointer: coarse) {
    body:not(.playing) .app {
        padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
            env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
    }
    body:not(.playing) .topbar .wordmark {
        display: none;
    }
    body:not(.playing) .topbar .icon-button {
        min-width: 44px;
        min-height: 44px;
    }
    .modal-shade {
        padding: max(8px, env(safe-area-inset-top, 0px)) max(8px, env(safe-area-inset-right, 0px))
            max(8px, env(safe-area-inset-bottom, 0px)) max(8px, env(safe-area-inset-left, 0px));
    }
    .modal {
        max-height: 100%;
        overscroll-behavior: contain;
    }
    .modal-body {
        min-width: 0;
        overflow-wrap: anywhere;
    }
    .modal-actions {
        flex-wrap: wrap;
    }
    .modal-actions .btn {
        flex: 1 1 160px;
        min-width: 0;
        white-space: normal;
    }
    .copy-row {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .big-code {
        grid-column: 1 / -1;
    }
    .copy-row .btn {
        height: auto;
        padding: 8px;
        white-space: normal;
    }
    .input-row {
        flex-wrap: wrap;
    }
    .input-row input {
        flex: 1 1 130px;
        width: auto;
        min-width: 0;
    }
    .setting {
        flex-wrap: wrap;
        gap: 10px;
    }
    .setting label {
        flex: 1 1 160px;
        min-width: 0;
    }
    .setting .volume-row,
    .setting > select {
        flex: 1 1 100%;
        min-width: 0;
        max-width: 100%;
    }
    .volume-row input[type="range"] {
        flex: 1;
        min-width: 0;
        width: auto;
        min-height: 44px;
    }
    .volume-row output,
    .setting .switch {
        flex-shrink: 0;
    }
    .avatar-option {
        min-width: 44px;
        min-height: 44px;
    }
    .result-grid {
        grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
    }
    .result-grid > div {
        min-width: 0;
    }
    .modal .result-score {
        font-size: clamp(18px, 5vw, 38px);
        line-height: 1.2;
        overflow-wrap: anywhere;
    }
    .result-grid strong {
        font-size: 14px;
    }
    .result-player-heading {
        display: grid;
        grid-template-columns: 28px minmax(0, 1fr);
    }
    .result-player-score {
        grid-column: 1 / -1;
        max-width: 100%;
        min-width: 0;
        text-align: left;
        overflow-wrap: anywhere;
    }
    .modal-close,
    .modal-back {
        min-width: 44px;
        min-height: 44px;
    }
    .modal .btn,
    .modal select,
    .modal input:not([type="checkbox"]):not([type="range"]):not([type="color"]) {
        min-height: 44px;
        font-size: 16px;
    }
}
/* Mobile play is a viewport-sized cabinet, with secondary actions in the menu. */
.playing.mobile-layout {
    overscroll-behavior: none;
}
.playing.mobile-layout .app {
    grid-template-rows: auto minmax(0, 1fr);
    padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
        env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
    background: var(--well);
}
.playing.mobile-layout .topbar {
    flex-wrap: nowrap;
    min-height: 48px;
    padding: 0 4px 4px;
}
.playing.mobile-layout .topbar .brand,
.playing.mobile-layout .top-actions,
.playing.mobile-layout .session-left,
.playing.mobile-layout #inviteButton,
.playing.mobile-layout #restartButton,
.playing.mobile-layout #leaveButton,
.playing.mobile-layout .footbar {
    display: none;
}
.playing.mobile-layout .mobile-menu-button {
    display: flex;
    align-items: center;
    gap: 6px;
    width: auto;
    height: 44px;
    min-width: 72px;
    padding: 0 8px;
    font: 700 10px var(--mono);
}
.playing.mobile-layout .session-bar {
    justify-content: flex-end;
    margin: 0;
}
.playing.mobile-layout .topbar #pauseButton {
    width: 44px;
    height: 44px;
}
.playing.mobile-layout .boss-timer {
    top: 4px;
    width: 128px;
    padding: 4px 12px 6px;
}
.playing.mobile-layout .boss-timer strong {
    font-size: 28px;
}
.playing.mobile-layout .game-view {
    display: flex;
}
.playing.mobile-layout .score-strip {
    flex: 0 0 32px;
    grid-template-columns: minmax(0, 1fr) auto;
}
.playing.mobile-layout .score-cell {
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    padding: 2px 8px;
    gap: 0;
}
/* Preserve exact values for assistive reading; only the visual HUD is abbreviated. */
.playing.mobile-layout .wide-stat {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}
.playing.mobile-layout .mobile-stat {
    display: block;
}
.playing.mobile-layout .score-cell:nth-child(n + 3),
.playing.mobile-layout .score-cell small {
    display: none;
}
.playing.mobile-layout .score-cell strong {
    font-size: 16px;
    line-height: 1;
}
.playing.mobile-layout .score-cell .stat-label {
    line-height: 1;
}
.playing.mobile-layout .boss-panel {
    grid-template-columns: 58px minmax(0, 1fr);
    grid-template-rows: 44px;
    gap: 0 8px;
    padding: 2px 8px;
}
.playing.mobile-layout .boss-portrait {
    height: 44px;
    width: auto;
}
.playing.mobile-layout .boss-summary {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: 22px 16px;
    gap: 2px 8px;
}
.playing.mobile-layout .boss-heading {
    grid-column: 1 / -1;
}
.playing.mobile-layout .boss-heading h2 {
    font-size: 12px;
}
.playing.mobile-layout .boss-health {
    height: 10px;
}
.playing.mobile-layout .boss-detail {
    font-size: 8px;
}
.playing.mobile-layout .boss-contributors,
.playing.mobile-layout .boss-detail > span:last-child,
.playing.mobile-layout .info-rail {
    display: none;
}
.playing.mobile-layout .arena-layout {
    padding: 0;
    min-width: 0;
}
.playing.mobile-layout .boards {
    display: flex;
    gap: 0;
}
.playing.mobile-layout .board-card {
    display: grid;
    flex: 1;
    width: 100%;
    height: 100%;
    max-width: none;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) 22px 56px;
    gap: 0;
    padding: 0 4px 4px;
    border: 0;
    box-shadow: none;
    background: var(--well);
}
.playing:is(.mobile-layout, .large-room) .cooperative .board-card:not(.selected-board) {
    display: none;
}
.playing.mobile-layout .board-card .board-head,
.playing.mobile-layout .tray-label {
    display: none;
}
.playing.mobile-layout .board-stage {
    display: flex;
    grid-column: 1;
    grid-row: 1;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
}
.playing.mobile-layout .queue-funnel {
    flex: 0 0 36px;
    width: var(--mobile-board-width, 240px);
    height: 36px;
}
.playing.mobile-layout .board-shell {
    flex: 0 0 auto;
    width: var(--mobile-board-width, 240px);
    border: 0;
}
.playing.mobile-layout .board-canvas {
    -webkit-touch-callout: none;
}
.playing.mobile-layout .power-state {
    grid-column: 1;
    grid-row: 2;
    align-self: center;
    height: 20px;
    margin: 0;
    padding: 0 4px;
    font-size: 9px;
    overflow: hidden;
}
.playing.mobile-layout .power-state > span:first-child {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}
.playing.mobile-layout .board-controls {
    display: grid;
    grid-column: 1;
    grid-row: 3;
    grid-template-columns: 48px minmax(0, 1fr) 48px;
    gap: 6px;
    min-height: 0;
}
.playing.mobile-layout .power-tray {
    display: grid;
    grid-column: 2;
    grid-row: 1;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-auto-rows: auto;
    gap: 5px;
    height: 52px;
    padding: 0;
    align-self: stretch;
}
.playing.mobile-layout .power-slot {
    height: 52px;
}
.playing.mobile-layout .power-slot.power-hint {
    /* Keep the nudge inside narrow touch rails instead of scaling beyond the viewport. */
    animation-name: mobile-power-hint;
}
@keyframes mobile-power-hint {
    50% {
        border-color: var(--accent-low);
        box-shadow: inset 0 0 0 2px var(--accent);
    }
}
.playing.mobile-layout .power-slot .power-name {
    display: block;
    font-size: 8px;
}
.playing.mobile-layout .power-slot .hotkey {
    display: none;
}
.playing.mobile-layout .mobile-rotate {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 3px;
    min-height: 44px;
    color: var(--ink);
    background: var(--panel-2);
    border: 2px solid var(--edge);
    font: 700 9px var(--mono);
}
.playing.mobile-layout .mobile-rotate:active {
    background: var(--hover-surface);
    border-color: var(--accent);
}
.playing.mobile-layout [data-ref="rotateLeft"] .icon {
    transform: scaleX(-1);
}
.playing.mobile-layout .board-card:not(.mine) .mobile-rotate {
    visibility: hidden;
}
.playing:is(.mobile-layout, .large-room) .team-tabs {
    display: flex;
    flex: 0 0 44px;
    gap: 4px;
    padding: 2px 4px;
    overflow: hidden;
}
.playing:is(.mobile-layout, .large-room) .team-tabs button {
    display: flex;
    flex: 1;
    align-items: center;
    justify-content: center;
    gap: 4px;
    min-width: 0;
    min-height: 40px;
    font: 700 10px var(--mono);
    background: var(--panel-2);
    border: 2px solid var(--edge);
}
.playing:is(.mobile-layout, .large-room) .team-tabs button[aria-pressed="true"] {
    border-color: var(--accent);
    color: var(--accent);
}
.playing:is(.mobile-layout, .large-room) .team-tabs button > span:last-child {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.playing:is(.mobile-layout, .large-room) .team-tabs .avatar {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}
.playing.mobile-layout .connection-banner {
    gap: 8px;
    padding: 2px 8px;
    font-size: 10px;
}
.playing.mobile-layout .connection-banner > span {
    min-width: 0;
    overflow-wrap: anywhere;
}
.playing.mobile-layout .connection-banner button {
    min-width: 64px;
    min-height: 44px;
}
/* Landscape keeps the well tall on phones and tablets. */
@media (min-aspect-ratio: 6/5) {
    .playing.mobile-layout .app {
        grid-template-rows: minmax(0, 1fr);
    }
    .playing.mobile-layout .topbar {
        position: absolute;
        z-index: 2;
        top: env(safe-area-inset-top, 0px);
        left: env(safe-area-inset-left, 0px);
        width: 164px;
    }
    .playing.mobile-layout .mobile-menu-button {
        min-width: 44px;
        width: 44px;
        padding: 0;
        justify-content: center;
    }
    .playing.mobile-layout .mobile-menu-button span {
        display: none;
    }
    .playing.mobile-layout .boss-timer {
        top: 52px;
    }
    .playing.mobile-layout .game-view {
        display: grid;
        grid-template-columns: 164px minmax(0, 1fr);
        grid-template-rows: minmax(0, 1fr) auto auto auto;
    }
    .playing.mobile-layout .perf {
        grid-column: 1;
        grid-row: 2;
        font-size: 8px;
    }
    .playing.mobile-layout .hud {
        grid-column: 1;
        grid-row: 1;
        padding-top: 112px;
        min-height: 0;
        overflow: auto;
        overscroll-behavior: contain;
    }
    .playing.mobile-layout .score-strip {
        grid-template-columns: minmax(0, 1fr);
        flex: none;
    }
    .playing.mobile-layout .score-cell {
        min-height: 28px;
    }
    .playing.mobile-layout .boss-panel {
        grid-template-columns: minmax(0, 1fr);
        grid-template-rows: 44px auto;
        justify-items: center;
        padding: 6px;
    }
    .playing.mobile-layout .boss-summary {
        width: 100%;
        grid-template-columns: minmax(0, 1fr);
        grid-template-rows: 20px 12px 14px;
    }
    .playing.mobile-layout .boss-heading h2 {
        font-size: 10px;
    }
    .playing.mobile-layout .boss-level {
        font-size: 8px;
    }
    .playing.mobile-layout .boss-detail {
        grid-column: 1;
        grid-row: 3;
    }
    .playing.mobile-layout .arena-layout {
        grid-column: 2;
        grid-row: 1 / -1;
    }
    .playing.mobile-layout .board-card {
        grid-template-columns: minmax(0, 1fr) 140px;
        grid-template-rows: minmax(0, 1fr) 28px;
        gap: 0 8px;
        padding: 4px;
    }
    .playing.mobile-layout .board-stage {
        grid-row: 1 / -1;
    }
    .playing.mobile-layout .queue-funnel {
        flex-basis: 28px;
        height: 28px;
    }
    .playing.mobile-layout .board-controls {
        grid-column: 2;
        grid-row: 1;
        align-self: center;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 6px;
    }
    .playing.mobile-layout .power-tray {
        grid-column: 1 / -1;
        grid-template-columns: minmax(0, 1fr);
        height: auto;
    }
    .playing.mobile-layout .power-slot {
        height: 44px;
        flex-direction: row;
        gap: 8px;
    }
    .playing.mobile-layout .mobile-rotate {
        grid-row: 2;
    }
    .playing.mobile-layout .power-state {
        grid-column: 2;
        grid-row: 2;
        font-size: 8px;
    }
    .playing.mobile-layout .power-state .combo {
        display: none;
    }
    .playing:is(.mobile-layout, .large-room) .team-tabs,
    .playing.mobile-layout .connection-banner {
        grid-column: 1;
        flex-wrap: wrap;
    }
    .playing:is(.mobile-layout, .large-room) .team-tabs {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

/* Motion */
@keyframes appear {
    from {
        opacity: 0;
        transform: translateY(4px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}
@keyframes background-drift {
    from {
        transform: translate3d(0, 0, 0);
    }
    to {
        transform: translate3d(160px, 160px, 0);
    }
}
@media (prefers-reduced-motion: reduce) {
    *,
    *:before,
    *:after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
    .background-blocks:before {
        animation: none;
    }
}
body.reduced *,
body.reduced *:before,
body.reduced *:after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
}
body.reduced .background-blocks:before {
    animation: none;
}

@media (max-width: 900px), (pointer: coarse) {
    .modal-shade.settings-shade {
        width: 100vw;
        height: 100dvh;
        padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
            env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
        background: var(--panel);
    }
    .settings-dialog,
    .settings-child {
        width: 100%;
        height: 100%;
        max-width: none;
        max-height: 100%;
        border: 0;
        box-shadow: none;
        animation-name: settings-appear;
    }
    .settings-child {
        display: flex;
        flex-direction: column;
    }
    .settings-child > .modal-bar {
        flex-shrink: 0;
    }
    .settings-child > .modal-body {
        min-height: 0;
        overflow: auto;
        scroll-padding-block: 12px 160px;
    }
    .settings-heading {
        padding: 14px 16px 12px;
    }
    .settings-heading h2 {
        font-size: 20px;
    }
    .settings-tabs {
        gap: 4px;
        padding: 0 12px 12px;
    }
    .settings-tab {
        padding: 6px 4px;
        font-size: 11px;
    }
    .settings-content {
        padding: 0 16px;
    }
    .settings-dialog .volume-row {
        flex: 1 1 100%;
    }
    .settings-footer {
        padding: 12px 16px;
    }
    .settings-footer .btn {
        min-width: 100px;
    }
}

@keyframes settings-appear {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@media (max-height: 500px) and (max-width: 900px), (max-height: 500px) and (pointer: coarse) {
    .settings-heading {
        padding: 8px 16px;
    }
    .settings-heading h2 {
        font-size: 16px;
    }
    .settings-heading p {
        display: none;
    }
    .settings-tabs {
        padding-bottom: 8px;
    }
    .settings-footer {
        padding-block: 8px;
    }
}

.playing.large-room .team-tabs {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
}
.playing.large-room .team-tabs button {
    flex: 0 0 auto;
    min-width: 96px;
}
