* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
    min-height: 100%;
}

body {
    font-family:
        -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        sans-serif;

    background:
        radial-gradient(
            circle at top,
            #243b55 0%,
            #141e30 55%,
            #0b1020 100%
        );

    color: #fff;

    overflow-x: hidden;
}

button,
input,
select {
    font: inherit;
}

button {
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}


/* =========================
   PAGE
========================= */

.page {
    width: 100%;
    min-height: 100vh;

    padding: 16px 14px 32px;

    display: flex;
    flex-direction: column;
    align-items: center;
}


/* =========================
   TOP BAR
========================= */

.topbar {
    width: min(100%, 900px);

    display: grid;
    grid-template-columns: 1fr auto 1fr;

    align-items: center;

    gap: 10px;

    margin-bottom: 18px;
}

.brand {
    font-size: 20px;
    font-weight: 800;
    text-align: center;
}

.ghost-btn {
    border: 0;
    border-radius: 11px;

    padding: 10px 13px;

    color: #fff;

    background: rgba(255, 255, 255, .1);

    transition:
        background .15s,
        transform .12s;
}

.topbar .ghost-btn:last-child {
    justify-self: end;
}

.ghost-btn:hover {
    background: rgba(255, 255, 255, .18);
}

.ghost-btn:active {
    transform: scale(.97);
}


/* =========================
   BUTTONS
========================= */

.primary-btn,
.action-btn {
    border: 0;
    border-radius: 12px;

    color: #fff;

    transition:
        transform .12s,
        background .12s,
        filter .12s;
}

.primary-btn:active,
.action-btn:active {
    transform: scale(.97);
}

.primary-btn {
    width: 100%;

    padding: 13px 16px;

    background:
        linear-gradient(
            135deg,
            #3b82f6,
            #2563eb
        );

    font-weight: 700;
}

.primary-btn:hover {
    filter: brightness(1.08);
}

.action-btn {
    padding: 11px 14px;

    background:
        rgba(255, 255, 255, .1);
}

.action-btn:hover {
    background:
        rgba(255, 255, 255, .18);
}

.action-btn.danger {
    background:
        rgba(220, 38, 38, .85);
}

.action-btn.danger:hover {
    background:
        rgba(239, 68, 68, .9);
}

.primary-btn:disabled,
.action-btn:disabled {
    opacity: .5;
    cursor: not-allowed;
}


/* =========================
   SETUP
========================= */

.setup {
    width: 100%;

    display: flex;
    justify-content: center;
}

.setup-card {
    width: min(100%, 480px);

    padding: 26px;

    border-radius: 22px;

    background:
        rgba(255, 255, 255, .075);

    border:
        1px solid
        rgba(255, 255, 255, .1);

    backdrop-filter: blur(16px);

    box-shadow:
        0 25px 60px
        rgba(0, 0, 0, .28);
}

.setup-card h1 {
    margin: 0;

    text-align: center;

    font-size:
        clamp(28px, 8vw, 40px);
}

.muted {
    margin: 8px 0 22px;

    text-align: center;

    color:
        rgba(255, 255, 255, .68);

    line-height: 1.5;
}


/* =========================
   MODE
========================= */

.mode-row {
    display: grid;

    grid-template-columns:
        repeat(3, 1fr);

    gap: 8px;

    margin-bottom: 16px;
}

.mode-btn {
    min-height: 48px;

    border:
        1px solid
        rgba(255, 255, 255, .12);

    border-radius: 12px;

    background:
        rgba(255, 255, 255, .06);

    color: #fff;

    padding: 8px;

    font-weight: 600;

    transition:
        background .15s,
        border-color .15s,
        transform .12s;
}

.mode-btn:hover {
    background:
        rgba(255, 255, 255, .11);
}

.mode-btn:active {
    transform: scale(.98);
}

.mode-btn.active {
    background:
        #2563eb;

    border-color:
        #60a5fa;

    box-shadow:
        0 5px 18px
        rgba(37, 99, 235, .25);
}


/* =========================
   FORM
========================= */

.field {
    display: flex;

    flex-direction: column;

    gap: 7px;

    margin-bottom: 14px;
}

.field label {
    font-size: 13px;

    color:
        rgba(255, 255, 255, .7);
}

.field select,
.room-input {
    width: 100%;

    border:
        1px solid
        rgba(255, 255, 255, .14);

    border-radius: 11px;

    padding: 12px;

    outline: none;

    color: #fff;

    background:
        rgba(0, 0, 0, .25);
}

.field select:focus,
.room-input:focus {
    border-color:
        rgba(96, 165, 250, .75);

    box-shadow:
        0 0 0 3px
        rgba(59, 130, 246, .15);
}

.field select option {
    color: #111;
    background: #fff;
}


/* =========================
   ONLINE PANEL
========================= */

.online-panel {
    margin:
        12px 0 16px;

    padding: 15px;

    border-radius: 15px;

    background:
        rgba(0, 0, 0, .18);

    border:
        1px solid
        rgba(255, 255, 255, .1);
}

.online-title {
    font-size: 17px;

    font-weight: 800;

    margin-bottom: 12px;
}


/*
 * HTML đang dùng:
 * .online-divider
 */

.online-divider {
    display: flex;

    align-items: center;

    gap: 10px;

    margin: 13px 0;

    color:
        rgba(255, 255, 255, .45);

    font-size: 13px;

    text-align: center;
}

.online-divider::before,
.online-divider::after {
    content: "";

    height: 1px;

    flex: 1;

    background:
        rgba(255, 255, 255, .1);
}


.join-row {
    display: grid;

    grid-template-columns:
        1fr auto;

    gap: 8px;
}

.room-input {
    text-transform: uppercase;

    letter-spacing: 2px;
}

.room-input::placeholder {
    text-transform: none;

    letter-spacing: 0;
}

.room-message {
    min-height: 20px;

    margin-top: 9px;

    color:
        rgba(255, 255, 255, .7);

    font-size: 13px;

    line-height: 1.4;
}


/* =========================
   CREATED ROOM
========================= */

.created-room {
    margin-top: 14px;

    padding-top: 14px;

    border-top:
        1px solid
        rgba(255, 255, 255, .1);

    text-align: center;
}

.waiting-icon {
    font-size: 30px;

    margin-bottom: 5px;
}

.waiting-text {
    margin-top: 5px;

    text-align: center;

    color:
        rgba(255, 255, 255, .7);

    font-size: 13px;

    line-height: 1.4;
}

.room-code {
    margin: 10px 0;

    padding: 13px;

    border-radius: 11px;

    background:
        rgba(0, 0, 0, .28);

    border:
        1px solid
        rgba(255, 255, 255, .07);

    text-align: center;

    font-size: 26px;

    font-weight: 900;

    letter-spacing: 5px;
}

.created-room .action-btn {
    width: 100%;

    margin-top: 7px;
}


/* =========================
   GAME
========================= */

.game {
    width: min(100%, 760px);
}


/* =========================
   ONLINE GAME BAR
========================= */

.online-game-bar {
    display: flex;

    align-items: center;

    gap: 7px;

    margin-bottom: 10px;

    padding: 9px 11px;

    border-radius: 12px;

    background:
        rgba(37, 99, 235, .18);

    border:
        1px solid
        rgba(96, 165, 250, .2);

    font-size: 13px;
}

.online-game-bar > span {
    flex: 1;

    min-width: 0;
}

.online-game-bar strong {
    letter-spacing: 2px;
}

.online-game-bar .action-btn {
    padding:
        8px 10px;

    font-size: 12px;
}


/* =========================
   GAME HEADER
========================= */

.game-head {
    display: grid;

    grid-template-columns:
        1fr auto 1fr;

    align-items: center;

    gap: 10px;

    margin-bottom: 12px;
}

.player-card {
    min-width: 0;

    display: flex;

    align-items: center;

    gap: 9px;

    padding: 10px 12px;

    border-radius: 13px;

    background:
        rgba(255, 255, 255, .08);

    border:
        1px solid
        rgba(255, 255, 255, .06);
}

.player-card div {
    min-width: 0;

    flex: 1;
}

.player-card strong,
.player-card small {
    display: block;

    overflow: hidden;

    white-space: nowrap;

    text-overflow: ellipsis;
}

.player-card strong {
    font-size: 14px;
}

.player-card small {
    margin-top: 2px;

    color:
        rgba(255, 255, 255, .55);

    font-size: 11px;
}

.player-card b {
    font-size: 18px;

    white-space: nowrap;
}

.dot {
    width: 12px;
    height: 12px;

    flex:
        0 0 12px;

    border-radius: 50%;
}

.white-dot {
    background: #fff;

    box-shadow:
        0 0 0 2px
        rgba(255, 255, 255, .2);
}

.black-dot {
    background: #111;

    box-shadow:
        0 0 0 2px
        rgba(255, 255, 255, .2);
}

.turn-pill {
    padding: 8px 11px;

    border-radius: 999px;

    background:
        #2563eb;

    font-size: 12px;

    font-weight: 800;

    white-space: nowrap;
}


/* =========================
   BOARD
========================= */

.board-wrap {
    width: 100%;

    display: flex;

    justify-content: center;
}


/*
 * BÀN CỜ LUÔN VUÔNG
 *
 * Không dùng height cố định.
 * aspect-ratio đảm bảo 8x8
 * luôn là hình vuông.
 */

.board {
    width:
        min(94vw, 620px);

    max-width: 620px;

    aspect-ratio: 1 / 1;

    display: grid;

    grid-template-columns:
        repeat(8, minmax(0, 1fr));

    grid-template-rows:
        repeat(8, minmax(0, 1fr));

    overflow: hidden;

    border:
        4px solid
        rgba(255, 255, 255, .12);

    border-radius: 12px;

    box-shadow:
        0 25px 60px
        rgba(0, 0, 0, .38);
}


/* =========================
   SQUARE
========================= */

.square {
    position: relative;

    width: 100%;
    height: 100%;

    min-width: 0;
    min-height: 0;

    padding: 0;

    border: 0;

    display: flex;

    align-items: center;
    justify-content: center;

    aspect-ratio: 1 / 1;
}

.square.light {
    background:
        #f0d9b5;
}

.square.dark {
    background:
        #b58863;
}

.square.selected {
    box-shadow:
        inset 0 0 0 4px
        rgba(250, 204, 21, .95) !important;
}

.square.last {
    box-shadow:
        inset 0 0 0 999px
        rgba(250, 204, 21, .15);
}

.square.check {
    box-shadow:
        inset 0 0 0 999px
        rgba(239, 68, 68, .3),
        inset 0 0 0 4px
        rgba(239, 68, 68, .9);
}


/* =========================
   PIECES
========================= */

.piece {
    position: relative;

    z-index: 5;

    width: 78%;
    height: 78%;

    display: flex;

    align-items: center;
    justify-content: center;

    font-family:
        "Arial Unicode MS",
        "Segoe UI Symbol",
        "Noto Sans Symbols 2",
        sans-serif;

    /*
     * Kích thước theo viewport nhưng
     * bị giới hạn để không vượt ô.
     */
    font-size:
        clamp(27px, 7vw, 58px);

    line-height: 1;

    user-select: none;

    pointer-events: none;

    transform:
        translateY(-1%);
}


/* =========================
   COORDINATES
========================= */

.coord {
    position: absolute;

    z-index: 8;

    pointer-events: none;

    font-size: 10px;

    font-weight: 800;

    opacity: .7;
}

.coord.file {
    right: 4px;
    bottom: 2px;
}

.coord.rank {
    left: 4px;
    top: 2px;
}

.square.light .coord {
    color:
        rgba(90, 55, 30, .75);
}

.square.dark .coord {
    color:
        rgba(255, 255, 255, .75);
}


/* =========================
   ACTIONS
========================= */

.actions {
    display: flex;

    justify-content: center;

    gap: 8px;

    flex-wrap: wrap;

    margin-top: 13px;
}

.actions .action-btn {
    min-width: 120px;
}


/* =========================
   MESSAGE
========================= */

.message {
    min-height: 22px;

    margin: 10px 0;

    text-align: center;

    color:
        rgba(255, 255, 255, .75);

    font-size: 14px;
}


/* =========================
   MOVES
========================= */

.moves-panel {
    margin-top: 10px;

    padding: 14px;

    border-radius: 15px;

    background:
        rgba(255, 255, 255, .07);

    border:
        1px solid
        rgba(255, 255, 255, .06);
}

.moves-panel h3 {
    margin: 0 0 9px;

    font-size: 15px;
}

.moves {
    max-height: 160px;

    overflow-y: auto;

    display: grid;

    grid-template-columns:
        1fr 1fr;

    gap: 4px;

    font-size: 13px;
}

.move-row {
    display: flex;

    gap: 8px;

    padding: 5px 7px;

    border-radius: 7px;

    background:
        rgba(255, 255, 255, .04);
}

.move-no {
    width: 26px;

    color:
        rgba(255, 255, 255, .35);
}


/* =========================
   RESULT
========================= */

.overlay {
    position: fixed;

    inset: 0;

    z-index: 100;

    display: flex;

    align-items: center;
    justify-content: center;

    padding: 20px;

    background:
        rgba(0, 0, 0, .65);

    backdrop-filter:
        blur(7px);
}

.result-card {
    width: min(100%, 380px);

    padding: 28px;

    border-radius: 22px;

    text-align: center;

    background:
        #172033;

    border:
        1px solid
        rgba(255, 255, 255, .1);

    box-shadow:
        0 30px 80px
        rgba(0, 0, 0, .5);
}

.result-icon {
    font-size: 55px;
}

.result-card h2 {
    margin: 8px 0;

    font-size: 27px;
}

.result-card p {
    margin: 0 0 20px;

    color:
        rgba(255, 255, 255, .7);
}

.result-actions {
    display: flex;

    gap: 8px;
}

.result-actions > * {
    flex: 1;
}


/* =========================
   UTILITY
========================= */

.hidden {
    display: none !important;
}


/* =========================
   MOBILE
========================= */

@media (max-width: 700px) {

    .page {
        padding:
            11px
            8px
            25px;
    }

    .topbar {
        margin-bottom: 12px;
    }

    .brand {
        font-size: 17px;
    }

    .ghost-btn {
        padding:
            9px 9px;

        font-size: 12px;
    }

    .setup-card {
        padding:
            20px 15px;

        border-radius: 18px;
    }

    .mode-row {
        grid-template-columns:
            1fr;
    }

    .game-head {
        grid-template-columns:
            1fr;

        gap: 7px;
    }

    .turn-pill {
        justify-self: center;

        order: -1;
    }

    .player-card {
        width: 100%;
    }

    /*
     * iPhone:
     * bàn cờ gần sát chiều ngang
     * nhưng vẫn luôn vuông.
     */

    .board {
        width:
            min(97vw, 620px);

        border-width: 3px;

        border-radius: 8px;
    }

    .piece {
        width: 76%;
        height: 76%;

        font-size:
            clamp(
                24px,
                8.7vw,
                50px
            );
    }

    .coord {
        font-size: 8px;
    }

    .actions {
        display: grid;

        grid-template-columns:
            1fr 1fr;
    }

    .actions .action-btn {
        min-width: 0;
    }

    .actions .danger {
        grid-column:
            1 / -1;
    }

    .online-game-bar {
        flex-wrap: wrap;
    }

    .online-game-bar > span {
        flex-basis: 100%;
    }

    .online-game-bar .action-btn {
        flex: 1;
    }

    .join-row {
        grid-template-columns:
            1fr;
    }

    .result-actions {
        flex-direction: column;
    }

}


/* =========================
   SMALL IPHONE
========================= */

@media (max-width: 380px) {

    .page {
        padding-left: 6px;
        padding-right: 6px;
    }

    .board {
        width: 96vw;
    }

    .piece {
        font-size:
            clamp(
                22px,
                8.5vw,
                38px
            );
    }

    .player-card {
        padding:
            9px 10px;
    }

    .player-card strong {
        font-size: 13px;
    }

    .player-card b {
        font-size: 16px;
    }

}
