/* Reset & Base */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #302e2b;
    --bg-secondary: #272522;
    --bg-card: #3a3733;
    --text-primary: #e0ddd5;
    --text-secondary: #a09b90;
    --accent: #81b64c;
    --accent-hover: #6fa33d;
    --square-light: #ebecd0;
    --square-dark: #779556;
    --square-selected: rgba(20, 85, 30, 0.5);
    --square-legal: rgba(20, 85, 0, 0.3);
    --square-last-from: rgba(155, 199, 0, 0.41);
    --square-last-to: rgba(155, 199, 0, 0.41);
    --square-check: radial-gradient(ellipse at center, rgba(255,0,0,0.8) 0%, rgba(255,0,0,0.3) 50%, rgba(255,0,0,0) 70%);
    --danger: #e05555;
    --border-radius: 8px;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow-x: hidden;
}

/* Landing Page */
.landing {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.landing h1 {
    font-size: 3rem;
    font-weight: 700;
    letter-spacing: 8px;
    margin-bottom: 8px;
    color: var(--text-primary);
}

.landing .subtitle {
    font-size: 1rem;
    color: var(--text-secondary);
    margin-bottom: 40px;
}

.mode-cards {
    display: flex;
    gap: 24px;
    flex-wrap: wrap;
    justify-content: center;
    max-width: 700px;
}

.mode-card {
    background: var(--bg-card);
    border-radius: var(--border-radius);
    padding: 32px 28px;
    width: 300px;
    text-align: center;
    box-shadow: var(--shadow);
    transition: transform 0.2s, box-shadow 0.2s;
    cursor: default;
}

.mode-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

.mode-card h2 {
    font-size: 1.3rem;
    margin-bottom: 4px;
    letter-spacing: 2px;
}

.mode-card .card-desc {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 20px;
}

.mode-card .chess-icon {
    font-size: 3rem;
    margin-bottom: 16px;
    display: block;
}

/* Difficulty selector */
.difficulty-select {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin-bottom: 20px;
}

.diff-btn {
    width: 40px;
    height: 40px;
    border: 2px solid var(--text-secondary);
    background: transparent;
    color: var(--text-secondary);
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
}

.diff-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.diff-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

/* Buttons */
.btn {
    display: inline-block;
    padding: 12px 28px;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
    text-decoration: none;
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: var(--accent);
    color: #fff;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid rgba(255,255,255,0.1);
}

.btn-secondary:hover {
    background: #3a3733;
}

.btn-danger {
    background: var(--danger);
    color: #fff;
}

.btn-danger:hover {
    background: #c94444;
}

.btn-small {
    padding: 8px 16px;
    font-size: 0.85rem;
}

/* Lobby */
.lobby {
    display: none;
    min-height: 100vh;
    padding: 20px;
}

.lobby-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    max-width: 600px;
    margin: 0 auto 24px;
}

.lobby-header h2 {
    font-size: 1.5rem;
    letter-spacing: 3px;
}

.games-list {
    max-width: 600px;
    margin: 0 auto;
}

.game-item {
    background: var(--bg-card);
    border-radius: var(--border-radius);
    padding: 16px 20px;
    margin-bottom: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: var(--shadow);
}

.game-item-info {
    display: flex;
    flex-direction: column;
}

.game-item-id {
    font-weight: 600;
    font-size: 1rem;
}

.game-item-status {
    color: var(--text-secondary);
    font-size: 0.85rem;
    margin-top: 2px;
}

.status-waiting {
    color: var(--accent);
}

.status-playing {
    color: #d4a847;
}

.lobby-actions {
    text-align: center;
    margin-top: 24px;
}

.lobby-empty {
    text-align: center;
    color: var(--text-secondary);
    padding: 40px;
}

/* Game Page */
.game-page {
    display: none;
    min-height: 100vh;
    padding: 16px;
}

.game-layout {
    max-width: 900px;
    margin: 0 auto;
    display: flex;
    gap: 20px;
    align-items: flex-start;
    justify-content: center;
}

.board-section {
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* Player info bars */
.player-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: var(--bg-card);
    border-radius: 6px;
    width: 100%;
    margin: 4px 0;
}

.player-bar.active-turn {
    background: rgba(129, 182, 76, 0.15);
    border-left: 3px solid var(--accent);
}

.player-icon {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 700;
}

.player-icon.white-icon {
    background: #eee;
    color: #333;
}

.player-icon.black-icon {
    background: #333;
    color: #eee;
}

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

/* Chess Board */
.board-container {
    width: min(88vw, 88vh, 560px);
    height: min(88vw, 88vh, 560px);
    position: relative;
    border-radius: 4px;
    overflow: hidden;
    box-shadow: var(--shadow);
}

.board {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    width: 100%;
    height: 100%;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}

.square {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.square-light {
    background: var(--square-light);
}

.square-dark {
    background: var(--square-dark);
}

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

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

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

/* Legal move indicators */
.square .legal-dot {
    display: none;
    position: absolute;
    width: 28%;
    height: 28%;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.15);
    pointer-events: none;
    z-index: 1;
}

.square .legal-ring {
    display: none;
    position: absolute;
    width: 85%;
    height: 85%;
    border-radius: 50%;
    border: 5px solid rgba(0, 0, 0, 0.15);
    pointer-events: none;
    z-index: 1;
}

.square.legal-move .legal-dot {
    display: block;
}

.square.legal-capture .legal-ring {
    display: block;
}

/* Coordinate labels */
.coord-label {
    position: absolute;
    font-size: 10px;
    font-weight: 700;
    pointer-events: none;
    opacity: 0.7;
    z-index: 2;
}

.coord-file {
    bottom: 2px;
    right: 4px;
}

.coord-rank {
    top: 2px;
    left: 4px;
}

.square-light .coord-label {
    color: var(--square-dark);
}

.square-dark .coord-label {
    color: var(--square-light);
}

/* Chess Pieces */
.piece {
    width: 85%;
    height: 85%;
    cursor: grab;
    z-index: 5;
    transition: none;
    pointer-events: auto;
}

.piece.dragging {
    cursor: grabbing;
    z-index: 100;
    position: fixed;
    width: calc(min(88vw, 88vh, 560px) / 8 * 0.85);
    height: calc(min(88vw, 88vh, 560px) / 8 * 0.85);
    pointer-events: none;
}

.piece.animating {
    transition: transform 0.15s ease;
}

/* Side Panel */
.side-panel {
    width: 220px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.panel-card {
    background: var(--bg-card);
    border-radius: var(--border-radius);
    padding: 16px;
    box-shadow: var(--shadow);
}

.panel-card h3 {
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-bottom: 10px;
    letter-spacing: 1px;
    text-transform: uppercase;
}

.move-list {
    max-height: 300px;
    overflow-y: auto;
    font-size: 0.85rem;
    line-height: 1.8;
}

.move-pair {
    display: flex;
    gap: 8px;
}

.move-number {
    color: var(--text-secondary);
    min-width: 28px;
}

.move-white, .move-black {
    min-width: 55px;
    cursor: default;
}

.game-controls {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.game-status {
    text-align: center;
    padding: 12px;
    font-weight: 600;
    font-size: 0.95rem;
    border-radius: 6px;
}

.status-your-turn {
    background: rgba(129, 182, 76, 0.15);
    color: var(--accent);
}

.status-waiting-turn {
    color: var(--text-secondary);
}

.status-game-over {
    background: rgba(224, 85, 85, 0.15);
    color: var(--danger);
}

.status-spectating {
    background: rgba(100, 100, 100, 0.15);
    color: var(--text-secondary);
}

/* Waiting overlay */
.waiting-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 16px;
}

.waiting-overlay.active {
    display: flex;
}

.waiting-overlay .spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--text-secondary);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.waiting-overlay .waiting-text {
    font-size: 1.2rem;
    color: var(--text-primary);
}

.waiting-overlay .room-code {
    font-size: 0.9rem;
    color: var(--text-secondary);
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Promotion modal */
.promo-modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.promo-modal.active {
    display: flex;
}

.promo-choices {
    background: var(--bg-card);
    border-radius: var(--border-radius);
    padding: 16px;
    display: flex;
    gap: 8px;
    box-shadow: var(--shadow);
}

.promo-choice {
    width: 64px;
    height: 64px;
    cursor: pointer;
    border-radius: 6px;
    border: 2px solid transparent;
    background: var(--bg-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: border-color 0.15s;
}

.promo-choice:hover {
    border-color: var(--accent);
}

.promo-choice svg {
    width: 48px;
    height: 48px;
}

/* Game over modal */
.game-over-modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.game-over-modal.active {
    display: flex;
}

.game-over-content {
    background: var(--bg-card);
    border-radius: var(--border-radius);
    padding: 32px;
    text-align: center;
    box-shadow: var(--shadow);
    min-width: 280px;
}

.game-over-content h2 {
    font-size: 1.5rem;
    margin-bottom: 8px;
}

.game-over-content .result-method {
    color: var(--text-secondary);
    margin-bottom: 24px;
}

.game-over-content .modal-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
}

/* Responsive */
@media (max-width: 768px) {
    .game-layout {
        flex-direction: column;
        align-items: center;
    }

    .side-panel {
        width: 100%;
        max-width: 560px;
    }

    .board-container {
        width: min(94vw, 94vh, 560px);
        height: min(94vw, 94vh, 560px);
    }

    .piece.dragging {
        width: calc(min(94vw, 94vh, 560px) / 8 * 0.85);
        height: calc(min(94vw, 94vh, 560px) / 8 * 0.85);
    }

    .landing h1 {
        font-size: 2rem;
    }

    .mode-cards {
        flex-direction: column;
        align-items: center;
    }

    .mode-card {
        width: 100%;
        max-width: 340px;
    }
}

@media (max-width: 400px) {
    .board-container {
        width: 98vw;
        height: 98vw;
    }

    .piece.dragging {
        width: calc(98vw / 8 * 0.85);
        height: calc(98vw / 8 * 0.85);
    }
}
