:root {
    --bg: #0f172a;
    --bg-soft: #1e293b;
    --panel: rgba(15, 23, 42, 0.9);
    --panel-stroke: rgba(148, 163, 184, 0.25);
    --text: #e2e8f0;
    --muted: #cbd5e1;
    --accent: #f59e0b;
    --accent-2: #06b6d4;
    --danger: #f97316;
    --shadow: 0 14px 42px rgba(2, 6, 23, 0.48);
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    min-height: 100%;
}

body {
    font-family: "Space Grotesk", sans-serif;
    color: var(--text);
    background:
        radial-gradient(circle at 20% 15%, #334155 0%, rgba(51, 65, 85, 0) 40%),
        radial-gradient(circle at 80% 15%, #0e7490 0%, rgba(14, 116, 144, 0) 38%),
        linear-gradient(160deg, #0b1120 0%, #10172a 45%, #0a0f1e 100%);
    overflow-x: hidden;
}

.bg-orb {
    position: fixed;
    border-radius: 50%;
    filter: blur(36px);
    z-index: 0;
    opacity: 0.32;
    pointer-events: none;
}

.orb-a {
    width: min(34vw, 320px);
    aspect-ratio: 1;
    background: #f59e0b;
    top: -8vh;
    right: -10vw;
    animation: floatA 20s ease-in-out infinite;
}

.orb-b {
    width: min(36vw, 360px);
    aspect-ratio: 1;
    background: #06b6d4;
    left: -12vw;
    bottom: -16vh;
    animation: floatB 22s ease-in-out infinite;
}

.app-shell {
    position: relative;
    z-index: 1;
    width: min(1180px, 96vw);
    margin: 2.5rem auto;
    display: grid;
    gap: 1.2rem;
    grid-template-columns: minmax(280px, 390px) 1fr;
}

.app-meta {
    grid-column: 1 / -1;
    justify-self: center;
    margin-top: 0.1rem;
    padding: 0.25rem 0.7rem;
    border-radius: 999px;
    color: rgba(203, 213, 225, 0.78);
    font-size: 0.78rem;
    letter-spacing: 0.02em;
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    background: rgba(15, 23, 42, 0.28);
    border: 1px solid rgba(148, 163, 184, 0.2);
}

.app-meta a {
    color: rgba(226, 232, 240, 0.96);
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
    text-decoration-color: rgba(34, 211, 238, 0.45);
    transition: color 0.16s ease, text-decoration-color 0.16s ease;
}

.app-meta a:hover {
    color: #ffffff;
    text-decoration-color: rgba(251, 191, 36, 0.85);
}

.app-meta a:focus-visible {
    outline: 2px solid rgba(34, 211, 238, 0.7);
    outline-offset: 2px;
    border-radius: 4px;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--panel-stroke);
    border-radius: 22px;
    box-shadow: var(--shadow);
    min-width: 0;
}

.panel-controls {
    padding: 1.35rem;
}

h1 {
    margin: 0;
    font-family: "Sora", sans-serif;
    font-size: clamp(1.7rem, 4vw, 2.4rem);
    letter-spacing: 0.03em;
}

.subtitle {
    margin: 0.45rem 0 1rem;
    color: var(--muted);
    line-height: 1.4;
}

.controls-grid {
    display: grid;
    gap: 0.8rem;
}

.is-hidden,
.online-config.is-hidden {
    display: none;
}

.online-config {
    border: 1px solid rgba(34, 211, 238, 0.35);
    border-radius: 12px;
    background: rgba(14, 116, 144, 0.12);
    padding: 0.75rem;
    display: grid;
    gap: 0.55rem;
}

.online-actions {
    display: flex;
    gap: 0.5rem;
}

.online-status {
    margin: 0;
    color: #bae6fd;
    font-size: 0.88rem;
    line-height: 1.35;
}

.online-diagnostic {
    margin: 0;
    padding: 0.4rem 0.5rem;
    border-radius: 8px;
    border: 1px dashed rgba(251, 191, 36, 0.55);
    background: rgba(15, 23, 42, 0.45);
    color: #fef3c7;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.72rem;
    line-height: 1.35;
    word-break: break-word;
}

label {
    display: grid;
    gap: 0.4rem;
    font-size: 0.95rem;
}

.inline-input {
    display: grid;
    gap: 0.5rem;
    grid-template-columns: 1fr auto;
    align-items: center;
}

output {
    min-width: 3.8rem;
    text-align: center;
    font-weight: 700;
    color: #fde68a;
    background: rgba(245, 158, 11, 0.2);
    border-radius: 8px;
    padding: 0.25rem 0.45rem;
}

input[type="range"] {
    width: 100%;
    accent-color: var(--accent);
}

select,
input[type="text"],
input[type="password"],
.btn {
    width: 100%;
    border-radius: 10px;
    border: 1px solid rgba(148, 163, 184, 0.35);
    background: rgba(15, 23, 42, 0.7);
    color: var(--text);
    padding: 0.65rem 0.75rem;
    font: inherit;
}

select:focus,
.btn:focus,
input:focus {
    outline: 2px solid rgba(6, 182, 212, 0.65);
    outline-offset: 1px;
}

.hint {
    color: #dbeafe;
    min-height: 1.3em;
}

.actions {
    margin-top: 1rem;
    display: grid;
    gap: 0.55rem;
}

.btn {
    cursor: pointer;
    transition: transform 0.18s ease, filter 0.18s ease;
}

.btn:hover {
    transform: translateY(-1px);
    filter: brightness(1.08);
}

.btn:active {
    transform: translateY(0);
}

.btn-primary {
    background: linear-gradient(130deg, #f59e0b 0%, #f97316 100%);
    border: 0;
    color: #111827;
    font-weight: 700;
}

.toggle-sound {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    user-select: none;
    color: #f1f5f9;
}

.toggle-sound input {
    width: 1.05rem;
    height: 1.05rem;
}

.panel-game {
    padding: 1.1rem;
    display: grid;
    align-content: start;
    gap: 0.75rem;
    min-width: 0;
}

.game-head {
    display: flex;
    justify-content: space-between;
    gap: 0.8rem;
    flex-wrap: wrap;
    align-items: baseline;
}

.turn-label {
    margin: 0;
    font-weight: 700;
    color: #fef3c7;
}

.goal-label {
    margin: 0;
    color: var(--muted);
    font-size: 0.95rem;
}

.board {
    --size: 3;
    --board-max: min(78vh, 90vw);
    width: min(100%, var(--board-max), 720px);
    max-width: 100%;
    aspect-ratio: 1;
    margin: 0 auto;
    display: grid;
    grid-template-columns: repeat(var(--size), 1fr);
    grid-template-rows: repeat(var(--size), 1fr);
    gap: clamp(4px, 1vw, 8px);
    padding: clamp(8px, 1.5vw, 14px);
    border-radius: 20px;
    position: relative;
    overflow: hidden;
    background:
        linear-gradient(145deg, rgba(14, 116, 144, 0.28), rgba(245, 158, 11, 0.2)),
        rgba(15, 23, 42, 0.58);
    border: 1px solid rgba(148, 163, 184, 0.35);
}

.game-actions {
    display: flex;
    justify-content: flex-end;
}

.btn-ghost {
    width: auto;
    min-width: 9rem;
    border-color: rgba(148, 163, 184, 0.45);
    background: rgba(15, 23, 42, 0.45);
}

.btn-ghost:disabled {
    cursor: not-allowed;
    opacity: 0.55;
    transform: none;
    filter: none;
}

.board.victory-flash::after {
    content: "";
    position: absolute;
    inset: -10%;
    pointer-events: none;
    z-index: 3;
    background:
        linear-gradient(var(--victory-flash-angle, 45deg), rgba(255, 255, 255, 0) 38%, rgba(255, 255, 255, 0.3) 50%, rgba(255, 255, 255, 0) 62%),
        radial-gradient(circle at 50% 50%, rgba(34, 211, 238, 0.16), rgba(34, 211, 238, 0));
    animation: victoryFlash 0.85s ease-out;
}

.board.draw-flash::after {
    content: "";
    position: absolute;
    inset: -8%;
    pointer-events: none;
    z-index: 3;
    background:
        radial-gradient(circle at 50% 50%, rgba(148, 163, 184, 0.2), rgba(15, 23, 42, 0.08) 45%, rgba(15, 23, 42, 0)),
        repeating-linear-gradient(135deg, rgba(148, 163, 184, 0.06) 0 9px, rgba(100, 116, 139, 0.12) 9px 18px);
    animation: drawFlash 1.1s ease-out;
}

.board.victory-dir-h {
    --victory-flash-angle: 0deg;
}

.board.victory-dir-v {
    --victory-flash-angle: 90deg;
}

.board.victory-dir-d1 {
    --victory-flash-angle: 45deg;
}

.board.victory-dir-d2 {
    --victory-flash-angle: 135deg;
}

.cell {
    border: 0;
    padding: 0;
    border-radius: 12px;
    background: linear-gradient(165deg, rgba(51, 65, 85, 0.85), rgba(30, 41, 59, 0.96));
    box-shadow: inset 0 0 0 1px rgba(148, 163, 184, 0.2);
    cursor: pointer;
    width: 100%;
    height: 100%;
    aspect-ratio: 1;
    min-height: 0;
    display: grid;
    place-items: center;
    position: relative;
    transition: transform 0.15s ease, box-shadow 0.22s ease;
    overflow: hidden;
}

.cell::before {
    content: "";
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at 30% 20%, rgba(255, 255, 255, 0.09), rgba(255, 255, 255, 0));
    pointer-events: none;
}

.cell:hover:not(.filled):not(.locked) {
    transform: translateY(-1px) scale(1.01);
    box-shadow: inset 0 0 0 1px rgba(251, 191, 36, 0.55);
}

.cell.locked {
    cursor: wait;
}

.cell.filled {
    cursor: default;
}

.cell.recent {
    animation: recentFlash 1.1s ease-out;
    box-shadow: inset 0 0 0 2px rgba(34, 211, 238, 0.82), 0 0 0 1px rgba(251, 191, 36, 0.72);
}

.cell.threat-danger:not(.filled) {
    box-shadow: inset 0 0 0 2px rgba(248, 113, 113, 0.9), 0 0 0 1px rgba(239, 68, 68, 0.55);
    background: linear-gradient(165deg, rgba(127, 29, 29, 0.52), rgba(30, 41, 59, 0.95));
}

.cell.threat-opportunity:not(.filled) {
    box-shadow: inset 0 0 0 2px rgba(134, 239, 172, 0.95), 0 0 0 1px rgba(34, 197, 94, 0.6);
    background: linear-gradient(165deg, rgba(20, 83, 45, 0.58), rgba(30, 41, 59, 0.95));
}

.cell.threat-danger.threat-opportunity:not(.filled) {
    box-shadow: inset 0 0 0 2px rgba(251, 191, 36, 0.95), 0 0 0 1px rgba(245, 158, 11, 0.6);
    background: linear-gradient(165deg, rgba(120, 53, 15, 0.58), rgba(30, 41, 59, 0.95));
}

.symbol {
    font-family: "Sora", sans-serif;
    font-weight: 700;
    font-size: clamp(1rem, calc(40vmin / var(--size)), 3.1rem);
    line-height: 1;
    animation: dropIn 0.2s ease-out;
}

.symbol-x {
    color: #f59e0b;
}

.symbol-o {
    color: #22d3ee;
}

.stone {
    width: clamp(10px, calc(30vmin / var(--size)), 46px);
    aspect-ratio: 1;
    border-radius: 50%;
    animation: popIn 0.18s ease-out;
}

.stone-black {
    background: radial-gradient(circle at 35% 30%, #475569, #0f172a 65%);
    box-shadow: inset -3px -2px 6px rgba(2, 6, 23, 0.6), 0 2px 8px rgba(2, 6, 23, 0.55);
}

.stone-white {
    background: radial-gradient(circle at 30% 25%, #ffffff, #cbd5e1 65%);
    box-shadow: inset -2px -1px 4px rgba(71, 85, 105, 0.4), 0 2px 8px rgba(2, 6, 23, 0.45);
}

.winning {
    box-shadow: inset 0 0 0 2px rgba(125, 211, 252, 0.95), 0 0 0 1px rgba(251, 191, 36, 0.82);
    animation: winningCellGlow 1s ease-in-out infinite;
}

.winning::before {
    background:
        radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.22), rgba(255, 255, 255, 0)),
        linear-gradient(140deg, rgba(34, 211, 238, 0.18), rgba(251, 191, 36, 0.2));
}

.winning .symbol,
.winning .stone {
    filter: drop-shadow(0 0 5px rgba(251, 191, 36, 0.45));
    animation: winningPiecePulse 0.9s ease-in-out infinite;
}

.win-burst {
    position: absolute;
    width: 1px;
    height: 1px;
    pointer-events: none;
    z-index: 4;
}

.spark {
    position: absolute;
    left: 0;
    top: 0;
    width: 6px;
    height: 6px;
    border-radius: 999px;
    transform: translate(-50%, -50%);
    opacity: 0;
    background: radial-gradient(circle at 32% 28%, #ffffff, #f59e0b 72%);
    box-shadow: 0 0 10px rgba(251, 191, 36, 0.8);
    animation: sparkBurst 0.72s ease-out forwards;
    animation-delay: var(--delay, 0s);
}

@media (max-width: 980px) {
    .app-shell {
        grid-template-columns: 1fr;
        margin: 1rem auto 1.6rem;
    }

    .app-meta {
        margin-top: 0.2rem;
    }

    .panel-controls,
    .panel-game {
        padding: 1rem;
    }

    .board {
        --board-max: min(76vh, 96vw);
    }

    .bg-orb {
        display: none;
    }
}

@media (max-width: 520px) {
    .controls-grid {
        gap: 0.65rem;
    }

    .game-head {
        flex-direction: column;
        gap: 0.2rem;
    }

    .board {
        border-radius: 16px;
    }
}

@keyframes dropIn {
    from {
        transform: scale(0.4) rotate(-8deg);
        opacity: 0;
    }

    to {
        transform: scale(1) rotate(0deg);
        opacity: 1;
    }
}

@keyframes popIn {
    from {
        transform: scale(0.3);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes pulse {

    0%,
    100% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.025);
    }
}

@keyframes recentFlash {
    0% {
        transform: scale(1);
        filter: brightness(1);
    }

    25% {
        transform: scale(1.045);
        filter: brightness(1.25);
    }

    100% {
        transform: scale(1);
        filter: brightness(1);
    }
}

@keyframes victoryFlash {
    0% {
        opacity: 0;
        transform: translateX(-20%) scale(0.95);
    }

    28% {
        opacity: 1;
        transform: translateX(0) scale(1.04);
    }

    100% {
        opacity: 0;
        transform: translateX(20%) scale(1);
    }
}

@keyframes drawFlash {
    0% {
        opacity: 0;
        transform: scale(0.96);
        filter: saturate(0.8);
    }

    40% {
        opacity: 0.95;
        transform: scale(1.01);
        filter: saturate(0.7);
    }

    100% {
        opacity: 0;
        transform: scale(1);
        filter: saturate(1);
    }
}

@keyframes sparkBurst {
    0% {
        transform: translate(-50%, -50%) scale(0.2);
        opacity: 0.95;
    }

    100% {
        transform: translate(calc(-50% + (var(--dx) * 1px)), calc(-50% + (var(--dy) * 1px))) scale(0.45);
        opacity: 0;
    }
}

@keyframes floatA {

    0%,
    100% {
        transform: translate(0, 0);
    }

    50% {
        transform: translate(-20px, 18px);
    }
}

@keyframes floatB {

    0%,
    100% {
        transform: translate(0, 0);
    }

    50% {
        transform: translate(28px, -16px);
    }
}

@keyframes winningCellGlow {

    0%,
    100% {
        transform: scale(1);
        box-shadow: inset 0 0 0 2px rgba(125, 211, 252, 0.95), 0 0 0 1px rgba(251, 191, 36, 0.95), 0 0 18px rgba(34, 211, 238, 0.45);
    }

    50% {
        transform: scale(1.03);
        box-shadow: inset 0 0 0 2px rgba(186, 230, 253, 1), 0 0 0 1px rgba(253, 230, 138, 1), 0 0 34px rgba(34, 211, 238, 0.72);
    }
}

@keyframes winningPiecePulse {

    0%,
    100% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.12);
    }
}