:root {
    --gold: #d4af37;
    --dark-bg: #0f172a;
    --light-sq: #f0d9b5;
    --dark-sq: #b58863;
    --danger: #ff0000;
}

body {
    background-color: var(--dark-bg);
    color: var(--gold);
    font-family: 'Segoe UI', sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    margin: 0;
}

.main-layout {
    display: flex;
    align-items: flex-start;
    gap: 30px;
    margin-top: 10px;
}

.side-panel { display: flex; flex-direction: column; align-items: center; }
.side-panel h3 { font-size: 12px; margin-bottom: 10px; text-transform: uppercase; }

.graveyard {
    width: auto;             /* Grows wider when a 2nd column starts */
    min-width: 50px;
    max-width: 120px;        /* Limits to about 2-3 columns max */
    height: 440px;           /* Matches the height of the board */
    border: 2px solid var(--gold);
    border-radius: 8px;
    display: flex;
    flex-direction: column;  /* Stack pieces vertically */
    flex-wrap: wrap;         /* This starts the 2nd line when height is reached */
    align-content: flex-start; 
    padding: 10px;
    gap: 2px;
    background: rgba(0, 0, 0, 0.2);
    overflow: hidden;        /* Keeps it clean */
}

.graveyard span {
    font-size: 32px;
    width: 40px;             /* Fixed width for each piece column */
    text-align: center;
    cursor: default;
}

.white-kill { border-color: #fff; }
.black-kill { border-color: #000; }

#board {
    display: grid;
    grid-template-columns: repeat(8, 60px);
    grid-template-rows: repeat(8, 60px);
    border: 8px solid #2d2d2d;
    position: relative;
}

.square {
    width: 60px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 44px;
    cursor: pointer;
}

.light { background-color: var(--light-sq); }
.dark { background-color: var(--dark-sq); }

/* TURN INDICATORS */
#status {
    font-size: 20px;
    text-align: center;
    padding: 10px 25px;
    border-radius: 30px;
    border: 2px solid var(--gold);
    transition: 0.4s;
    font-weight: bold;
}

.white-turn-active { background: #fff !important; color: #000 !important; box-shadow: 0 0 15px #fff; }
.black-turn-active { background: #000 !important; color: var(--gold) !important; box-shadow: 0 0 15px var(--gold); }

/* KING IN CHECK RED ALERT */
.check {
    background-color: var(--danger) !important;
    animation: urgentPulse 0.8s infinite !important;
    border: 2px solid white !important;
}

@keyframes urgentPulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); background-color: #b30000; }
    100% { transform: scale(1); }
}

.piece-white { color: #fff; text-shadow: 2px 2px 4px #000, -1px -1px 0 #000; }
.piece-black { color: #000; }
.selected { background-color: var(--gold) !important; }

.controls { margin-top: 25px; display: flex; gap: 15px; }
button { padding: 10px 20px; background: transparent; color: var(--gold); border: 2px solid var(--gold); cursor: pointer; border-radius: 5px; font-weight: bold; }
button:hover { background: var(--gold); color: #000; }

.overlay { display: none; position: absolute; width: 100%; height: 100%; background: rgba(0,0,0,0.9); z-index: 100; flex-direction: column; align-items: center; justify-content: center; }
.promo-choice { font-size: 45px; cursor: pointer; transition: 0.2s; }
.promo-choice:hover { transform: scale(1.2); }

/* --- MOBILE RESPONSIVENESS --- */
@media (max-width: 768px) {
    :root {
        --board-size: 90vw;
        --sq-size: calc(90vw / 8);
    }

    .main-layout {
        flex-direction: column;
        align-items: center;
        gap: 15px;
    }

    .side-panel {
        width: 90vw;
    }

    .graveyard {
        width: 100%;
        max-width: none;
        height: auto;
        min-height: 50px;
        flex-direction: row; /* Stack horizontally on mobile */
        justify-content: center;
        padding: 5px;
    }

    .graveyard span {
        font-size: 28px;
        width: 35px;
    }

    .square {
        font-size: 32px; /* Smaller pieces for small screens */
    }

    #status {
        font-size: 16px;
    }
}

/* --- UNIVERSAL MOBILE & TABLET RESPONSIVENESS --- */
@media (max-width: 550px) {
    :root {
        /* Board takes 92% of the screen width for breathing room */
        --board-size: 92vw;
        --sq-size: calc(92vw / 8);
    }

    body {
        padding: 10px 0;
        justify-content: flex-start; /* Prevents center-clipping on short screens */
    }

    .main-layout {
        flex-direction: column; /* FORCES VERTICAL STACK */
        align-items: center;
        gap: 10px;
        width: 100%;
    }

    /* REORDERING: Captured Black -> Board -> Captured White */
    .side-panel:nth-child(1) { order: 1; } 
    #board { order: 2; margin: 10px 0; }
    .side-panel:nth-child(3) { order: 3; }

    .side-panel {
        width: 92vw;
    }

    .graveyard {
        width: 100%;
        max-width: none;
        height: auto; /* Remove fixed height for mobile */
        min-height: 50px;
        flex-direction: row; /* Pieces stack left-to-right */
        justify-content: center;
        flex-wrap: wrap;
        padding: 5px;
    }

    .graveyard span {
        font-size: 26px; /* Optimized for touch */
        width: 32px;
    }

    #board {
        width: var(--board-size);
        height: var(--board-size);
        grid-template-columns: repeat(8, var(--sq-size));
        grid-template-rows: repeat(8, var(--sq-size));
        border-width: 4px; /* Scaled down border */
    }

    .square {
        width: var(--sq-size);
        height: var(--sq-size);
        font-size: calc(var(--sq-size) * 0.7); /* Piece size relative to square */
    }

    #status {
        font-size: 16px;
        margin-bottom: 10px;
    }

    .controls {
        width: 92vw;
        justify-content: center;
        gap: 10px;
    }

    button {
        flex: 1; /* Buttons share width equally on mobile */
        padding: 12px 5px;
        font-size: 14px;
    }
}

/* Extra tweak for very thin phones */
@media (max-width: 360px) {
    .graveyard span {
        font-size: 20px;
        width: 25px;
    }
}
