/* Square size: as large as the window allows, leaving about 170px of height for the
   player rows above and below the board, and room beside the board for the side bar. */
:root {
    --tile: clamp(40px, min(calc((100vh - 170px) / 8), calc((100vw - 200px) / 8)), 80px);
    --board-width: calc(var(--tile) * 8 + 10px);   /* including the 5px border */

    /* Side bar sizes, all derived from the squircle size (at most 54px). The bar holds
       4 buttons, the turn light and 4 item slots, which take about 11 squircles of
       height, so it scales with the board to fit beside it. A squircle is as wide as
       the turn light's housing (socket + padding + 2 × 2px border). */
    --slot: min(54px, calc(var(--board-width) / 11.5));
    --light-pad: calc(var(--slot) * 0.15);
    --light-gap: calc(var(--slot) * 0.185);
    --socket: calc(var(--slot) - 2 * var(--light-pad) - 4px);
    --bar-gap: 16px;

    /* Player rows hold up to 8 squircles (icon, material, ability, 5 conversions)
       and stay within the board's width. */
    --row-gap: 8px;
    --row-item: min(54px, calc((var(--board-width) - 7 * var(--row-gap)) / 8));
}

/* Narrow screens (phones): the board and side bar are centered together rather than
   centering the board alone, and the side bar shrinks, so the board can be wider. */
@media (max-width: 700px) {
    :root {
        --bar-gap: 8px;
        --row-gap: 5px;
        /* The board, the gap and the side bar (1/11.5 of the board) share the width
           inside 12px margins: board = (width - 24px - 8px) × 11.5 / 12.5. */
        --tile: clamp(28px, min(calc((100vh - 170px) / 8),
            calc(((100vw - 32px) * 0.92 - 10px) / 8)), 80px);
    }
    body {
        box-sizing: border-box;
        padding: 0 12px;
    }
    /* The extra right padding shifts the game left by half the side bar's width. */
    #game { padding-right: calc(var(--bar-gap) + var(--slot)); }
}

/* Phones wait ~300ms after a tap to see if it's a double-tap zoom before sending the
   click. The page doesn't use double-tap zoom, so let taps through immediately, and
   skip the gray flash phones draw over tapped elements. */
html {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

body {
    background-color: #222;
    color: white;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    margin: 0;
    font-family: sans-serif;
}

#game { display: flex; flex-direction: column; align-items: center; }
#game[hidden], #menu[hidden] { display: none; }

#board {
    display: grid;
    grid-template-columns: repeat(8, var(--tile));
    grid-template-rows: repeat(8, var(--tile));
    border: 5px solid #444;
    user-select: none;
}

.tile {
    width: var(--tile);
    height: var(--tile);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: calc(var(--tile) * 0.67);
    cursor: pointer;
}
.light-tile { background-color: #EEEED2; }
.dark-tile { background-color: #769656; }

.white-piece { color: #fff; text-shadow: 0 0 2px #000; }
.black-piece { color: #000; }

/* Tints the square's own color, so light and dark squares stay distinguishable */
.last-move { background-image: linear-gradient(rgba(246, 246, 105, 0.45), rgba(246, 246, 105, 0.45)); }
.tile.selected { background-color: rgba(255, 255, 0, 0.5) !important; }
.in-check { background-color: #e05a4f !important; }

.highlight { position: relative; }
.highlight::after {
    content: '';
    position: absolute;
    width: calc(var(--tile) * 0.33);
    height: calc(var(--tile) * 0.33);
    background-color: rgba(0, 0, 0, 0.2);
    border-radius: 50%;
}

/* Player rows above and below the board */
.player-row {
    display: flex;
    align-items: center;
    gap: var(--row-gap);
    width: var(--board-width);
    height: var(--row-item);
    margin: 8px 0;
}
.player-row .slot { --size: var(--row-item); }
.choices { display: flex; gap: var(--row-gap); }
.choices[hidden] { display: none; }

/* A ruler's placeholder art in its color */
.ruler-icon {
    background: #2d2d2d;
    border: 2px solid var(--accent);
    color: var(--accent);
    font-size: calc(var(--size) * 0.6);
}

/* Conversion and promotion choices: a piece on a light squircle, with its price
   in points once a piece is selected */
.choice {
    position: relative;
    width: var(--row-item);
    height: var(--row-item);
    padding: 0;
    font-size: calc(var(--row-item) * 0.68);
    line-height: 1;
    background: #EEEED2;
    border: 2px solid transparent;
    border-radius: 32%;
    cursor: pointer;
}
.choice:hover:not(:disabled) { border-color: #3f7bd1; }
.choice:disabled { cursor: default; opacity: 0.4; }
.choice .price {
    position: absolute;
    right: 8%;
    bottom: 2%;
    font-size: calc(var(--row-item) * 0.24);
    font-weight: bold;
    color: #2a5ca6;
    text-shadow: none;
}

/* Game buttons: squircles the size of the side bar, with a drawn icon */
.icon-button {
    display: flex;
    justify-content: center;
    align-items: center;
    width: var(--slot);
    height: var(--slot);
    padding: 0;
    background: #444;
    color: white;
    border: none;
    border-radius: 32%;
    cursor: pointer;
}
.icon-button:hover:not(:disabled) { background: #555; }
.icon-button:disabled { opacity: 0.45; cursor: default; }
.icon-button svg {
    width: 48%;
    height: 48%;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}
#copy-game .copied-icon { display: none; }
#copy-game.copied .copied-icon { display: inline; }
#copy-game.copied .copy-icon { display: none; }
#sound .sound-off { display: none; }
#sound.muted .sound-off { display: inline; }
#sound.muted .sound-on { display: none; }

/* Side bar beside the board, positioned so the board itself stays centered (on wide screens).
   It spans the board's height: 4 game buttons at the top, the turn light in the middle,
   and 4 slots for items at the bottom. */
#board-area { position: relative; }
#side-bar {
    position: absolute;
    left: calc(100% + var(--bar-gap));
    top: 0;
    bottom: 0;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
}
#game-buttons, #item-slots, #turn-light {
    display: flex;
    flex-direction: column;
    gap: var(--light-gap);
}
/* The turn light has a housing; the buttons and slots match its width. */
#turn-light {
    padding: var(--light-pad);
    background: #333;
    border: 2px solid #444;
    border-radius: 14px;
}
/* One marker slides between two sockets, fading from white through gray to black.
   The bottom socket is one socket plus one gap further down. */
#turn-light { position: relative; }
.socket, #turn-marker {
    width: var(--socket);
    height: var(--socket);
    box-sizing: border-box;
    border-radius: 32%;   /* squircle-ish */
}
/* The sockets only reserve the marker's two positions; they're not drawn. */
.socket { visibility: hidden; }
#turn-marker {
    position: absolute;
    left: var(--light-pad);
    top: var(--light-pad);
    border: 2px solid transparent;
    transition: transform 0.35s ease, background-color 0.35s ease, border-color 0.35s ease;
}
#turn-marker.top { transform: translateY(0); }
#turn-marker.middle { transform: translateY(calc((var(--socket) + var(--light-gap)) / 2)); }
#turn-marker.bottom { transform: translateY(calc(var(--socket) + var(--light-gap))); }
#turn-marker.white { background-color: #f4f4f4; border-color: #bbb; }
#turn-marker.black { background-color: #111; border-color: #777; }
#turn-marker.draw { background-color: #888; border-color: #999; }

/* A Necromancer's king glows faintly purple */
.ruler-necromancer { text-shadow: 0 0 6px #a35cff, 0 0 2px #000; }

/* The grave: a purple border on its square, shown even when a piece stands on it.
   While it's open, a faded ghost of the dead piece shows what would rise. */
.grave { box-shadow: inset 0 0 0 calc(var(--tile) * 0.05) rgba(150, 90, 230, 0.9); }
.ghost { opacity: 0.4; }
.can-raise .ghost { opacity: 0.7; }
.can-raise { animation: raise-pulse 1.4s ease-in-out infinite; }
@keyframes raise-pulse {
    0%, 100% { box-shadow: inset 0 0 0 calc(var(--tile) * 0.05) rgba(150, 90, 230, 0.9); }
    50% { box-shadow: inset 0 0 0 calc(var(--tile) * 0.09) rgba(170, 110, 255, 1); }
}

.slot {
    --size: var(--slot);
    width: var(--size);
    height: var(--size);
    box-sizing: border-box;
    border-radius: 32%;
    display: flex;
    justify-content: center;
    align-items: center;
    user-select: none;
}
.material {
    background: #555;
    color: #eee;
    font-size: calc(var(--size) * 0.37);
    font-weight: bold;
}
.ability.none { display: none; }
.ability.necromancer {
    background: #9677cf;
    border: 2px solid #6b3fa0;
    font-size: calc(var(--size) * 0.7);
}
.ability.locked { color: rgba(0, 0, 0, 0); }
.ability.locked.white-piece { text-shadow: 0 0 0 rgba(255, 255, 255, 0.55), 0 0 2px rgba(0, 0, 0, 0.4); }
.ability.locked.black-piece { text-shadow: 0 0 0 rgba(0, 0, 0, 0.4); }
.ability.open { box-shadow: 0 0 10px 2px rgba(163, 92, 255, 0.7); }
.ability.clickable { cursor: pointer; }
.item-slot { border: 2px dashed #444; }

/* The Alchemist's budget: a blue squircle */
.ability.alchemist {
    background: #3f7bd1;
    border: 2px solid #2a5ca6;
    color: #fff;
    font-size: calc(var(--size) * 0.37);
    font-weight: bold;
}
.ruler-alchemist { text-shadow: 0 0 6px #4a9bff, 0 0 2px #000; }

/* Main menu: a row of ruler cards (playing-card proportions) and the start text */
#menu {
    --card-width: clamp(120px, 17vw, 200px);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 48px;
}
#ruler-cards {
    display: grid;
    grid-template-columns: repeat(4, var(--card-width));
    gap: 24px;
}
@media (max-width: 700px) {
    #menu { --card-width: min(40vw, 170px); gap: 32px; }
    #ruler-cards { grid-template-columns: repeat(2, var(--card-width)); gap: 16px; }
}
.ruler-card {
    width: var(--card-width);
    aspect-ratio: 5 / 7;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    padding: 18% 8% 12%;
    background: #2d2d2d;
    border: 2px solid #444;
    border-radius: 12px;
    color: #eee;
    font-family: inherit;
    cursor: pointer;
    transition: transform 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}
.card-art { font-size: calc(var(--card-width) * 0.42); color: var(--accent); line-height: 1; }
.card-name { font-size: calc(var(--card-width) * 0.1); }
/* Previewing a card: it grows and glows in its ruler's color. Only devices with a real
   pointer get the hover version; on phones it would stick after a tap. */
.ruler-card:not(.locked):focus-visible {
    outline: none;
    transform: translateY(-8px) scale(1.06);
    border-color: var(--accent);
    box-shadow: 0 14px 28px rgba(0, 0, 0, 0.5), 0 0 20px var(--accent);
}
@media (hover: hover) {
    .ruler-card:not(.locked):hover {
        transform: translateY(-8px) scale(1.06);
        border-color: var(--accent);
        box-shadow: 0 14px 28px rgba(0, 0, 0, 0.5), 0 0 20px var(--accent);
    }
}
.ruler-card.locked { opacity: 0.4; cursor: default; }

#start-text {
    margin: 0;
    color: #ddd;
    font-size: clamp(22px, 3vw, 32px);
}
#start-name {
    color: var(--accent, inherit);
    transition: opacity 0.15s ease;
}
#start-name.fading { opacity: 0; }
