/*!
 * EmoIntel Chess — board and game-screen styling.
 * Themes are plain variable swaps on .chess-board[data-theme].
 */

:root {
    --chess-primary: #F97316;
    --chess-ink: #0F172A;
    --chess-muted: #64748B;
    --chess-line: #E2E8F0;
    --chess-card: #FFFFFF;
    --chess-shell: #F8FAFC;
}

/* ------------------------------------------------------------ board themes */

.chess-board {
    --sq-light: #EFE0C4;
    --sq-dark: #B58863;
    --sq-label-on-light: #B58863;
    --sq-label-on-dark: #EFE0C4;
    --sq-select: rgba(249, 168, 37, 0.55);
    --sq-last: rgba(255, 213, 79, 0.48);
    --sq-hint: rgba(30, 41, 59, 0.28);
    --sq-check: rgba(239, 68, 68, 0.9);
    --sq-frame: #7A5236;
}

.chess-board[data-theme="ocean"] {
    --sq-light: #DEE9F2;
    --sq-dark: #6B93B8;
    --sq-label-on-light: #6B93B8;
    --sq-label-on-dark: #DEE9F2;
    --sq-select: rgba(14, 165, 233, 0.55);
    --sq-last: rgba(56, 189, 248, 0.45);
    --sq-frame: #3F6787;
}

.chess-board[data-theme="forest"] {
    --sq-light: #E8EDD3;
    --sq-dark: #7D946B;
    --sq-label-on-light: #7D946B;
    --sq-label-on-dark: #E8EDD3;
    --sq-select: rgba(34, 197, 94, 0.5);
    --sq-last: rgba(163, 230, 53, 0.5);
    --sq-frame: #556B45;
}

.chess-board[data-theme="midnight"] {
    --sq-light: #7E8AA2;
    --sq-dark: #46516B;
    --sq-label-on-light: #46516B;
    --sq-label-on-dark: #C3CBDC;
    --sq-select: rgba(139, 92, 246, 0.6);
    --sq-last: rgba(167, 139, 250, 0.45);
    --sq-hint: rgba(15, 23, 42, 0.4);
    --sq-frame: #2C3348;
}

/* -------------------------------------------------------------- the board */

.chess-board-wrap {
    width: 100%;
    max-width: 640px;
    margin-inline: auto;
    /* The board carries a 6px frame drawn as an outer shadow; without this the
       frame sits flush against the player cards and the viewport edge. */
    padding: 6px;
}

.chess-board {
    position: relative;
    aspect-ratio: 1 / 1;
    width: 100%;
    border-radius: 14px;
    overflow: hidden;
    box-shadow:
        0 0 0 6px var(--sq-frame),
        0 18px 40px -12px rgba(15, 23, 42, 0.45);
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;   /* the board handles its own dragging */
}

/*
 * Two layers. The squares are built once and never rebuilt — only their
 * classes change — and the pieces sit above in their own layer so a piece can
 * slide from one square to another instead of being destroyed and recreated.
 */
.chess-squares {
    position: absolute;
    inset: 0;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
}

.chess-pieces {
    position: absolute;
    inset: 0;
    pointer-events: none;   /* clicks fall through to the squares below */
}

.chess-sq {
    position: relative;
    background: var(--sq-light);
    cursor: default;
}

.chess-sq.dark {
    background: var(--sq-dark);
}

/*
 * Coordinate ticks, tucked into the board corners. Both are written into every
 * square once and shown only on the edges of the board as it is currently
 * facing, so flipping costs a class change rather than a rebuild.
 */
.chess-sq .coord {
    position: absolute;
    display: none;
    font-size: calc(var(--sq, 64px) * 0.19);
    font-weight: 800;
    line-height: 1;
    pointer-events: none;
    color: var(--sq-label-on-light);
}

.chess-sq.dark .coord {
    color: var(--sq-label-on-dark);
}

.chess-sq.show-rank .coord.rank {
    display: block;
    top: 4%;
    left: 5%;
}

.chess-sq.show-file .coord.file {
    display: block;
    bottom: 4%;
    right: 6%;
}

/* ------------------------------------------------------------- highlights */

.chess-sq.last-move::before,
.chess-sq.selected::before {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.chess-sq.last-move::before {
    background: var(--sq-last);
}

.chess-sq.selected::before {
    background: var(--sq-select);
}

.chess-sq.in-check::after {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
    background: radial-gradient(circle at center,
        var(--sq-check) 0%,
        rgba(239, 68, 68, 0.55) 42%,
        rgba(239, 68, 68, 0) 72%);
    animation: chess-check-pulse 1.1s ease-in-out infinite;
}

@keyframes chess-check-pulse {
    0%, 100% { opacity: 0.85; }
    50%      { opacity: 0.45; }
}

/* Quiet destination: a dot. Capture: a ring around the target piece. */
.chess-sq.hint-move::after,
.chess-sq.hint-capture::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    border-radius: 50%;
    pointer-events: none;
    transform: translate(-50%, -50%);
}

.chess-sq.hint-move::after {
    width: 30%;
    height: 30%;
    background: var(--sq-hint);
}

/* border-width takes no percentage, so this is sized from the square instead. */
.chess-sq.hint-capture::after {
    width: 86%;
    height: 86%;
    border: calc(var(--sq, 64px) * 0.075) solid var(--sq-hint);
}

.chess-sq.playable {
    cursor: pointer;
}

.chess-sq.drop-target {
    box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.85);
}

/* ----------------------------------------------------------------- pieces */

/*
 * Every piece is an eighth of the board, parked at the top-left corner and
 * moved by transform alone — which is what lets it animate smoothly and land
 * exactly on a square every time.
 */
.chess-piece {
    position: absolute;
    top: 0;
    left: 0;
    width: 12.5%;
    height: 12.5%;
    display: block;
    overflow: visible;
    /* Breathing room inside the square so pieces do not touch its edges or
       each other. Sized from --sq, not a percentage: percentage padding
       resolves against the containing block's width — the whole board — which
       would shrink the pieces to almost nothing. */
    box-sizing: border-box;
    padding: calc(var(--sq, 64px) * 0.055);
    transition: transform 190ms cubic-bezier(0.22, 0.72, 0.28, 1);
    will-change: transform;
    filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.28));
}

.chess-piece use {
    fill: var(--pc-fill);
    stroke: var(--pc-stroke);
}

.chess-piece.w { --pc-fill: #FFFFFF; --pc-stroke: #2B2926; }
.chess-piece.b { --pc-fill: #3A3734; --pc-stroke: #131211; }

/* A piece being lifted, and the whole board while it is flipping, must not
   animate — otherwise pieces glide across the board on a flip. */
.chess-piece.lifted { opacity: 0.3; }

.chess-pieces.no-anim .chess-piece { transition: none; }

.chess-piece.leaving {
    transition: opacity 150ms ease, transform 150ms ease;
    opacity: 0;
    pointer-events: none;
}

.chess-drag-ghost {
    position: fixed;
    z-index: 60;
    pointer-events: none;
    width: 64px;
    height: 64px;
    transform: translate(-50%, -50%) scale(1.1);
    transition: none;   /* must track the pointer exactly, not ease after it */
    filter: drop-shadow(0 8px 12px rgba(0, 0, 0, 0.45));
}

/* Static pieces used outside the board: avatars, the tray, promotion menu. */
.avatar-piece,
.cap-piece,
.promo-piece {
    display: block;
    overflow: visible;
}

.avatar-piece use,
.cap-piece use,
.promo-piece use {
    fill: var(--pc-fill);
    stroke: var(--pc-stroke);
}

.avatar-piece.w, .cap-piece.w, .promo-piece.w { --pc-fill: #FFFFFF; --pc-stroke: #2B2926; }
.avatar-piece.b, .cap-piece.b, .promo-piece.b { --pc-fill: #3A3734; --pc-stroke: #131211; }

.avatar-piece { width: 30px; height: 30px; }
.promo-piece  { width: 78%;  height: 78%; }

/* --------------------------------------------------------- promotion menu */

.promo-overlay {
    position: absolute;
    inset: 0;
    z-index: 30;
    background: rgba(15, 23, 42, 0.55);
    backdrop-filter: blur(2px);
    display: grid;
    place-items: center;
}

.promo-card {
    background: #fff;
    border-radius: 16px;
    padding: 14px 16px;
    box-shadow: 0 20px 40px -10px rgba(0, 0, 0, 0.5);
    text-align: center;
}

.promo-card p {
    font-weight: 800;
    font-size: 13px;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--chess-muted);
    margin-bottom: 8px;
}

.promo-options {
    display: flex;
    gap: 6px;
}

.promo-btn {
    width: calc(var(--sq, 64px) * 1.05);
    height: calc(var(--sq, 64px) * 1.05);
    border-radius: 12px;
    border: 2px solid var(--chess-line);
    background: #F8FAFC;
    display: grid;
    place-items: center;
    cursor: pointer;
    transition: transform 0.12s ease, border-color 0.12s ease, background 0.12s ease;
}

.promo-btn:hover {
    transform: translateY(-2px);
    border-color: var(--chess-primary);
    background: #FFF7ED;
}

/* --------------------------------------------------------- player panels */

.player-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
    border-radius: 14px;
    background: var(--chess-card);
    border: 2px solid var(--chess-line);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.player-card.active {
    border-color: var(--chess-primary);
    box-shadow: 0 0 0 4px rgba(249, 115, 22, 0.12);
}

.player-avatar {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    display: grid;
    place-items: center;
    font-size: 26px;
    line-height: 1;
    flex-shrink: 0;
}

.player-avatar.w { background: #F1F5F9; }
.player-avatar.b { background: #E2E8F0; }

.player-clock {
    font-variant-numeric: tabular-nums;
    font-weight: 800;
    font-size: 22px;
    padding: 4px 12px;
    border-radius: 10px;
    background: #F1F5F9;
    color: var(--chess-ink);
    letter-spacing: 0.02em;
}

.player-card.active .player-clock {
    background: var(--chess-ink);
    color: #fff;
}

.player-clock.low {
    background: #FEE2E2 !important;
    color: #B91C1C !important;
    animation: chess-check-pulse 1s ease-in-out infinite;
}

/* ------------------------------------------------------- captured tray */

/*
 * Taken pieces live off to the side in a tray of fixed height. The fixed
 * height is the point: if this grew as pieces came off, everything below it
 * would slide down the page in the middle of a game.
 */
.cap-tray {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;
    border-radius: 12px;
    background: #F8FAFC;
    border: 2px solid var(--chess-line);
    min-height: 56px;
}

.cap-tray + .cap-tray { margin-top: 8px; }

.cap-side {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--chess-muted);
    width: 58px;
    flex-shrink: 0;
}

.cap-list {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    flex: 1;
    min-width: 0;
    padding-right: 8px;
}

.cap-piece {
    width: 27px;
    height: 27px;
    margin-right: -8px;   /* overlap a little, like pieces set down in a row */
}

.cap-empty {
    font-size: 12px;
    font-weight: 700;
    color: #CBD5E1;
}

.cap-edge {
    font-size: 13px;
    font-weight: 900;
    color: #15803D;
    background: #DCFCE7;
    padding: 3px 9px;
    border-radius: 999px;
    flex-shrink: 0;
}

.cap-edge[hidden] { display: none; }

/* ------------------------------------------------------------- move list */

/*
 * Fixed height, never auto-sizing: the panel must not grow as moves are added,
 * or everything below it shifts down the page mid-game. Newest move sits at
 * the top, so there is nothing to chase as the list fills.
 */
.move-list {
    display: grid;
    grid-template-columns: 2.6rem 1fr 1fr;
    align-content: start;
    font-variant-numeric: tabular-nums;
    font-size: 14px;
    height: 320px;
    overflow-y: auto;
    overscroll-behavior: contain;   /* scrolling the list never scrolls the page */
}

.move-list .num {
    padding: 5px 6px;
    color: var(--chess-muted);
    font-weight: 700;
    background: #F8FAFC;
}

.move-list .ply {
    padding: 5px 8px;
    font-weight: 700;
    color: var(--chess-ink);
    cursor: pointer;
    border-radius: 6px;
    white-space: nowrap;
    transition: background 0.12s ease;
}

.move-list .ply:hover { background: #FFF7ED; }

.move-list .ply.current {
    background: var(--chess-primary);
    color: #fff;
}

.move-list .ply.empty {
    cursor: default;
    color: transparent;
}

.move-list .ply.empty:hover { background: transparent; }

/* ------------------------------------------------------------- controls */

.ctrl-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    padding: 10px 12px;
    border-radius: 11px;
    font-weight: 800;
    font-size: 13.5px;
    border: 2px solid var(--chess-line);
    background: #fff;
    color: #334155;
    cursor: pointer;
    transition: all 0.15s ease;
}

.ctrl-btn:hover:not(:disabled) {
    border-color: var(--chess-primary);
    color: var(--chess-primary);
    background: #FFF7ED;
    transform: translateY(-1px);
}

.ctrl-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

/* The display above outranks the browser's own [hidden] rule, so restate it. */
.ctrl-btn[hidden] {
    display: none;
}

.ctrl-btn.danger:hover:not(:disabled) {
    border-color: #EF4444;
    color: #EF4444;
    background: #FEF2F2;
}

.ctrl-btn.solid {
    background: var(--chess-primary);
    border-color: var(--chess-primary);
    color: #fff;
}

.ctrl-btn.solid:hover:not(:disabled) {
    background: #EA580C;
    border-color: #EA580C;
    color: #fff;
}

/* ---------------------------------------------------------------- modal */

.chess-modal {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
}

.chess-modal[hidden] { display: none; }

.chess-modal-card {
    width: 100%;
    max-width: 420px;
    background: #fff;
    border-radius: 20px;
    padding: 28px 26px;
    text-align: center;
    box-shadow: 0 30px 60px -15px rgba(0, 0, 0, 0.5);
    animation: chess-pop 0.28s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes chess-pop {
    from { opacity: 0; transform: translateY(14px) scale(0.94); }
    to   { opacity: 1; transform: none; }
}

/* -------------------------------------------------------- review banner */

.review-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 8px 12px;
    border-radius: 11px;
    background: #EFF6FF;
    border: 2px solid #BFDBFE;
    color: #1D4ED8;
    font-weight: 800;
    font-size: 13px;
}

.review-banner[hidden] { display: none; }

/* ------------------------------------------------------ online chrome */

.conn-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 13px;
    border-radius: 999px;
    font-weight: 800;
    font-size: 12.5px;
    border: 2px solid transparent;
}

.conn-pill::before {
    content: '';
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: currentColor;
}

.conn-pill.ok {
    background: #ECFDF5;
    border-color: #A7F3D0;
    color: #047857;
}

.conn-pill.bad {
    background: #FEF2F2;
    border-color: #FECACA;
    color: #B91C1C;
}

.conn-pill.bad::before {
    animation: chess-check-pulse 0.9s ease-in-out infinite;
}

.thinking-badge {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    border-radius: 999px;
    font-weight: 800;
    font-size: 12.5px;
    background: #EEF2FF;
    border: 2px solid #C7D2FE;
    color: #4338CA;
}

.thinking-badge[hidden] { display: none; }

.think-dots {
    display: inline-flex;
    gap: 3px;
}

.think-dots i {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: currentColor;
    animation: think-bounce 1s ease-in-out infinite;
}

.think-dots i:nth-child(2) { animation-delay: 0.15s; }
.think-dots i:nth-child(3) { animation-delay: 0.3s; }

@keyframes think-bounce {
    0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
    30%           { opacity: 1;   transform: translateY(-3px); }
}

.room-code {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-weight: 800;
    font-size: 21px;
    letter-spacing: 0.22em;
    padding: 8px 14px;
    border-radius: 11px;
    background: #fff;
    border: 2px dashed #FCD34D;
    color: #92400E;
    white-space: nowrap;
}

/* -------------------------------------------------------------- toast */

.chess-toast {
    position: fixed;
    left: 50%;
    bottom: 26px;
    transform: translateX(-50%) translateY(120%);
    z-index: 90;
    padding: 11px 20px;
    border-radius: 999px;
    background: var(--chess-ink);
    color: #fff;
    font-weight: 800;
    font-size: 14px;
    box-shadow: 0 12px 28px -8px rgba(0, 0, 0, 0.5);
    /* Hidden outright rather than only shifted: an empty toast is short enough
       that a percentage translate leaves its top edge on screen. */
    opacity: 0;
    visibility: hidden;
    transition: transform 0.3s cubic-bezier(0.34, 1.4, 0.64, 1),
                opacity 0.2s ease,
                visibility 0s linear 0.3s;
    pointer-events: none;
}

.chess-toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
    visibility: visible;
    transition-delay: 0s;
}

@media (max-width: 1024px) {
    .chess-board-wrap { max-width: 560px; }
}
