:root {
    --bg: #020617;
    --panel: rgba(15, 23, 42, 0.78);
    --panel-strong: rgba(2, 6, 23, 0.92);
    --text: #e5e7eb;
    --soft: #cbd5f5;
    --muted: #94a3b8;
    --cyan: #38bdf8;
    --green: #22c55e;
    --purple: #a78bfa;
    --amber: #f59e0b;
    --red: #ef4444;
    --border: rgba(56, 189, 248, 0.28);
    --shadow: rgba(56, 189, 248, 0.18);
}

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

body {
    background:
        radial-gradient(circle at 16% 12%, rgba(56, 189, 248, 0.13), transparent 28%),
        radial-gradient(circle at 84% 16%, rgba(167, 139, 250, 0.13), transparent 26%),
        linear-gradient(180deg, #020617 0%, #050816 52%, #020617 100%);
    color: var(--text);
    font-family: Inter, "Segoe UI", system-ui, sans-serif;
    margin: 0;
    min-height: 100vh;
    overflow: hidden;
}

.game-shell {
    align-items: center;
    display: flex;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    padding: clamp(8px, 2.2vh, 24px);
    width: 100%;
}

.game-panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 0 42px var(--shadow), inset 0 0 36px rgba(2, 6, 23, 0.42);
    margin: 0 auto;
    max-width: 1180px;
    height: 100%;
    max-height: min(790px, 100%);
    overflow: hidden;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    width: 100%;
    min-width: 0;
}

.game-topbar {
    align-items: center;
    background: linear-gradient(180deg, rgba(15, 23, 42, 0.94), rgba(2, 6, 23, 0.86));
    border-bottom: 1px solid var(--border);
    display: grid;
    gap: 16px;
    grid-template-columns: auto 1fr auto;
    padding: 14px 18px;
    min-width: 0;
}

.back-link,
.controls button,
.overlay-card button {
    background: rgba(2, 6, 23, 0.78);
    border: 1px solid var(--border);
    border-radius: 7px;
    color: var(--cyan);
    cursor: pointer;
    font: 700 12px/1 Inter, "Segoe UI", system-ui, sans-serif;
    letter-spacing: 0;
    padding: 10px 12px;
    text-decoration: none;
}

.back-link:hover,
.controls button:hover,
.overlay-card button:hover,
.back-link:focus-visible,
.controls button:focus-visible,
.overlay-card button:focus-visible {
    box-shadow: 0 0 16px var(--shadow);
    outline: 1px solid var(--cyan);
}

.game-title h1 {
    color: var(--cyan);
    font-size: 22px;
    line-height: 1.1;
    margin: 0;
}

.game-title {
    min-width: 0;
}

.game-title p {
    color: var(--muted);
    font: 600 12px/1.4 "SFMono-Regular", Consolas, monospace;
    margin: 4px 0 0;
}

.scoreboard {
    display: flex;
    gap: 10px;
    min-width: 0;
}

.scoreboard div {
    border: 1px solid rgba(56, 189, 248, 0.22);
    border-radius: 7px;
    min-width: 94px;
    padding: 8px 10px;
    text-align: right;
}

.scoreboard span {
    color: var(--muted);
    display: block;
    font: 700 10px/1 "SFMono-Regular", Consolas, monospace;
    text-transform: uppercase;
}

.scoreboard strong {
    color: var(--green);
    display: block;
    font: 800 22px/1.1 "SFMono-Regular", Consolas, monospace;
    margin-top: 4px;
}

.arena-wrap {
    display: grid;
    place-items: center;
    background:
        linear-gradient(rgba(56, 189, 248, 0.08) 1px, transparent 1px),
        linear-gradient(90deg, rgba(56, 189, 248, 0.08) 1px, transparent 1px),
        radial-gradient(circle at 50% 50%, rgba(56, 189, 248, 0.12), transparent 42%),
        #030712;
    background-size: 54px 54px, 54px 54px, auto, auto;
    height: auto;
    min-height: 0;
    overflow: hidden;
    position: relative;
}

.game-canvas {
    display: block;
    height: 100%;
    width: 100%;
    touch-action: none;
}

.match-status { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

.game-canvas:focus-visible {
    outline: 2px solid var(--cyan);
    outline-offset: -2px;
}

.controls {
    background: rgba(2, 6, 23, 0.72);
    border-top: 1px solid var(--border);
    display: flex;
    gap: 10px;
    padding: 14px 18px;
}

.overlay {
    align-items: center;
    background:
        radial-gradient(circle at 28% 28%, rgba(56, 189, 248, 0.19), transparent 30%),
        radial-gradient(circle at 72% 64%, rgba(167, 139, 250, 0.16), transparent 28%),
        linear-gradient(135deg, rgba(2, 6, 23, 0.92), rgba(2, 6, 23, 0.64));
    display: flex;
    inset: 0;
    justify-content: center;
    padding: 18px;
    position: absolute;
    z-index: 10;
}

.overlay.hidden {
    display: none;
}

.overlay::before,
.overlay::after {
    content: "";
    pointer-events: none;
    position: absolute;
}

.overlay::before {
    background:
        linear-gradient(90deg, transparent, rgba(56, 189, 248, 0.24), transparent),
        repeating-linear-gradient(180deg, rgba(226, 232, 240, 0.06) 0 1px, transparent 1px 7px);
    inset: 0;
    opacity: 0.5;
}

.overlay::after {
    border: 1px solid rgba(56, 189, 248, 0.24);
    border-radius: 14px;
    box-shadow: inset 0 0 36px rgba(56, 189, 248, 0.13);
    inset: 18px;
}

.overlay-card {
    align-items: center;
    background:
        linear-gradient(135deg, rgba(15, 23, 42, 0.92), rgba(2, 6, 23, 0.82)),
        radial-gradient(circle at 12% 16%, rgba(56, 189, 248, 0.16), transparent 34%);
    border: 1px solid rgba(56, 189, 248, 0.38);
    border-radius: 10px;
    box-shadow: 0 0 34px rgba(56, 189, 248, 0.22), inset 0 0 28px rgba(2, 6, 23, 0.62);
    display: grid;
    gap: 22px;
    grid-template-columns: minmax(170px, 0.62fr) minmax(330px, 1fr);
    grid-template-areas: "visual content" "visual actions";
    grid-template-rows: minmax(0, 1fr) auto;
    max-width: 760px;
    max-height: 100%;
    overflow: hidden;
    padding: 22px;
    position: relative;
    width: min(92vw, 760px);
    z-index: 1;
}

.overlay-content {
    grid-area: content;
    align-self: stretch;
    min-height: 0;
    min-width: 0;
    overflow: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    padding: 2px;
}

.overlay-actions {
    grid-area: actions;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    gap: 8px;
}

.overlay-actions .back-link {
    display: flex;
    align-items: center;
    justify-content: center;
}

.overlay-actions a,
.overlay-actions button {
    min-height: 44px;
}

.overlay-end .overlay-card {
    border-color: rgba(245, 158, 11, 0.42);
    box-shadow: 0 0 36px rgba(245, 158, 11, 0.2), inset 0 0 30px rgba(127, 29, 29, 0.16);
}

.overlay-card::before {
    background: linear-gradient(90deg, transparent, rgba(56, 189, 248, 0.75), transparent);
    content: "";
    height: 1px;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
}

.overlay-end .overlay-card::before {
    background: linear-gradient(90deg, transparent, rgba(245, 158, 11, 0.82), transparent);
}

.overlay-visual {
    grid-area: visual;
    aspect-ratio: 1;
    min-width: 0;
    position: relative;
}

.orb-grid {
    animation: spinGrid 9s linear infinite;
    background:
        radial-gradient(circle, rgba(56, 189, 248, 0.22) 0 2px, transparent 3px),
        conic-gradient(from 12deg, transparent, rgba(56, 189, 248, 0.34), transparent, rgba(167, 139, 250, 0.28), transparent);
    background-size: 22px 22px, auto;
    border: 1px solid rgba(56, 189, 248, 0.24);
    border-radius: 50%;
    inset: 8%;
    position: absolute;
}

.paddle-scan {
    background: linear-gradient(180deg, var(--cyan), var(--green));
    border-radius: 4px;
    box-shadow: 0 0 18px rgba(56, 189, 248, 0.6);
    height: 78px;
    position: absolute;
    top: calc(50% - 39px);
    width: 10px;
}

.paddle-left {
    left: 26%;
}

.paddle-right {
    right: 26%;
}

.ball-preview {
    animation: previewBall 1.7s ease-in-out infinite;
    background: #e5e7eb;
    border-radius: 50%;
    box-shadow: 0 0 18px rgba(56, 189, 248, 0.8), inset 0 0 8px rgba(56, 189, 248, 0.55);
    height: 18px;
    left: calc(50% - 9px);
    position: absolute;
    top: calc(50% - 9px);
    width: 18px;
}

.ball-preview.small {
    animation-delay: -0.7s;
    height: 10px;
    opacity: 0.72;
    top: 35%;
    width: 10px;
}

.overlay-mode {
    color: var(--green);
    display: block;
    font: 800 11px/1.2 "SFMono-Regular", Consolas, monospace;
    margin-bottom: 8px;
    text-transform: uppercase;
}

.overlay-end .overlay-mode {
    color: var(--amber);
}

.overlay-end .settings-grid {
    display: none;
}

.overlay-card h2 {
    color: var(--cyan);
    font-size: clamp(30px, 5vw, 52px);
    line-height: 1.1;
    margin: 0 0 10px;
}

.overlay-card p {
    color: var(--soft);
    line-height: 1.55;
    margin: 0 0 18px;
    max-width: 500px;
}

.settings-grid {
    display: grid;
    gap: 10px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin: 0 0 14px;
}

.settings-grid fieldset {
    border: 1px solid rgba(56, 189, 248, 0.22);
    border-radius: 8px;
    margin: 0;
    padding: 12px;
}

.settings-grid legend {
    color: var(--muted);
    font: 800 10px/1 "SFMono-Regular", Consolas, monospace;
    padding: 0 4px;
    text-transform: uppercase;
}

.settings-grid label {
    align-items: center;
    background: rgba(2, 6, 23, 0.48);
    border: 1px solid rgba(56, 189, 248, 0.18);
    border-radius: 7px;
    color: var(--soft);
    cursor: pointer;
    display: flex;
    font: 700 12px/1.2 Inter, "Segoe UI", system-ui, sans-serif;
    gap: 8px;
    justify-content: center;
    margin-top: 8px;
    min-height: 36px;
    padding: 9px 10px;
    text-align: center;
}

.settings-grid input {
    accent-color: var(--cyan);
    height: 1px;
    opacity: 0;
    position: absolute;
    width: 1px;
}

.settings-grid label:has(input:checked) {
    background: linear-gradient(135deg, rgba(56, 189, 248, 0.22), rgba(34, 197, 94, 0.12));
    border-color: rgba(56, 189, 248, 0.62);
    box-shadow: 0 0 18px rgba(56, 189, 248, 0.16);
    color: var(--green);
}

.settings-grid label:focus-within {
    outline: 1px solid var(--cyan);
    outline-offset: 2px;
}

.overlay-stats {
    display: grid;
    gap: 8px;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    margin: 0 0 18px;
}

.overlay-stats div {
    background: rgba(2, 6, 23, 0.62);
    border: 1px solid rgba(56, 189, 248, 0.22);
    border-radius: 7px;
    padding: 10px;
}

.overlay-end .overlay-stats div {
    border-color: rgba(245, 158, 11, 0.24);
}

.overlay-stats span {
    color: var(--muted);
    display: block;
    font: 800 10px/1 "SFMono-Regular", Consolas, monospace;
    text-transform: uppercase;
}

.overlay-stats strong {
    color: var(--green);
    display: block;
    font: 800 18px/1.1 "SFMono-Regular", Consolas, monospace;
    margin-top: 6px;
}

.overlay-end .overlay-card h2,
.overlay-end .overlay-stats strong {
    color: var(--amber);
}

.overlay-card button {
    background: linear-gradient(135deg, rgba(56, 189, 248, 0.22), rgba(2, 6, 23, 0.88));
    box-shadow: 0 0 18px rgba(56, 189, 248, 0.12);
    min-width: 132px;
}

.overlay-end .overlay-card button {
    background: linear-gradient(135deg, rgba(245, 158, 11, 0.2), rgba(2, 6, 23, 0.9));
    border-color: rgba(245, 158, 11, 0.44);
    color: var(--amber);
}

@media (max-width: 780px) {
    .game-shell {
        min-height: 0;
        padding: 8px;
    }

    .game-panel {
        max-width: calc(100dvw - 16px);
    }

    .game-topbar {
        gap: 10px;
        grid-template-columns: auto 1fr;
        padding: 10px 12px;
    }

    .back-link {
        grid-row: 1;
    }

    .game-title {
        grid-row: 1;
    }

    .scoreboard {
        grid-column: 1 / -1;
        justify-content: space-between;
    }

    .scoreboard div {
        flex: 1;
    }

    .arena-wrap {
        height: auto;
        min-height: 0;
    }

    .overlay-card {
        grid-template-columns: 1fr;
        grid-template-areas: "content" "actions";
        max-width: 540px;
        max-height: 100%;
        text-align: center;
    }

    .overlay-visual {
        display: none;
    }
}

@media (max-width: 520px) {
    .game-shell {
        padding: 6px;
    }

    .game-panel {
        border-radius: 8px;
        margin: 0 auto;
        max-width: min(378px, calc(100dvw - 12px));
        width: min(378px, calc(100dvw - 12px));
    }

    .game-title h1 {
        font-size: 19px;
    }

    .game-title p {
        font-size: 11px;
    }

    .scoreboard div {
        min-width: 0;
        padding: 7px 9px;
    }

    .scoreboard strong {
        font-size: 19px;
    }

    .arena-wrap {
        aspect-ratio: auto;
        height: auto;
        min-height: 0;
    }

    .controls {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        padding: 10px;
    }

    .controls button {
        min-width: 0;
        padding: 10px 5px;
    }

    .overlay {
        padding: 12px;
    }

    .overlay::after {
        inset: 8px;
    }

    .overlay-card {
        gap: 12px;
        max-height: 100%;
        max-width: min(342px, calc(100dvw - 44px));
        padding: 16px;
        width: min(342px, calc(100dvw - 44px));
    }

    .overlay-content {
        overflow: auto;
    }

    .overlay-visual {
        display: none;
    }

    .overlay-card h2 {
        font-size: 30px;
        margin-bottom: 8px;
    }

    .overlay-card p {
        font-size: 14px;
        line-height: 1.35;
        margin-bottom: 12px;
    }

    .settings-grid,
    .overlay-stats {
        grid-template-columns: 1fr;
    }

    .settings-grid fieldset {
        padding: 8px;
    }

    .settings-grid label {
        justify-content: center;
        margin-top: 6px;
        min-height: 32px;
        padding: 7px 8px;
    }

    .overlay-stats {
        margin-bottom: 12px;
    }

    .overlay-stats div {
        align-items: center;
        display: flex;
        justify-content: space-between;
        padding: 8px 10px;
    }

    .overlay-stats strong {
        margin-top: 0;
    }
}

/* Short-height mode reserves space for the match, not a minimum-size canvas. */
@media (max-width: 780px), (max-height: 600px) {
    .game-shell {
        padding: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
    }
    .game-panel { max-height: 100%; }
    .game-topbar { gap: 6px; padding: 6px 8px; }
    .back-link { display: flex; align-items: center; justify-content: center; min-height: 44px; }
    .scoreboard div { padding: 4px 8px; }
    .scoreboard span { font-size: 11px; }
    .controls { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; padding: 6px; }
    .controls button { min-width: 0; min-height: 44px; padding: 6px 4px; line-height: 1.2; }
    .overlay {
        position: fixed;
        padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    }
    .overlay-card {
        grid-template-columns: minmax(0, 1fr);
        grid-template-areas: "content" "actions";
        width: 100%;
        max-width: 560px;
        max-height: 100%;
        gap: 10px;
        padding: 14px;
    }
    .overlay-visual { display: none; }
    .settings-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .settings-grid fieldset { min-width: 0; padding: 6px; }
    .settings-grid label { min-height: 44px; padding: 8px 4px; }
    .overlay-stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .overlay-stats div { display: block; padding: 8px 4px; }
    .overlay-stats strong { font-size: 16px; margin-top: 5px; }
}

@media (pointer: coarse) {
    .back-link, .controls button, .settings-grid label { min-height: 44px; }
    .controls button { min-width: 44px; }
}

@media (max-height: 500px) and (min-width: 480px) {
    .game-panel { width: 100%; max-width: 1180px; }
    .game-topbar { grid-template-columns: auto minmax(0, 1fr) auto; }
    .game-title h1 { font-size: 18px; }
    .game-title p { font-size: 11px; }
    .scoreboard { grid-column: auto; gap: 4px; }
    .scoreboard div { min-width: 48px; }
    .scoreboard strong { font-size: 16px; }
}

@keyframes spinGrid {
    to {
        transform: rotate(360deg);
    }
}

@keyframes previewBall {
    0%,
    100% {
        transform: translate(-60px, -28px);
    }

    50% {
        transform: translate(62px, 30px);
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }
}
[hidden] { display: none !important; }
.overlay-end .overlay-actions { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.overlay-end .overlay-actions > * { min-width: 0; white-space: normal; overflow-wrap: anywhere; }
