*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

:root {
    --bg: #e5dfd4;
    --panel: #ebe6dc;
    --surface: #f2eee6;
    --text: #2e2a24;
    --muted: #6e6860;
    --line: rgba(50, 45, 40, 0.1);
    --gold: #b08d46;
    --gold-light: #c9a55a;
    --gold-dark: #8f6e2e;
    --gold-text: #ffffff;
    --green: #2e9d6c;
    --danger: #b94b42;
    --white-square: #f0dfbd;
    --black-square: #9b6f43;
    --selected: #d9b75a;
    --last: rgba(176, 141, 70, 0.35);
}

[data-theme="dark"] {
    --bg: #cfc4b3;
    --panel: #e3d9ca;
    --surface: #ece2d4;
    --text: #25211c;
    --muted: #5a534a;
    --line: rgba(45, 38, 30, 0.16);
    --gold: #9d762d;
    --gold-light: #bb9140;
    --gold-dark: #745620;
    --gold-text: #1a1816;
    --green: #277d55;
    --danger: #c9695f;
    --white-square: #ead8b7;
    --black-square: #9f6b3f;
    --selected: #caa64d;
    --last: rgba(157, 118, 45, 0.34);
}

html,
body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: 'Nunito', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

button,
input {
    font: inherit;
}

#app {
    width: 100%;
    height: 100%;
    max-width: 1600px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    background:
        radial-gradient(circle at 50% 18%, rgba(169, 130, 54, 0.1), transparent 30rem),
        var(--bg);
}

.app-header,
.bottom-bar,
.side-panel,
.score-strip {
    background: var(--panel);
    border-color: var(--line);
}

.app-header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.65rem max(1rem, env(safe-area-inset-right)) 0.65rem max(1rem, env(safe-area-inset-left));
    border-bottom: 1px solid var(--line);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.18);
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 0.65rem;
    color: inherit;
    text-decoration: none;
    font-weight: 800;
    letter-spacing: 0.01em;
}

.brand__mark {
    width: 2.35rem;
    aspect-ratio: 1;
    display: grid;
    place-items: center;
    border-radius: 0.8rem;
    background: linear-gradient(145deg, var(--gold-light), var(--gold), var(--gold-dark));
    color: var(--gold-text);
    font-size: 1.45rem;
    font-weight: 900;
}

.header-actions,
.controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.header-link {
    min-height: 2.6rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 0.85rem;
    border: 1px solid var(--line);
    border-radius: 0.8rem;
    background: var(--surface);
    color: var(--gold);
    text-decoration: none;
    font-weight: 800;
}

.icon-btn,
.btn {
    border: 1px solid var(--line);
    background: var(--surface);
    color: var(--text);
    min-height: 2.6rem;
    border-radius: 0.8rem;
    cursor: pointer;
    transition: transform 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.icon-btn {
    width: 2.6rem;
    font-weight: 800;
}

.btn {
    padding: 0 1rem;
    font-weight: 800;
}

.btn--gold {
    background: linear-gradient(145deg, var(--gold-light), var(--gold), var(--gold-dark));
    color: var(--gold-text);
    border-color: transparent;
}

.icon-btn:active,
.btn:active {
    transform: scale(0.96);
}

.btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.game-layout {
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    grid-template-columns: 5.5rem minmax(0, 1fr) 5.5rem;
}

.side-panel {
    border-inline: 1px solid var(--line);
    padding: 0.65rem 0.45rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    text-align: center;
    color: var(--muted);
    position: relative;
}

.side-panel strong {
    display: block;
    color: var(--text);
}

.side-panel span {
    display: block;
    margin-top: 0.25rem;
    font-size: 0.78rem;
    line-height: 1.35;
}

.side-panel em {
    display: none;
    position: absolute;
    bottom: 1rem;
    padding: 0.25rem 0.55rem;
    border-radius: 999px;
    background: rgba(53, 163, 107, 0.14);
    color: var(--green);
    font-size: 0.72rem;
    font-style: normal;
    font-weight: 800;
}

.side-panel.is-active {
    box-shadow: inset 0 0 0 2px rgba(53, 163, 107, 0.25);
}

.side-panel.is-active em {
    display: inline-flex;
}

.player-avatar {
    width: 3rem;
    aspect-ratio: 1;
    display: grid;
    place-items: center;
    border-radius: 1rem;
    background: var(--surface);
    color: var(--gold);
    font-size: 1.75rem;
    font-weight: 900;
    border: 1px solid var(--line);
}

.board-wrap {
    min-width: 0;
    min-height: 0;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    place-items: center;
    padding: 0.45rem;
    gap: 0.45rem;
}

.score-strip {
    width: min(100%, 48rem);
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.5rem 0.8rem;
    border: 1px solid var(--line);
    border-radius: 0.9rem;
    color: var(--muted);
    font-weight: 800;
    font-size: 0.85rem;
}

.board {
    width: min(100%, calc(100vh - 11rem), 58rem);
    aspect-ratio: 1;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    border: 0.65rem solid var(--panel);
    border-radius: 1rem;
    overflow: hidden;
    box-shadow: 0 1.25rem 3.5rem rgba(0, 0, 0, 0.36), 0 0 0 1px var(--line);
    touch-action: manipulation;
}

.square {
    position: relative;
    border: 0;
    border-radius: 0;
    display: grid;
    place-items: center;
    color: #161b26;
    cursor: pointer;
}

.square[data-tooltip]::after {
    content: attr(data-tooltip);
    position: absolute;
    left: 50%;
    bottom: calc(100% + 0.45rem);
    z-index: 30;
    width: max-content;
    max-width: min(16rem, 70vw);
    padding: 0.5rem 0.65rem;
    border: 1px solid color-mix(in srgb, var(--line) 70%, var(--gold) 30%);
    border-radius: 0.65rem;
    background: color-mix(in srgb, var(--panel) 94%, #ffffff 6%);
    color: var(--text);
    box-shadow: 0 10px 24px rgba(45, 38, 30, 0.22);
    font-size: 0.75rem;
    font-weight: 800;
    line-height: 1.35;
    text-align: center;
    white-space: normal;
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, 0.25rem);
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.square[data-tooltip]::before {
    content: '';
    position: absolute;
    left: 50%;
    bottom: calc(100% + 0.12rem);
    z-index: 31;
    width: 0.55rem;
    height: 0.55rem;
    background: color-mix(in srgb, var(--panel) 94%, #ffffff 6%);
    border-right: 1px solid color-mix(in srgb, var(--line) 70%, var(--gold) 30%);
    border-bottom: 1px solid color-mix(in srgb, var(--line) 70%, var(--gold) 30%);
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, 0.25rem) rotate(45deg);
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.square[data-tooltip]:hover::after,
.square[data-tooltip]:focus-visible::after,
.square[data-tooltip]:hover::before,
.square[data-tooltip]:focus-visible::before {
    opacity: 1;
    transform: translate(-50%, 0) rotate(0);
}

.square[data-tooltip]:hover::before,
.square[data-tooltip]:focus-visible::before {
    transform: translate(-50%, 0) rotate(45deg);
}

.square[data-file="0"][data-tooltip]::after,
.square[data-file="1"][data-tooltip]::after {
    left: 0.2rem;
    transform: translate(0, 0.25rem);
}

.square[data-file="0"][data-tooltip]:hover::after,
.square[data-file="1"][data-tooltip]:hover::after,
.square[data-file="0"][data-tooltip]:focus-visible::after,
.square[data-file="1"][data-tooltip]:focus-visible::after {
    transform: translate(0, 0);
}

.square[data-file="6"][data-tooltip]::after,
.square[data-file="7"][data-tooltip]::after {
    left: auto;
    right: 0.2rem;
    transform: translate(0, 0.25rem);
}

.square[data-file="6"][data-tooltip]:hover::after,
.square[data-file="7"][data-tooltip]:hover::after,
.square[data-file="6"][data-tooltip]:focus-visible::after,
.square[data-file="7"][data-tooltip]:focus-visible::after {
    transform: translate(0, 0);
}

.square.light {
    background: var(--white-square);
}

.square.dark {
    background: var(--black-square);
}

.square.is-selected {
    background: var(--selected);
}

.square.is-last::after {
    content: '';
    position: absolute;
    inset: 0;
    background: var(--last);
    pointer-events: none;
}

.square.is-check {
    background: var(--danger);
}

.piece {
    position: relative;
    z-index: 1;
    display: inline-block;
    font-size: clamp(2rem, 7vw, 4.6rem);
    line-height: 1;
    font-weight: 400;
    text-shadow: 0 2px 2px rgba(0, 0, 0, 0.16);
    user-select: none;
}

.piece.white {
    color: #fbf3df;
    filter: drop-shadow(0 1px 0 #5d4323) drop-shadow(0 0 1px #5d4323);
}

.piece.black {
    color: #211813;
    filter: drop-shadow(0 1px 0 rgba(255, 255, 255, 0.24));
}

.square.is-move::before,
.square.is-capture::before {
    content: '';
    position: absolute;
    z-index: 2;
    width: 26%;
    aspect-ratio: 1;
    border-radius: 999px;
    background: rgba(53, 163, 107, 0.72);
}

.square.is-capture::before {
    width: 78%;
    background: transparent;
    border: 0.35rem solid rgba(53, 163, 107, 0.78);
}

.coord {
    position: absolute;
    z-index: 3;
    left: 0.25rem;
    bottom: 0.15rem;
    font-size: 0.62rem;
    font-weight: 800;
    opacity: 0.46;
    pointer-events: none;
}

.status {
    width: min(100%, 42rem);
    min-height: 2.4rem;
    display: grid;
    place-items: center;
    text-align: center;
    color: var(--muted);
    font-weight: 800;
}

.status strong {
    color: var(--gold);
}

.bottom-bar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.8rem;
    flex-wrap: wrap;
    padding: 0.55rem max(1rem, env(safe-area-inset-right)) max(0.65rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
    border-top: 1px solid var(--line);
    box-shadow: 0 -1px 4px rgba(0, 0, 0, 0.18);
}

.mode-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.55rem;
    color: var(--muted);
    font-weight: 800;
    cursor: pointer;
}

.mode-toggle input {
    width: 1.1rem;
    height: 1.1rem;
    accent-color: var(--gold);
}

.mode-toggle select {
    min-height: 1.9rem;
    border: 1px solid var(--line);
    border-radius: 0.55rem;
    background: var(--panel);
    color: var(--text);
    padding: 0 0.55rem;
    font-weight: 800;
    cursor: pointer;
}

.mode-toggle select:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.share-result-area {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    flex-wrap: wrap;
    color: var(--muted);
    font-size: 0.85rem;
    font-weight: 800;
}

.share-result-area[hidden] {
    display: none;
}

.btn--sm {
    min-height: 2.15rem;
    padding: 0 0.75rem;
    border-radius: 0.65rem;
    font-size: 0.82rem;
}

.dialog {
    width: min(92vw, 28rem);
    margin: auto;
    border: 1px solid var(--line);
    border-radius: 1rem;
    background: var(--panel);
    color: var(--text);
    padding: 1.25rem;
    box-shadow: 0 2rem 5rem rgba(0, 0, 0, 0.35);
}

.dialog::backdrop {
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(6px);
}

.dialog form {
    position: relative;
}

.dialog h2 {
    margin-bottom: 0.75rem;
}

.dialog p {
    color: var(--muted);
    line-height: 1.6;
    margin-top: 0.7rem;
}

.online-actions,
.online-link-row {
    display: flex;
    gap: 0.6rem;
    flex-wrap: wrap;
    margin-top: 1rem;
}

.online-link-box,
.online-join-box {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--line);
}

.online-link-box[hidden],
.online-join-box[hidden] {
    display: none;
}

.online-link-box label,
.online-join-box label {
    display: block;
    margin-bottom: 0.45rem;
    color: var(--muted);
    font-size: 0.85rem;
    font-weight: 800;
}

.online-link-row input,
.online-join-box input {
    min-width: 0;
    flex: 1;
    width: 100%;
    min-height: 2.8rem;
    padding: 0 0.8rem;
    border: 1px solid var(--line);
    border-radius: 0.75rem;
    background: var(--surface);
    color: var(--text);
}

#online-waiting {
    color: var(--gold);
    font-weight: 800;
}

.dialog__close {
    position: absolute;
    right: 0;
    top: 0;
    min-width: 4rem;
    height: 2rem;
    padding: 0 0.65rem;
    border: 0;
    border-radius: 999px;
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    font-size: 0.78rem;
    font-weight: 800;
}

.result-dialog {
    text-align: center;
}

.result-dialog h2 {
    color: var(--gold);
    font-size: 1.65rem;
}

.result-dialog p {
    font-size: 1rem;
}

.result-dialog form {
    display: grid;
    gap: 0.8rem;
}

.result-new-game {
    margin-top: 0.4rem;
}

@media (max-width: 760px) {
    .app-header {
        gap: 0.45rem;
        padding: 0.38rem 0.55rem;
    }

    .brand span:last-child {
        font-size: 0.9rem;
    }

    .brand__mark {
        width: 1.95rem;
        border-radius: 0.65rem;
        font-size: 1.15rem;
    }

    .header-actions {
        gap: 0.35rem;
        flex-wrap: nowrap;
    }

    .header-link,
    .icon-btn {
        min-height: 2.05rem;
        height: 2.05rem;
        border-radius: 0.62rem;
        font-size: 0.78rem;
    }

    .header-link {
        padding: 0 0.6rem;
    }

    .icon-btn {
        width: 2.05rem;
    }

    .game-layout {
        grid-template-columns: 1fr;
        grid-template-rows: auto minmax(0, 1fr) auto;
    }

    .side-panel {
        min-height: 2.55rem;
        padding: 0.25rem 0.55rem;
        flex-direction: row;
        justify-content: flex-start;
        text-align: left;
        border-inline: 0;
        border-block: 1px solid var(--line);
    }

    .side-panel--black {
        order: 1;
    }

    .board-wrap {
        order: 2;
        gap: 0.45rem;
        padding: 0.5rem;
    }

    .side-panel--white {
        order: 3;
    }

    .player-avatar {
        width: 1.9rem;
        border-radius: 0.55rem;
        font-size: 1.05rem;
    }

    .side-panel em {
        right: 0.8rem;
        bottom: auto;
        font-size: 0.62rem;
        padding: 0.15rem 0.42rem;
    }

    .side-panel span {
        display: none;
    }

    .side-panel strong {
        font-size: 0.82rem;
    }

    .board {
        width: min(100%, calc(100dvh - 12.8rem), 96vw);
        max-width: 96vw;
        border-width: 0.28rem;
        border-radius: 0.8rem;
    }

    .piece {
        font-size: clamp(1.45rem, 9vw, 3rem);
    }

    .coord {
        display: none;
    }

    .score-strip {
        width: min(100%, 96vw);
        padding: 0.32rem 0.55rem;
        border-radius: 0.65rem;
        font-size: 0.72rem;
    }

    .status {
        min-height: 1.55rem;
        font-size: 0.76rem;
        line-height: 1.25;
        padding: 0 0.4rem;
    }

    .bottom-bar {
        gap: 0.35rem;
        padding: 0.35rem 0.45rem max(0.42rem, env(safe-area-inset-bottom));
    }

    .controls {
        justify-content: center;
        gap: 0.35rem;
    }

    .btn {
        min-height: 2rem;
        padding: 0 0.55rem;
        border-radius: 0.55rem;
        font-size: 0.72rem;
    }

    .mode-toggle {
        font-size: 0.72rem;
    }

    .mode-toggle input {
        width: 0.92rem;
        height: 0.92rem;
    }
}

@media (max-width: 380px) {
    .brand span:last-child {
        display: none;
    }

    .board-wrap {
        gap: 0.25rem;
        padding: 0.28rem;
    }

    .board {
        width: min(100%, calc(100dvh - 10.8rem), 97vw);
        border-width: 0.22rem;
    }

    .piece {
        font-size: clamp(1.2rem, 8.8vw, 2.5rem);
    }

    .side-panel {
        min-height: 2.15rem;
        padding-block: 0.18rem;
    }

    .player-avatar {
        width: 1.55rem;
        font-size: 0.78rem;
    }

    .side-panel strong {
        font-size: 0.74rem;
    }

    .score-strip {
        font-size: 0.66rem;
        padding: 0.25rem 0.42rem;
    }

    .status {
        min-height: 1.25rem;
        font-size: 0.68rem;
    }

    .btn {
        min-height: 1.82rem;
        padding: 0 0.4rem;
        font-size: 0.66rem;
    }
}

@media (max-height: 500px) and (orientation: landscape) {
    .app-header {
        padding-block: 0.35rem;
    }

    .bottom-bar {
        padding-block: 0.35rem;
    }

    .game-layout {
        grid-template-columns: 5.2rem minmax(0, 1fr) 5.2rem;
    }

    .side-panel {
        padding: 0.4rem;
    }

    .side-panel span,
    .side-panel em,
    .coord {
        display: none;
    }

    .player-avatar {
        width: 2.4rem;
        font-size: 1.45rem;
    }

    .board-wrap {
        padding: 0.35rem;
        gap: 0.35rem;
    }

    .board {
        width: min(100%, 68vh);
        border-width: 0.3rem;
    }

    .status {
        min-height: 1.4rem;
        font-size: 0.78rem;
    }
}

/* ========================== UI/UX REFRESH ========================== */
html,
body {
    min-height: 100dvh;
}

#app {
    height: 100dvh;
    max-width: none;
    background:
        radial-gradient(circle at 50% 0%, rgba(157, 118, 45, 0.16), transparent 26rem),
        linear-gradient(180deg, color-mix(in srgb, var(--bg) 94%, #ffffff 6%), var(--bg));
}

.app-header {
    min-height: 3.4rem;
    padding: 0.45rem clamp(0.7rem, 2vw, 1.25rem);
    background: color-mix(in srgb, var(--panel) 96%, var(--gold) 4%);
}

.brand {
    min-width: 0;
}

.brand__mark,
.player-avatar {
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25), 0 3px 10px rgba(45, 38, 30, 0.14);
}

.brand span:last-child {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.header-link,
.icon-btn,
.btn {
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.28);
}

.game-layout {
    min-height: 0;
    grid-template-columns: clamp(4rem, 5.4vw, 5.4rem) minmax(0, 1fr) clamp(4rem, 5.4vw, 5.4rem);
    gap: clamp(0.25rem, 0.7vw, 0.5rem);
    padding: clamp(0.25rem, 0.7vw, 0.5rem);
}

.side-panel {
    border: 1px solid var(--line);
    border-radius: 1rem;
    background:
        linear-gradient(180deg, color-mix(in srgb, var(--panel) 96%, #ffffff 4%), var(--panel));
    box-shadow: 0 8px 24px rgba(45, 38, 30, 0.1);
}

.side-panel.is-active {
    border-color: color-mix(in srgb, var(--green) 55%, var(--line));
    box-shadow: 0 8px 24px rgba(45, 38, 30, 0.1), inset 0 0 0 2px rgba(39, 125, 85, 0.2);
}

.board-wrap {
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 1.2rem;
    background:
        linear-gradient(180deg, color-mix(in srgb, var(--surface) 92%, #ffffff 8%), var(--surface));
    box-shadow: 0 10px 30px rgba(45, 38, 30, 0.11);
    grid-template-rows: auto minmax(0, 1fr) auto;
}

.score-strip {
    border-radius: 0.75rem;
    background: color-mix(in srgb, var(--panel) 84%, #ffffff 16%);
}

.board {
    width: min(calc(100vw - 11.5rem), calc(100dvh - 10.2rem), 54rem);
    border: clamp(0.28rem, 0.7vw, 0.55rem) solid color-mix(in srgb, var(--panel) 82%, var(--gold) 18%);
    border-radius: 0.95rem;
    box-shadow:
        0 16px 36px rgba(45, 38, 30, 0.22),
        0 0 0 1px rgba(45, 38, 30, 0.08);
}

.square.light {
    background: linear-gradient(145deg, color-mix(in srgb, var(--white-square) 96%, #ffffff 4%), var(--white-square));
}

.square.dark {
    background: linear-gradient(145deg, color-mix(in srgb, var(--black-square) 92%, #ffffff 8%), var(--black-square));
}

.square.is-selected {
    box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.34);
}

.square.is-check {
    background: linear-gradient(145deg, #d97968, var(--danger));
}

.status {
    min-height: 2rem;
    padding: 0 0.6rem;
    border-radius: 0.75rem;
    background: rgba(255, 255, 255, 0.16);
}

.bottom-bar {
    display: grid;
    grid-template-columns: minmax(0, auto) minmax(0, auto) minmax(0, auto) minmax(0, auto);
    align-items: center;
    min-height: 3.7rem;
    background: color-mix(in srgb, var(--panel) 96%, var(--gold) 4%);
}

.controls {
    display: grid;
    grid-template-columns: repeat(4, minmax(4.4rem, auto));
}

.btn {
    min-height: 2.35rem;
    padding: 0 0.85rem;
}

.mode-toggle {
    padding: 0.45rem 0.65rem;
    border: 1px solid var(--line);
    border-radius: 0.8rem;
    background: var(--surface);
}

.tooltip-toggle {
    color: var(--text);
}

.dialog {
    border-radius: 1.1rem;
}

@media (max-width: 760px) {
    #app {
        height: 100dvh;
    }

    .app-header {
        min-height: 2.7rem;
        padding: 0.32rem 0.5rem;
    }

    .brand__mark {
        width: 2rem;
    }

    .game-layout {
        grid-template-columns: 1fr;
        grid-template-rows: 2.35rem minmax(0, 1fr) 2.35rem;
        gap: 0.28rem;
        padding: 0.28rem;
    }

    .side-panel {
        min-height: 0;
        border-radius: 0.75rem;
        padding: 0.25rem 0.5rem;
    }

    .player-avatar {
        width: 1.75rem;
        border-radius: 0.5rem;
        font-size: 1rem;
    }

    .board-wrap {
        border-radius: 0.85rem;
        gap: 0.25rem;
        padding: 0.28rem;
    }

    .score-strip {
        min-height: 1.65rem;
        padding: 0.2rem 0.5rem;
        font-size: 0.68rem;
    }

    .board {
        width: min(98vw, calc(100dvh - 12.4rem));
        border-width: 0.22rem;
        border-radius: 0.7rem;
    }

    .piece {
        font-size: clamp(1.25rem, 8.7vw, 2.8rem);
    }

    .status {
        width: min(100%, 97vw);
        min-height: 1.35rem;
        padding: 0 0.35rem;
        font-size: 0.68rem;
        line-height: 1.2;
    }

    .bottom-bar {
        grid-template-columns: 1fr;
        gap: 0.25rem;
        min-height: 4.8rem;
        padding: 0.32rem 0.42rem max(0.38rem, env(safe-area-inset-bottom));
    }

    .controls {
        width: 100%;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 0.28rem;
    }

    .btn {
        min-height: 1.9rem;
        padding: 0 0.25rem;
        border-radius: 0.55rem;
        font-size: 0.68rem;
    }

    .mode-toggle {
        justify-self: center;
        min-height: 1.65rem;
        padding: 0.16rem 0.5rem;
        font-size: 0.68rem;
    }

    .share-result-area {
        font-size: 0.66rem;
    }
}

@media (max-width: 380px) {
    .app-header {
        min-height: 2.35rem;
    }

    .header-link,
    .icon-btn {
        min-height: 1.85rem;
        height: 1.85rem;
        border-radius: 0.5rem;
    }

    .icon-btn {
        width: 1.85rem;
    }

    .game-layout {
        grid-template-rows: 2.05rem minmax(0, 1fr) 2.05rem;
        gap: 0.22rem;
        padding: 0.22rem;
    }

    .board-wrap {
        padding: 0.22rem;
        gap: 0.2rem;
    }

    .board {
        width: min(99vw, calc(100dvh - 11.35rem));
        border-width: 0.18rem;
    }

    .piece {
        font-size: clamp(1.05rem, 8.4vw, 2.35rem);
    }

    .bottom-bar {
        min-height: 4.35rem;
    }

    .btn {
        min-height: 1.7rem;
        font-size: 0.62rem;
    }

    .mode-toggle {
        min-height: 1.45rem;
        font-size: 0.62rem;
    }
}

@media (max-height: 500px) and (orientation: landscape) {
    .app-header {
        min-height: 2.2rem;
        padding-block: 0.2rem;
    }

    .game-layout {
        grid-template-columns: 3.6rem minmax(0, 1fr) 3.6rem;
        grid-template-rows: minmax(0, 1fr);
        gap: 0.25rem;
        padding: 0.25rem;
    }

    .side-panel {
        border-radius: 0.65rem;
        padding: 0.2rem;
    }

    .board-wrap {
        padding: 0.22rem;
        gap: 0.2rem;
    }

    .board {
        width: min(calc(100vw - 7.4rem), calc(100dvh - 6.4rem));
        border-width: 0.18rem;
    }

    .bottom-bar {
        display: flex;
        min-height: 2.25rem;
        gap: 0.3rem;
        padding-block: 0.22rem;
    }

    .controls {
        display: flex;
        width: auto;
    }

    .btn,
    .mode-toggle {
        min-height: 1.65rem;
        font-size: 0.62rem;
    }
}

/* ========================== BOARD FIT + HUD POLISH ========================== */
.board-wrap {
    grid-template-rows: auto minmax(0, 1fr);
}

.score-strip {
    width: min(100%, 54rem);
    display: grid;
    grid-template-columns: auto auto minmax(0, 1fr);
    align-items: center;
    gap: 0.55rem;
    padding: 0.42rem;
    border: 1px solid color-mix(in srgb, var(--line) 80%, var(--gold) 20%);
    border-radius: 1rem;
    background:
        linear-gradient(180deg, color-mix(in srgb, var(--panel) 88%, #ffffff 12%), var(--panel));
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.28), 0 6px 16px rgba(45, 38, 30, 0.08);
}

.hud-chip,
.hud-status {
    min-height: 2.05rem;
    display: inline-flex;
    align-items: center;
    border-radius: 0.75rem;
}

.hud-chip {
    gap: 0.4rem;
    padding: 0 0.7rem;
    background: color-mix(in srgb, var(--surface) 82%, #ffffff 18%);
    border: 1px solid var(--line);
    color: var(--muted);
    white-space: nowrap;
}

.hud-chip span {
    font-size: 0.68rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.hud-chip strong {
    color: var(--text);
    font-size: 0.95rem;
    line-height: 1;
}

.hud-status {
    width: auto;
    min-width: 0;
    justify-content: center;
    padding: 0 0.85rem;
    background: rgba(39, 125, 85, 0.1);
    border: 1px solid rgba(39, 125, 85, 0.18);
    color: color-mix(in srgb, var(--green) 76%, var(--text));
    font-size: 0.88rem;
    line-height: 1.25;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.hud-status strong {
    color: var(--gold-dark);
}

.board {
    max-width: 100%;
    max-height: 100%;
    flex-shrink: 0;
}

@media (max-width: 760px) {
    .score-strip {
        width: 100%;
        grid-template-columns: auto auto minmax(0, 1fr);
        gap: 0.3rem;
        padding: 0.28rem;
        border-radius: 0.75rem;
    }

    .hud-chip,
    .hud-status {
        min-height: 1.55rem;
        border-radius: 0.55rem;
    }

    .hud-chip {
        gap: 0.25rem;
        padding: 0 0.42rem;
    }

    .hud-chip span {
        display: none;
    }

    .hud-chip strong {
        font-size: 0.75rem;
    }

    .hud-status {
        padding: 0 0.45rem;
        font-size: 0.67rem;
    }
}

@media (max-width: 420px) {
    .score-strip {
        grid-template-columns: auto auto minmax(0, 1fr);
    }

    .hud-chip {
        min-width: 2.1rem;
        justify-content: center;
        padding: 0 0.32rem;
    }

    .hud-status {
        justify-content: flex-start;
    }
}

/* ========================== DRAG + DROP ========================== */
.board.is-drag-enabled {
    touch-action: none;
}

.board.is-drag-enabled .piece.is-draggable {
    cursor: grab;
}

.board.is-dragging,
.board.is-dragging .piece.is-draggable {
    cursor: grabbing;
}

.board.is-dragging .square.is-selected .piece {
    opacity: 0.35;
}

.drag-ghost.piece {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 9999;
    pointer-events: none;
    opacity: 0.95;
    transform: translate(-50%, -50%);
    filter: drop-shadow(0 0.45rem 0.7rem rgba(0, 0, 0, 0.32));
}

/* ========================== TURN CLARITY + WIN MOMENT ========================== */
.side-panel.is-active {
    border-color: var(--gold);
    background:
        radial-gradient(circle at 50% 0%, rgba(224, 181, 86, 0.22), transparent 70%),
        var(--panel);
    box-shadow:
        0 0 0 3px rgba(224, 181, 86, 0.22),
        0 14px 34px rgba(0, 0, 0, 0.18);
}

.side-panel.is-active .player-avatar {
    background: linear-gradient(145deg, #f0cd7a, var(--gold), var(--gold-dark));
    color: #201a0c;
    border-color: transparent;
    transform: scale(1.08);
    box-shadow: 0 0 0 4px rgba(224, 181, 86, 0.22), 0 10px 24px rgba(0, 0, 0, 0.22);
}

.side-panel.is-active em {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 1.65rem;
    padding: 0.22rem 0.8rem;
    background: var(--gold);
    color: #201a0c;
    font-size: 0.78rem;
    letter-spacing: 0.08em;
    box-shadow: 0 6px 16px rgba(224, 181, 86, 0.28);
}

.hud-status {
    background: linear-gradient(145deg, rgba(224, 181, 86, 0.2), rgba(47, 158, 111, 0.16));
    border: 1px solid rgba(224, 181, 86, 0.35);
    color: var(--text);
    font-size: 0.98rem;
}

.hud-status strong {
    color: var(--gold);
}

.result-dialog {
    width: min(94vw, 34rem);
    padding: clamp(1.4rem, 4vw, 2.4rem);
    border: 2px solid rgba(224, 181, 86, 0.45);
    box-shadow: 0 2rem 6rem rgba(0, 0, 0, 0.52), 0 0 0 6px rgba(224, 181, 86, 0.1);
}

.result-dialog h2 {
    font-size: clamp(2.4rem, 9vw, 4.8rem);
    line-height: 0.98;
    letter-spacing: -0.05em;
    color: var(--gold);
    text-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
}

.result-dialog p {
    font-size: clamp(1.05rem, 3vw, 1.35rem);
    font-weight: 800;
}

.confetti {
    position: fixed;
    inset: 0;
    z-index: 9998;
    pointer-events: none;
    overflow: hidden;
}

.confetti span {
    position: absolute;
    top: -1rem;
    width: 0.72rem;
    height: 1.05rem;
    border-radius: 0.18rem;
    opacity: 0.95;
    animation: confetti-fall linear forwards;
}

@keyframes confetti-fall {
    0% {
        transform: translate3d(0, -1rem, 0) rotate(0deg);
        opacity: 1;
    }
    100% {
        transform: translate3d(var(--drift), 105vh, 0) rotate(calc(var(--spin) * 760deg));
        opacity: 0;
    }
}

@media (max-width: 760px) {
    .side-panel.is-active {
        box-shadow:
            0 0 0 2px rgba(224, 181, 86, 0.26),
            0 8px 22px rgba(0, 0, 0, 0.16);
    }

    .side-panel.is-active .player-avatar {
        transform: scale(1.04);
    }

    .side-panel.is-active em {
        min-height: 1.35rem;
        padding: 0.16rem 0.55rem;
        font-size: 0.66rem;
    }

    .hud-status {
        font-size: 0.78rem;
    }
}

/* ========================== SELECTED PIECE CLARITY ========================== */
.piece {
    transition: transform 0.14s ease, filter 0.14s ease, opacity 0.14s ease;
}

.square.is-selected {
    background:
        radial-gradient(circle at 50% 48%, rgba(20, 16, 10, 0.18), transparent 48%),
        linear-gradient(145deg, #c08b32, #9f7027, #6f4a1c) !important;
    outline: clamp(0.14rem, 0.42vw, 0.24rem) solid rgba(240, 205, 122, 0.72);
    outline-offset: clamp(-0.42rem, -0.75vw, -0.24rem);
    box-shadow:
        inset 0 0 0 clamp(0.26rem, 0.7vw, 0.42rem) rgba(21, 15, 8, 0.42),
        inset 0 0 0.85rem rgba(32, 20, 8, 0.34),
        0 0 0.75rem rgba(224, 181, 86, 0.38) !important;
    animation: selected-square-pulse 1.25s ease-in-out infinite alternate;
}

.square.is-selected .piece {
    z-index: 4;
    transform: scale(1.1);
    filter:
        drop-shadow(0 0.22rem 0.1rem rgba(0, 0, 0, 0.42))
        drop-shadow(0 0 0.45rem rgba(224, 181, 86, 0.42));
}

.square.is-selected .coord {
    color: #201a0c;
    opacity: 0.8;
}

@keyframes selected-square-pulse {
    from {
        box-shadow:
            inset 0 0 0 clamp(0.26rem, 0.7vw, 0.42rem) rgba(21, 15, 8, 0.4),
            inset 0 0 0.7rem rgba(32, 20, 8, 0.3),
            0 0 0.45rem rgba(224, 181, 86, 0.28);
    }
    to {
        box-shadow:
            inset 0 0 0 clamp(0.26rem, 0.7vw, 0.42rem) rgba(21, 15, 8, 0.48),
            inset 0 0 0.95rem rgba(32, 20, 8, 0.42),
            0 0 0.9rem rgba(224, 181, 86, 0.48);
    }
}

/* Coordinates should stay below pieces, including selected pieces. */
.coord {
    z-index: 0;
}

.piece {
    z-index: 2;
}

.square.is-selected .coord {
    z-index: 0;
}

.square .piece {
    z-index: 5 !important;
}

.square .coord {
    z-index: 0 !important;
    color: rgba(20, 16, 10, 0.42);
    opacity: 0.34;
}

.square.is-selected .coord {
    color: rgba(20, 16, 10, 0.32) !important;
    opacity: 0.16;
    text-shadow: none;
}

/* ========================== HELP GUIDE ========================== */
#help-dialog {
    width: min(94vw, 46rem);
    max-height: min(86vh, 48rem);
    overflow: hidden;
}

#help-dialog form {
    max-height: calc(min(86vh, 48rem) - 2.8rem);
    overflow: auto;
    padding-right: 0.25rem;
}

.help-content {
    display: grid;
    gap: 0.85rem;
    margin-top: 0.85rem;
}

.help-content section {
    padding: 0.85rem;
    border: 1px solid var(--line);
    border-radius: 0.9rem;
    background: color-mix(in srgb, var(--surface) 78%, transparent);
}

.help-content h3 {
    margin-bottom: 0.45rem;
    color: var(--gold);
    font-size: 1rem;
}

.help-content ul {
    display: grid;
    gap: 0.45rem;
    padding-left: 1.1rem;
    color: var(--muted);
    line-height: 1.45;
}

.help-content li strong,
.help-content p strong {
    color: var(--text);
}

.piece-guide li {
    padding-bottom: 0.45rem;
    border-bottom: 1px solid var(--line);
}

.piece-guide li:last-child {
    padding-bottom: 0;
    border-bottom: 0;
}

@media (max-width: 760px) {
    #help-dialog {
        width: 94vw;
        max-height: 88vh;
        padding: 1rem;
    }

    #help-dialog form {
        max-height: calc(88vh - 2rem);
    }

    .help-content section {
        padding: 0.7rem;
    }

    .help-content h3 {
        font-size: 0.92rem;
    }

    .help-content ul,
    .help-content p {
        font-size: 0.86rem;
    }
}

/* ========================== GAME EXPERIENCE REDESIGN ========================== */
:root {
    --bg: #edf1f6;
    --panel: #ffffff;
    --surface: #f4f6fa;
    --text: #172033;
    --muted: #68748a;
    --line: #dce2eb;
    --gold: #b77a21;
    --gold-light: #e0ae54;
    --gold-dark: #855316;
    --gold-text: #ffffff;
    --green: #1e9a68;
    --danger: #d0504e;
    --white-square: #e9d6ab;
    --black-square: #a86f3e;
    --last: rgba(224, 174, 84, 0.35);
}

[data-theme="dark"] {
    --bg: #11151b;
    --panel: #1b222d;
    --surface: #252e3a;
    --text: #f3f6fb;
    --muted: #a4afc0;
    --line: #354152;
    --gold: #dfad4e;
    --gold-light: #f0c974;
    --gold-dark: #a97024;
    --gold-text: #1b1308;
    --green: #4cc58e;
    --danger: #e16f66;
    --white-square: #dfc493;
    --black-square: #885733;
    --last: rgba(240, 201, 116, 0.28);
}

#app {
    max-width: 1680px;
    background:
        radial-gradient(70rem 38rem at 50% -14rem, rgba(223, 173, 78, 0.13), transparent 70%),
        linear-gradient(180deg, color-mix(in srgb, var(--bg) 88%, #000 12%), var(--bg));
}

.app-header,
.bottom-bar {
    background: color-mix(in srgb, var(--panel) 94%, transparent);
    border-color: var(--line);
    box-shadow: none;
}

.app-header {
    min-height: 4.2rem;
    padding: 0.65rem clamp(0.8rem, 2vw, 1.6rem);
}

.brand {
    gap: 0.75rem;
    font-size: 1.1rem;
}

.brand__mark {
    border-radius: 0.75rem;
    box-shadow: 0 0.45rem 1.2rem rgba(0, 0, 0, 0.2);
}

.header-link,
.icon-btn,
.btn,
.mode-toggle {
    border-color: var(--line);
    background: var(--surface);
    box-shadow: none;
}

.header-link:hover,
.icon-btn:hover,
.btn:hover:not(:disabled),
.mode-toggle:hover {
    border-color: color-mix(in srgb, var(--gold) 50%, var(--line));
    background: color-mix(in srgb, var(--surface) 82%, var(--gold) 18%);
}

.game-layout {
    gap: clamp(0.65rem, 1.5vw, 1.25rem);
    padding: clamp(0.65rem, 1.5vw, 1.25rem);
}

.side-panel {
    justify-content: flex-start;
    gap: 0.8rem;
    padding: 1rem 0.65rem;
    border: 1px solid var(--line);
    border-radius: 1.1rem;
    background: color-mix(in srgb, var(--panel) 92%, var(--surface) 8%);
    box-shadow: 0 0.6rem 1.8rem rgba(0, 0, 0, 0.1);
}

.side-panel::before {
    content: '';
    width: 2.5rem;
    height: 0.22rem;
    border-radius: 999px;
    background: var(--line);
}

.side-panel strong {
    font-size: 1rem;
    letter-spacing: 0.02em;
}

.side-panel .player-identity {
    display: block;
    margin-top: 0.22rem;
    color: var(--muted);
    font-size: 0.77rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.side-panel span:not(.player-identity) {
    margin-top: 0.45rem;
    color: var(--muted);
}

.player-avatar {
    border-color: var(--line);
    background: var(--surface);
    box-shadow: none;
}

.side-panel.is-active {
    border-color: color-mix(in srgb, var(--gold) 72%, var(--line));
    background:
        linear-gradient(180deg, rgba(223, 173, 78, 0.17), transparent 58%),
        var(--panel);
    box-shadow: 0 0 0 1px rgba(223, 173, 78, 0.25), 0 0.8rem 2rem rgba(0, 0, 0, 0.14);
}

.side-panel.is-active::before {
    background: var(--gold);
    box-shadow: 0 0 0.7rem rgba(223, 173, 78, 0.7);
}

.side-panel.is-active .player-avatar {
    transform: none;
    background: linear-gradient(145deg, var(--gold-light), var(--gold), var(--gold-dark));
    box-shadow: 0 0.4rem 1rem rgba(0, 0, 0, 0.2);
}

.side-panel.is-active em {
    background: var(--gold);
    color: var(--gold-text);
    box-shadow: none;
}

.board-wrap {
    gap: 0.8rem;
    padding: clamp(0.55rem, 1.25vw, 1rem);
    border: 1px solid var(--line);
    border-radius: 1.25rem;
    background: color-mix(in srgb, var(--panel) 94%, var(--surface) 6%);
    box-shadow: 0 1rem 2.8rem rgba(0, 0, 0, 0.16);
}

.score-strip {
    width: min(100%, 56rem);
    grid-template-columns: auto auto minmax(13rem, 1fr);
    gap: 0.45rem;
    padding: 0.45rem;
    border-color: var(--line);
    border-radius: 0.85rem;
    background: var(--surface);
    box-shadow: none;
}

.hud-chip {
    min-height: 3.25rem;
    flex-direction: column;
    justify-content: center;
    gap: 0.15rem;
    padding: 0.3rem 0.75rem;
    background: color-mix(in srgb, var(--panel) 76%, transparent);
}

.hud-chip span {
    font-size: 0.6rem;
}

.hud-chip strong {
    font-size: 0.9rem;
}

.turn-banner {
    min-width: 0;
    min-height: 3.25rem;
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-rows: auto auto;
    align-items: center;
    column-gap: 0.65rem;
    padding: 0.4rem 0.8rem;
    border: 1px solid var(--line);
    border-left: 0.32rem solid var(--muted);
    border-radius: 0.7rem;
    background: color-mix(in srgb, var(--panel) 88%, transparent);
}

.turn-banner__eyebrow {
    grid-column: 1;
    grid-row: 1 / span 2;
    display: grid;
    place-items: center;
    width: 1.8rem;
    height: 1.8rem;
    border-radius: 999px;
    background: var(--surface);
    color: transparent;
    font-size: 0;
}

.turn-banner__eyebrow::after {
    content: '';
    width: 0.52rem;
    height: 0.52rem;
    border-radius: 50%;
    background: currentColor;
    color: var(--muted);
}

.turn-banner__title {
    grid-column: 2;
    color: var(--text);
    font-size: clamp(0.78rem, 1.4vw, 0.98rem);
    letter-spacing: 0.055em;
}

.turn-banner .hud-status {
    grid-column: 2;
    width: 100%;
    min-height: 1.05rem;
    justify-content: flex-start;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    color: var(--muted);
    font-size: 0.68rem;
    font-weight: 700;
    white-space: nowrap;
}

.turn-banner.is-your-turn {
    border-color: color-mix(in srgb, var(--green) 55%, var(--line));
    border-left-color: var(--green);
    background: color-mix(in srgb, var(--green) 10%, var(--panel));
}

.turn-banner.is-your-turn .turn-banner__eyebrow::after {
    color: var(--green);
    box-shadow: 0 0 0.7rem color-mix(in srgb, var(--green) 65%, transparent);
}

.turn-banner.is-opponent-turn,
.turn-banner.is-black-turn {
    border-left-color: #95a0b1;
}

.turn-banner.is-opponent-turn .turn-banner__eyebrow::after,
.turn-banner.is-black-turn .turn-banner__eyebrow::after {
    color: #95a0b1;
}

.turn-banner.is-white-turn {
    border-left-color: var(--gold);
    background: color-mix(in srgb, var(--gold) 10%, var(--panel));
}

.turn-banner.is-white-turn .turn-banner__eyebrow::after {
    color: var(--gold);
}

.turn-banner.is-waiting,
.turn-banner.is-finished {
    border-left-color: var(--gold);
}

.turn-banner.is-waiting .turn-banner__eyebrow::after,
.turn-banner.is-finished .turn-banner__eyebrow::after {
    color: var(--gold);
}

.board {
    border-color: #51351f;
    border-radius: 0.45rem;
    box-shadow: 0 1.15rem 2.6rem rgba(0, 0, 0, 0.35);
}

.bottom-bar {
    min-height: 4.5rem;
    gap: 0.6rem;
    padding: 0.65rem clamp(0.8rem, 2vw, 1.6rem) max(0.65rem, env(safe-area-inset-bottom));
}

.btn--gold {
    box-shadow: 0 0.35rem 0.9rem rgba(0, 0, 0, 0.2);
}

@media (max-width: 760px) {
    .app-header {
        min-height: 3.25rem;
    }

    .game-layout {
        gap: 0.45rem;
        padding: 0.45rem;
    }

    .side-panel {
        min-height: 3.25rem;
        gap: 0.55rem;
        padding: 0.35rem 0.65rem;
        border-inline: 1px solid var(--line);
        border-radius: 0.8rem;
    }

    .side-panel::before {
        width: 0.22rem;
        height: 1.65rem;
    }

    .side-panel .player-identity {
        display: inline;
        margin-left: 0.35rem;
        font-size: 0.64rem;
    }

    .side-panel span:not(.player-identity) {
        display: inline;
        margin-left: 0.35rem;
        font-size: 0.65rem;
    }

    .board-wrap {
        gap: 0.45rem;
        padding: 0.4rem;
        border-radius: 0.9rem;
    }

    .score-strip {
        grid-template-columns: auto auto minmax(0, 1fr);
        padding: 0.3rem;
    }

    .hud-chip {
        min-width: 2.5rem;
        min-height: 2.85rem;
        padding: 0.25rem 0.38rem;
    }

    .hud-chip span {
        display: block;
        font-size: 0.52rem;
    }

    .hud-chip strong {
        font-size: 0.68rem;
    }

    .turn-banner {
        min-height: 2.85rem;
        column-gap: 0.38rem;
        padding: 0.3rem 0.45rem;
        border-left-width: 0.22rem;
    }

    .turn-banner__eyebrow {
        width: 1.35rem;
        height: 1.35rem;
    }

    .turn-banner__eyebrow::after {
        width: 0.4rem;
        height: 0.4rem;
    }

    .turn-banner__title {
        font-size: 0.64rem;
    }

    .turn-banner .hud-status {
        font-size: 0.57rem;
    }

    .bottom-bar {
        min-height: 0;
    }
}
