body {
    margin: 0;
    padding: 60px 12px 12px;
    box-sizing: border-box;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: linear-gradient(to bottom, #1a2980, #26d0ce);
    font-family: 'Arial', sans-serif;
    color: #fff;
}

#menuButton,
#fullscreenButton {
    position: fixed;
    top: 12px;
    right: 12px;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    width: 40px;
    height: 40px;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.45);
    color: #fff;
    text-decoration: none;
    cursor: pointer;
}

#menuButton {
    flex-direction: column;
    gap: 4px;
}

#fullscreenButton {
    top: 60px;
}

#menuButton span {
    display: block;
    width: 20px;
    height: 2px;
    border-radius: 1px;
    background: #fff;
}

#menuButton:hover,
#fullscreenButton:hover {
    background: rgba(0, 0, 0, 0.7);
}

#title {
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 10;
    padding: 6px 10px;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.45);
    font-size: 20px;
    font-weight: bold;
}

main {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    min-height: 0;
}

.captured {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 2px;
    min-height: 24px;
    width: min(92vw, 640px);
}

.captured .piece {
    font-size: clamp(18px, 3vw, 28px);
}

#board {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    /* Size from the space left after the fixed chrome so the page never scrolls. */
    width: min(92vw, calc(100vh - 330px), calc(100dvh - 330px), 640px);
    aspect-ratio: 1;
    border: 4px solid rgba(0, 0, 0, 0.6);
    border-radius: 4px;
    overflow: hidden;
    box-shadow: 0 0 24px rgba(0, 0, 0, 0.5);
}

.square {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    aspect-ratio: 1;
    cursor: pointer;
    user-select: none;
}

.square.light { background: #eeeed2; }
.square.dark  { background: #769656; }

.square[data-c="0"]::before {
    content: attr(data-rank);
    position: absolute;
    top: 2px;
    left: 3px;
    font-size: clamp(8px, 1.6vw, 12px);
    color: rgba(0, 0, 0, 0.45);
}

.square[data-r="7"]::after {
    content: attr(data-file);
    position: absolute;
    right: 3px;
    bottom: 1px;
    font-size: clamp(8px, 1.6vw, 12px);
    color: rgba(0, 0, 0, 0.45);
}

.square.selected {
    box-shadow: inset 0 0 0 5px rgba(255, 215, 0, 0.85);
}

.square.target {
    background-image: radial-gradient(circle, rgba(0, 0, 0, 0.28) 22%, transparent 24%);
}

.square.capture-target {
    box-shadow: inset 0 0 0 5px rgba(220, 40, 40, 0.75);
}

.piece {
    font-family: 'Segoe UI Symbol', 'DejaVu Sans', 'Arial Unicode MS', sans-serif;
    font-size: clamp(24px, 5.2vw, 52px);
    line-height: 1;
    pointer-events: none;
}

.piece.white {
    color: #fff;
    text-shadow: 0 0 2px #000, 0 1px 2px rgba(0, 0, 0, 0.8);
}

.piece.black {
    color: #1a1a1a;
    text-shadow: 0 0 1px rgba(255, 255, 255, 0.5);
}

#meta {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    width: 100%;
}

#gameTitle {
    box-sizing: border-box;
    width: min(92vw, 640px);
    padding: 6px 12px;
    border: 1px solid rgba(255, 255, 255, 0.4);
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.35);
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    text-align: center;
}

#gameTitle::placeholder { color: rgba(255, 255, 255, 0.65); }

#clock {
    font-size: 13px;
    text-align: center;
    opacity: 0.9;
}

#status {
    box-sizing: border-box;
    min-height: 30px;
    padding: 3px 12px;
    border: 2px solid transparent;
    border-radius: 6px;
    font-size: 18px;
    font-weight: bold;
    text-align: center;
}

#status.white-turn {
    background: rgba(0, 0, 0, 0.45);
    color: #fff;
}

#status.black-turn {
    background: transparent;
    color: #000;
    border-color: #000;
}

#moves {
    min-height: 16px;
    max-width: 640px;
    font-size: 13px;
    text-align: center;
    opacity: 0.9;
    word-break: break-word;
}

#actionRow {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    flex-wrap: nowrap;
}

#actionRow button {
    padding: 8px 12px;
    border: none;
    border-radius: 6px;
    background: #4ECDC4;
    color: #123;
    font-size: 14px;
    font-weight: bold;
    cursor: pointer;
    white-space: nowrap;
}

#actionRow button:hover { background: #1a936f; color: #fff; }

#hint {
    max-width: 640px;
    margin: 0;
    font-size: 13px;
    text-align: center;
    opacity: 0.85;
}

#promotion {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 20;
    display: flex;
    gap: 6px;
    padding: 12px;
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.85);
    box-shadow: 0 0 20px rgba(255, 255, 255, 0.3);
}

#promotion button {
    width: 48px;
    height: 48px;
    border: none;
    border-radius: 6px;
    background: #eeeed2;
    font-family: 'Segoe UI Symbol', 'DejaVu Sans', sans-serif;
    font-size: 34px;
    line-height: 1;
    cursor: pointer;
}

#promotion button:hover { background: #baca6b; }

.hidden { display: none !important; }

@media (max-width: 600px) {
    body { padding: 88px 6px 12px; }

    #title { top: 8px; left: 8px; font-size: 16px; }

    #menuButton,
    #fullscreenButton {
        top: 8px;
        right: 8px;
        width: 36px;
        height: 36px;
    }

    #fullscreenButton { top: 52px; }

    #board {
        width: min(96vw, calc(100vh - 350px), calc(100dvh - 350px), 640px);
    }

    #status { font-size: 17px; }
}

/* Short screens (e.g. landscape phones): drop optional text and keep the board usable. */
@media (max-height: 500px) {
    body { padding: 48px 12px 8px; }

    main { gap: 4px; }

    #gameTitle { padding: 4px 10px; font-size: 16px; }
    #clock { font-size: 12px; }
    .captured { min-height: 20px; }
    #moves, #hint { display: none; }
    #status { min-height: 26px; font-size: 15px; }
    #actionRow button { padding: 6px 10px; font-size: 13px; }

    #board {
        width: min(92vw, calc(100vh - 190px), calc(100dvh - 190px), 640px);
    }
}
