:root {
    --bg-color: #0a0f1e;
    --glass-bg: rgba(15, 23, 42, 0.85);
    --glass-border: rgba(255, 255, 255, 0.08);
    --panel-bg: rgba(255, 255, 255, 0.03);
    --primary: #38bdf8;
    --primary-dark: #0284c7;
    --danger: #ef4444;
    --success: #22c55e;
    --warning: #f59e0b;
    --text: #f1f5f9;
    --text-muted: #64748b;
    --board-light: #e8edf9;
    --board-dark: #5a7fa0;
    --board-light-hover: #d4dff5;
    --board-dark-hover: #4a6f90;
    --selected-color: rgba(56, 189, 248, 0.5);
    --premove-color: rgba(249, 115, 22, 0.55);
    --last-move-color: rgba(56, 189, 248, 0.25);
    --check-color: rgba(239, 68, 68, 0.6);
    --legal-move-color: rgba(0, 0, 0, 0.18);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Inter', sans-serif;
    -webkit-font-smoothing: antialiased;
}

body {
    background: var(--bg-color);
    background-image:
        radial-gradient(at 0% 0%, hsla(210, 100%, 8%, 0.9) 0, transparent 55%),
        radial-gradient(at 100% 0%, hsla(220, 90%, 12%, 0.9) 0, transparent 55%),
        radial-gradient(at 50% 100%, hsla(200, 100%, 4%, 1) 0, transparent 50%);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.glass-container {
    background: var(--glass-bg);
    backdrop-filter: blur(20px);
    border: 1px solid var(--glass-border);
    border-radius: 28px;
    width: 98vw;
    max-width: 1200px;
    min-height: 90vh;
    display: flex;
    flex-direction: column;
    padding: 1.5rem 2rem;
    box-shadow:
        0 0 0 1px rgba(255, 255, 255, 0.05) inset,
        0 30px 60px -20px rgba(0, 0, 0, 0.7);
    gap: 1.2rem;
}

/* ─── HEADER ─────────────────────────────── */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

h1 {
    font-weight: 800;
    letter-spacing: -1.5px;
    font-size: 1.4rem;
    text-transform: uppercase;
}

h1 span {
    color: var(--primary);
    font-weight: 300;
    letter-spacing: 2px;
}

.status-pill {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.78rem;
    padding: 5px 14px;
    background: var(--panel-bg);
    border: 1px solid var(--glass-border);
    border-radius: 20px;
    color: var(--text-muted);
    transition: color 0.3s;
}

.status-pill.connected { color: var(--success); }
.status-pill.disconnected { color: var(--danger); }

.status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: currentColor;
    animation: pulse-dot 2s infinite;
}

@keyframes pulse-dot {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

/* ─── LAYOUT ─────────────────────────────── */
main {
    flex: 1;
    overflow: hidden;
}

.game-layout {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 2rem;
    height: 100%;
    align-items: start;
}

/* ─── BOARD AREA ─────────────────────────── */
.board-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
}

.player-card {
    width: var(--board-size, 520px);
    display: flex;
    align-items: center;
    gap: 0.9rem;
    padding: 0.6rem 1rem;
    background: var(--panel-bg);
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    transition: border-color 0.3s;
}

.player-card.active-turn {
    border-color: var(--primary);
    box-shadow: 0 0 14px rgba(56, 189, 248, 0.15);
}

.avatar {
    width: 36px;
    height: 36px;
    background: linear-gradient(135deg, var(--primary-dark), var(--primary));
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.75rem;
    color: #0a0f1e;
    flex-shrink: 0;
}

.avatar.black-player {
    background: linear-gradient(135deg, #1e293b, #334155);
    color: var(--text);
}

.player-name {
    flex: 1;
    font-weight: 600;
    font-size: 0.9rem;
}

.captured-pieces {
    display: flex;
    flex-wrap: wrap;
    gap: 1px;
    font-size: 0.7rem;
    flex: 2;
    opacity: 0.7;
}

.clock {
    font-family: 'Inter', monospace;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text);
    min-width: 70px;
    text-align: right;
    letter-spacing: -1px;
    transition: color 0.3s;
}

.clock.ticking { color: var(--primary); }
.clock.critical { color: var(--danger); animation: blink-clock 1s infinite; }

@keyframes blink-clock {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ─── BOARD ──────────────────────────────── */
.board-wrapper {
    position: relative;
    display: flex;
}

.rank-labels {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    padding: 0 6px 0 0;
    font-size: 0.65rem;
    color: var(--text-muted);
    font-weight: 600;
    height: var(--board-size, 520px);
}

.file-labels {
    display: flex;
    justify-content: space-around;
    padding: 4px 0 0 0;
    font-size: 0.65rem;
    color: var(--text-muted);
    font-weight: 600;
    width: var(--board-size, 520px);
}

#board-container {
    position: relative;
    width: var(--board-size, 520px);
    height: var(--board-size, 520px);
    border-radius: 6px;
    overflow: hidden;
    box-shadow:
        0 0 0 3px rgba(255, 255, 255, 0.06),
        0 20px 60px rgba(0, 0, 0, 0.5);
    flex-shrink: 0;
}

.chess-board {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    width: 100%;
    height: 100%;
}

#arrow-canvas {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    pointer-events: none;
    z-index: 10;
}

/* ─── SQUARES ────────────────────────────── */
.square {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    user-select: none;
    transition: background-color 0.1s;
}

.square.light { background-color: var(--board-light); }
.square.dark  { background-color: var(--board-dark); }
.square.light:hover { background-color: var(--board-light-hover); }
.square.dark:hover  { background-color: var(--board-dark-hover); }

.square.selected {
    background-color: var(--selected-color) !important;
}

.square.last-move {
    background-color: var(--last-move-color) !important;
}

.square.premove {
    background-color: var(--premove-color) !important;
}

.square.in-check {
    background-color: var(--check-color) !important;
}

/* Legal move dot */
.legal-dot {
    position: absolute;
    width: 30%;
    height: 30%;
    background: var(--legal-move-color);
    border-radius: 50%;
    z-index: 4;
    pointer-events: none;
}

/* Legal capture ring */
.legal-capture {
    position: absolute;
    inset: 0;
    border-radius: 0;
    box-shadow: inset 0 0 0 4px var(--legal-move-color);
    z-index: 4;
    pointer-events: none;
}

/* ─── PIECES ─────────────────────────────── */
.piece {
    width: 85%;
    height: 85%;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    z-index: 5;
    transition: transform 0.1s ease;
    pointer-events: none;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.4));
}

.square:hover .piece { transform: scale(1.05); }
.square.selected .piece { transform: scale(1.08); }

/* ─── RIGHT SIDEBAR ──────────────────────── */
.sidebar-right {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    height: 100%;
    min-width: 0;
}

.panel {
    background: var(--panel-bg);
    border: 1px solid var(--glass-border);
    border-radius: 16px;
    padding: 1.2rem 1.4rem;
}

.panel-title {
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1.5px;
    margin-bottom: 1rem;
}

/* Time selector */
.time-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.4rem;
    margin-bottom: 1rem;
}

.time-btn {
    background: rgba(255, 255, 255, 0.04);
    color: var(--text-muted);
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    padding: 0.55rem 0.4rem;
    cursor: pointer;
    font-size: 0.82rem;
    font-weight: 600;
    transition: all 0.2s;
    font-family: 'Inter', sans-serif;
}

.time-btn:hover {
    background: rgba(56, 189, 248, 0.1);
    color: var(--primary);
    border-color: rgba(56, 189, 248, 0.3);
}

.time-btn.active {
    background: var(--primary);
    color: #0a0f1e;
    border-color: var(--primary);
    box-shadow: 0 0 12px rgba(56, 189, 248, 0.3);
}

/* Buttons */
.btn {
    border: none;
    padding: 0.75rem 1rem;
    border-radius: 10px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.85rem;
    font-family: 'Inter', sans-serif;
    transition: all 0.2s;
    width: 100%;
}

.btn-primary {
    background: var(--primary);
    color: #0a0f1e;
}

.btn-primary:hover {
    background: #7dd3fc;
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(56, 189, 248, 0.35);
}

.btn-danger {
    background: rgba(239, 68, 68, 0.12);
    color: var(--danger);
    border: 1px solid rgba(239, 68, 68, 0.25);
}

.btn-danger:hover {
    background: rgba(239, 68, 68, 0.25);
    border-color: var(--danger);
}

.btn-ghost {
    background: var(--panel-bg);
    color: var(--text-muted);
    border: 1px solid var(--glass-border);
}

.btn-ghost:hover {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text);
}

.action-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

/* Move history */
.history-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.move-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.2rem 0;
}

.move-list::-webkit-scrollbar { width: 3px; }
.move-list::-webkit-scrollbar-track { background: transparent; }
.move-list::-webkit-scrollbar-thumb { background: var(--glass-border); border-radius: 4px; }

.history-row {
    display: grid;
    grid-template-columns: 28px 1fr 1fr;
    gap: 0.3rem;
    padding: 0.3rem 0.4rem;
    border-radius: 6px;
    font-size: 0.83rem;
    transition: background 0.15s;
}

.history-row:hover { background: rgba(255, 255, 255, 0.04); }
.history-row:last-child { background: rgba(56, 189, 248, 0.07); }

.move-num { color: var(--text-muted); font-weight: 500; }
.move-san {
    font-weight: 600;
    cursor: pointer;
    padding: 1px 4px;
    border-radius: 4px;
}

.move-san:hover { background: rgba(56, 189, 248, 0.15); color: var(--primary); }
.move-san.active { background: rgba(56, 189, 248, 0.2); color: var(--primary); }

.empty-state {
    color: var(--text-muted);
    font-size: 0.82rem;
    text-align: center;
    padding: 1.5rem 0;
}

/* ─── FOOTER ─────────────────────────────── */
footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: 0.5rem;
    border-top: 1px solid var(--glass-border);
}

.room-info {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-size: 0.78rem;
    color: var(--text-muted);
}

.room-id-badge {
    background: rgba(56, 189, 248, 0.1);
    color: var(--primary);
    border: 1px solid rgba(56, 189, 248, 0.2);
    padding: 2px 10px;
    border-radius: 20px;
    font-weight: 600;
    font-size: 0.75rem;
    letter-spacing: 1px;
}

.btn-icon {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 0.9rem;
    opacity: 0.5;
    transition: opacity 0.2s;
    padding: 4px;
}

.btn-icon:hover { opacity: 1; }

.premove-badge {
    font-size: 0.72rem;
    color: #f97316;
    opacity: 0.8;
}

/* ─── GAME STATUS BAR ────────────────────── */
.status-bar {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-size: 0.8rem;
    font-weight: 600;
}

.tag {
    padding: 3px 10px;
    border-radius: 20px;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.tag-check { background: rgba(239, 68, 68, 0.2); color: var(--danger); }
.tag-waiting { background: rgba(245, 158, 11, 0.15); color: var(--warning); }
.tag-playing { background: rgba(34, 197, 94, 0.15); color: var(--success); }
.tag-info { background: rgba(56, 189, 248, 0.12); color: var(--primary); }

/* ─── MODAL ──────────────────────────────── */
#overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(10px);
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal {
    background: rgba(15, 23, 42, 0.95);
    border: 1px solid var(--glass-border);
    border-radius: 24px;
    padding: 2.5rem 3rem;
    text-align: center;
    max-width: 400px;
    width: 90%;
    box-shadow: 0 30px 60px rgba(0, 0, 0, 0.5);
}

.modal-icon {
    font-size: 3rem;
    margin-bottom: 1rem;
}

.modal h2 {
    font-size: 1.8rem;
    font-weight: 800;
    margin-bottom: 0.5rem;
    letter-spacing: -1px;
}

.modal p {
    color: var(--text-muted);
    margin-bottom: 1.5rem;
    font-size: 0.9rem;
}

.modal-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

/* Promotion modal */
#promotion-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(8px);
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
}

.promotion-modal {
    background: rgba(15, 23, 42, 0.98);
    border: 1px solid var(--glass-border);
    border-radius: 18px;
    padding: 1.5rem;
    display: flex;
    gap: 0.5rem;
}

.promotion-piece {
    width: 72px;
    height: 72px;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    cursor: pointer;
    border-radius: 10px;
    border: 2px solid transparent;
    transition: all 0.15s;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.4));
}

.promotion-piece:hover {
    border-color: var(--primary);
    background-color: rgba(56, 189, 248, 0.1);
    transform: scale(1.08);
}

/* ─── UTILITIES ──────────────────────────── */
.hidden { display: none !important; }

/* ─── RESPONSIVE ─────────────────────────── */
@media (max-width: 900px) {
    :root { --board-size: 380px; }
    .glass-container { padding: 1rem; }
    .game-layout { grid-template-columns: 1fr; }
    .board-area { order: 1; }
    .sidebar-right { order: 2; }
    .clock { font-size: 1.2rem; }
}

@media (max-width: 480px) {
    :root { --board-size: 320px; }
}

/* ─── DRAG & DROP ────────────────────────── */
.drag-ghost {
    position: fixed;
    pointer-events: none;
    z-index: 1000;
    width: 70px;
    height: 70px;
    background-size: 90% 90%;
    background-repeat: no-repeat;
    background-position: center;
    transform: translate(-50%, -50%) scale(1.18);
    filter: drop-shadow(0 8px 20px rgba(0,0,0,0.6));
    transition: none;
}

/* While dragging, show grab cursor everywhere */
body.is-dragging,
body.is-dragging .square,
body.is-dragging .piece {
    cursor: grabbing !important;
}

.square.drag-over {
    background-color: rgba(56, 189, 248, 0.35) !important;
}

/* Pieces show grab cursor */
.square .piece {
    cursor: grab;
}

/* ─── SQUARE ANIMATIONS ──────────────────── */
@keyframes square-pop {
    0%   { transform: scale(1); }
    40%  { transform: scale(1.12); }
    100% { transform: scale(1); }
}

.square.pop {
    animation: square-pop 0.25s ease;
}

@keyframes check-pulse {
    0%, 100% { background-color: rgba(239, 68, 68, 0.6); }
    50%       { background-color: rgba(239, 68, 68, 0.9); }
}

.square.in-check {
    animation: check-pulse 0.8s ease infinite !important;
}

/* ─── CONFETTI ────────────────────────────── */
@keyframes confetti-fall {
    0%   { transform: rotate(0deg) translateY(0); opacity: 1; }
    80%  { opacity: 1; }
    100% { transform: rotate(720deg) translateY(105vh); opacity: 0; }
}

/* ─── SCREEN FLASH (game start) ──────────── */
@keyframes flash-in {
    0%   { opacity: 1; }
    100% { opacity: 0; }
}

/* ─── CARD POP (opponent joined) ─────────── */
@keyframes card-pop {
    0%   { transform: scale(1); }
    40%  { transform: scale(1.04); box-shadow: 0 0 30px rgba(56,189,248,0.5); }
    100% { transform: scale(1); }
}

/* ─── PIECE ENTRANCE (overrides inline transition after load) ── */
.piece {
    transition: opacity 0.3s ease, transform 0.3s cubic-bezier(.34,1.56,.64,1) !important;
}
