:root {
    --bg-main: #1a1612;
    --bg-panel: rgba(36, 30, 24, 0.85);
    --border-panel: rgba(212, 175, 55, 0.25);
    --gold: #d4af37;
    --gold-glow: rgba(212, 175, 55, 0.5);
    --board-border: #3b2818;
    --square-light: #e8d8b8;
    --square-dark: #8b5a2b;
    --square-highlight-from: rgba(255, 235, 59, 0.35);
    --square-highlight-to: rgba(255, 235, 59, 0.45);
    --square-valid: rgba(46, 204, 113, 0.6);
    --square-jump: rgba(231, 76, 60, 0.6);
    --p1-piece-bg: radial-gradient(circle at 35% 35%, #fffdfa 0%, #ecdcc9 45%, #bca58c 100%);
    --p1-piece-border: #8c765c;
    --p2-piece-bg: radial-gradient(circle at 35% 35%, #424242 0%, #212121 55%, #111111 100%);
    --p2-piece-border: #0d0d0d;
    --text-main: #f5eedb;
    --text-muted: #b8a99a;
    --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Sarabun", sans-serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

body, html {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: var(--bg-main);
    color: var(--text-main);
    font-family: var(--font-family);
    display: flex;
    justify-content: center;
    align-items: center;
}

#app-container {
    width: 100vw;
    height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    max-width: 600px;
    margin: 0 auto;
    position: relative;
}

/* Header & Top Bar */
.top-nav {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 12px;
    background: var(--bg-panel);
    border-radius: 12px;
    border: 1px solid var(--border-panel);
    backdrop-filter: blur(8px);
}

.game-title {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--gold);
    display: flex;
    align-items: center;
    gap: 6px;
}

.header-actions {
    display: flex;
    gap: 6px;
}

.icon-btn {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(212, 175, 55, 0.4);
    color: var(--gold);
    border-radius: 8px;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 1.1rem;
    transition: all 0.2s;
}

.icon-btn:hover, .icon-btn:active {
    background: var(--gold);
    color: #1a1612;
    transform: scale(1.05);
}

.ui-icon {
    width: 20px;
    height: 20px;
    display: block;
    stroke: currentColor;
    transition: transform 0.2s ease;
}

.icon-btn:hover .ui-icon {
    transform: scale(1.1);
}

.lang-btn {
    font-size: 0.85rem;
    font-weight: 700;
    letter-spacing: 0.5px;
    border-color: rgba(212, 175, 55, 0.4);
    color: var(--gold);
}

/* Player Info Bars */
.player-bar {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 14px;
    background: var(--bg-panel);
    border-radius: 10px;
    border: 1px solid transparent;
    transition: border-color 0.3s, box-shadow 0.3s;
}

.player-bar.active-turn {
    border-color: var(--gold);
    box-shadow: 0 0 12px var(--gold-glow);
}

/* AI Thinking Load Spinner on Circled Avatar */
.player-bar.thinking .avatar-p2 {
    position: relative;
    border-color: rgba(241, 196, 15, 0.3) !important;
}

.player-bar.thinking .avatar-p2::after {
    content: '';
    position: absolute;
    top: -4px;
    left: -4px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-top-color: var(--gold);
    border-right-color: var(--gold);
    box-sizing: border-box;
    animation: avatar-load-spin 0.75s linear infinite;
    pointer-events: none;
}

@keyframes avatar-load-spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.player-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.player-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 2px solid #fff;
    box-shadow: 0 2px 5px rgba(0,0,0,0.5);
    position: relative;
}

.avatar-p1 {
    background: var(--p1-piece-bg);
    border-color: var(--p1-piece-border);
}

.avatar-p2 {
    background: var(--p2-piece-bg);
    border-color: #666;
    transition: all 0.3s ease;
}

.player-name {
    font-weight: 600;
    font-size: 0.95rem;
    display: inline-flex;
    align-items: center;
}

.player-stats {
    display: flex;
    gap: 12px;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.stat-item {
    display: flex;
    align-items: center;
    gap: 4px;
}

.crown-stat-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-right: 2px;
}

.ui-crown-mini {
    width: 17px;
    height: 17px;
    fill: var(--gold);
    display: inline-block;
    vertical-align: middle;
    filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.6));
}

.stat-count {
    font-weight: bold;
    color: var(--gold);
}

/* Game Board Area */
.board-wrapper {
    position: relative;
    width: min(88vw, 62vh, 480px);
    height: min(88vw, 62vh, 480px);
    aspect-ratio: 1 / 1;
    margin: auto 0;
    border-radius: 14px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.7), inset 0 0 10px rgba(0, 0, 0, 0.5);
    background: var(--board-border);
    padding: 10px;
    border: 2px solid #5a3e26;
    transition: box-shadow 0.4s ease, border-color 0.4s ease;
}

/* Board Micro-Shake on Capture */
.board-wrapper.screen-shake {
    animation: board-shake 0.24s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

@keyframes board-shake {
    0%, 100% { transform: translate(0, 0); }
    20% { transform: translate(-3px, 2px); }
    40% { transform: translate(3px, -2px); }
    60% { transform: translate(-2px, 1px); }
    80% { transform: translate(2px, -1px); }
}

#board {
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    border-radius: 8px;
    overflow: hidden;
    position: relative;
}

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

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

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

.square.last-move-from {
    background-color: #b08d4a !important;
}

.square.last-move-to {
    background-color: #c7a45d !important;
}

.square.last-move-captured {
    background-color: rgba(220, 53, 69, 0.45) !important;
}

.square.last-move-captured::after {
    content: '✕';
    position: absolute;
    font-size: 1.5rem;
    font-weight: 900;
    color: rgba(255, 255, 255, 0.75);
    text-shadow: 0 0 8px rgba(0, 0, 0, 0.9);
    pointer-events: none;
    animation: captured-flash 1s ease-out;
}

@keyframes captured-flash {
    0% { transform: scale(1.6); opacity: 1; }
    50% { transform: scale(1.1); opacity: 0.9; }
    100% { transform: scale(1.0); opacity: 0.75; }
}

/* Valid Move Indicators */
.valid-target-marker {
    width: 32%;
    height: 32%;
    border-radius: 50%;
    background: var(--square-valid);
    box-shadow: 0 0 8px rgba(46, 204, 113, 0.8);
    pointer-events: none;
    animation: pulse-marker 1.2s infinite ease-in-out;
}

.valid-jump-marker {
    width: 38%;
    height: 38%;
    border-radius: 50%;
    background: var(--square-jump);
    box-shadow: 0 0 10px rgba(231, 76, 60, 0.9);
    pointer-events: none;
    animation: pulse-marker 0.8s infinite ease-in-out;
}

@keyframes pulse-marker {
    0%, 100% { transform: scale(0.9); opacity: 0.7; }
    50% { transform: scale(1.15); opacity: 1; }
}

/* Pieces */
.piece {
    width: 82%;
    height: 82%;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5), inset 0 2px 3px rgba(255, 255, 255, 0.4);
    position: relative;
    transition: transform 0.15s ease, filter 0.2s ease;
    z-index: 2;
}

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

.piece.p1 {
    background: var(--p1-piece-bg);
    border: 3px solid var(--p1-piece-border);
}

.piece.p2 {
    background: var(--p2-piece-bg);
    border: 3px solid var(--p2-piece-border);
}

.piece.selected {
    outline: 3px solid var(--gold);
    outline-offset: 3px;
    filter: drop-shadow(0 0 14px var(--gold));
    transform: translateY(-6px) scale(1.12);
    box-shadow: 0 14px 22px rgba(0, 0, 0, 0.65), inset 0 2px 4px rgba(255, 255, 255, 0.5);
    z-index: 10;
}

/* Piece Landing Bounce */
.piece.landed {
    animation: piece-land 0.28s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

@keyframes piece-land {
    0% { transform: scale(1.22) translateY(-8px); }
    55% { transform: scale(0.9, 1.1) translateY(1px); }
    75% { transform: scale(1.05, 0.95); }
    100% { transform: scale(1) translateY(0); }
}

.piece.mandatory-jump {
    animation: pulse-mandatory 1s infinite alternate;
}

@keyframes pulse-mandatory {
    from { filter: drop-shadow(0 0 2px #e74c3c); }
    to { filter: drop-shadow(0 0 10px #e74c3c); }
}

/* King Crown Icon */
.crown-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    color: var(--gold);
    pointer-events: none;
}

.piece-crown-svg {
    width: 24px;
    height: 24px;
    fill: currentColor;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.8));
}

.crown-icon.drop {
    display: flex;
    animation: crown-drop 0.55s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes crown-drop {
    0% { transform: translateY(-28px) scale(0) rotate(-45deg); opacity: 0; }
    60% { transform: translateY(2px) scale(1.4) rotate(8deg); opacity: 1; filter: drop-shadow(0 0 12px #ffeaa7); }
    80% { transform: translateY(-2px) scale(1.05) rotate(-3deg); }
    100% { transform: translateY(0) scale(1) rotate(0deg); opacity: 1; }
}

.piece.p1 .crown-icon {
    color: #9c6c10;
}

.piece.p2 .crown-icon {
    color: #f1c40f;
}

/* King Promotion Celebration Aura */
.piece.king-promoted {
    animation: king-pulse 0.65s ease-out;
}

@keyframes king-pulse {
    0% { transform: scale(0.9); filter: drop-shadow(0 0 4px var(--gold)); }
    50% { transform: scale(1.22); filter: drop-shadow(0 0 24px #ffd700); }
    100% { transform: scale(1); filter: drop-shadow(0 0 8px var(--gold)); }
}

.sunburst-overlay {
    position: absolute;
    width: 220%;
    height: 220%;
    top: -60%;
    left: -60%;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(255, 215, 0, 0.7) 0%, rgba(255, 179, 0, 0.3) 45%, transparent 70%);
    pointer-events: none;
    z-index: 15;
    animation: sunburst-fade 0.75s ease-out forwards;
}

@keyframes sunburst-fade {
    0% { transform: scale(0.4) rotate(0deg); opacity: 0; }
    30% { opacity: 1; transform: scale(1.15) rotate(45deg); }
    100% { opacity: 0; transform: scale(1.6) rotate(90deg); }
}

/* Capture Shatter & Wood Chip Particles */
.capture-shatter {
    position: absolute;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: none;
    z-index: 8;
}

.capture-shatter .shatter-ring {
    position: absolute;
    width: 50%;
    height: 50%;
    border-radius: 50%;
    border: 3px solid #ff4757;
    box-shadow: 0 0 14px rgba(255, 71, 87, 0.9);
    animation: shatter-ring 0.4s ease-out forwards;
}

@keyframes shatter-ring {
    0% { transform: scale(0.6); opacity: 1; border-width: 4px; }
    100% { transform: scale(2.3); opacity: 0; border-width: 1px; }
}

.capture-shatter .wood-chip {
    position: absolute;
    width: 8px;
    height: 8px;
    border-radius: 2px;
    background: #c7a45d;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.6);
    animation: wood-chip-fly 0.45s ease-out forwards;
}

@keyframes wood-chip-fly {
    0% { transform: translate(0, 0) scale(1) rotate(0deg); opacity: 1; }
    100% { transform: translate(var(--chip-dx, 20px), var(--chip-dy, -20px)) scale(0.2) rotate(var(--chip-rot, 180deg)); opacity: 0; }
}

/* Multi-Jump Combo Banner */
.combo-banner {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 1.6rem;
    font-weight: 900;
    color: #fff;
    text-shadow: 0 0 12px rgba(255, 71, 87, 0.9), 0 2px 6px rgba(0, 0, 0, 0.9);
    background: linear-gradient(135deg, rgba(231, 76, 60, 0.94), rgba(241, 196, 15, 0.94));
    padding: 8px 22px;
    border-radius: 30px;
    border: 2px solid #fff;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.6), 0 0 20px rgba(241, 196, 15, 0.8);
    pointer-events: none;
    z-index: 50;
    animation: combo-float 0.85s cubic-bezier(0.2, 0.9, 0.3, 1.2) forwards;
    white-space: nowrap;
}

@keyframes combo-float {
    0% { transform: translate(-50%, -20%) scale(0.5); opacity: 0; }
    25% { transform: translate(-50%, -50%) scale(1.25); opacity: 1; }
    50% { transform: translate(-50%, -55%) scale(1.1); opacity: 1; }
    100% { transform: translate(-50%, -85%) scale(0.95); opacity: 0; }
}

/* Confetti Overlay */
#confetti-canvas {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    pointer-events: none;
    z-index: 150;
}

/* Bottom Controls */
.bottom-controls {
    width: 100%;
    display: flex;
    justify-content: center;
    gap: 12px;
    padding: 8px 0;
}

.action-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px 24px;
    background: var(--bg-panel);
    border: 1px solid rgba(212, 175, 55, 0.4);
    color: var(--gold);
    border-radius: 10px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    min-width: 100px;
    text-align: center;
    transition: all 0.2s;
}

.action-btn:hover:not(:disabled), .action-btn:active:not(:disabled) {
    background: var(--gold);
    color: #1a1612;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px var(--gold-glow);
}

.action-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* Status Banner */
.status-banner {
    font-size: 0.88rem;
    color: var(--gold);
    font-weight: 500;
    min-height: 1.4rem;
    text-align: center;
    line-height: 1.4;
}


/* Modals */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.75);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    padding: 20px;
    backdrop-filter: blur(5px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal-content {
    background: #241e18;
    border: 2px solid var(--gold);
    border-radius: 16px;
    padding: 24px;
    max-width: 420px;
    width: 100%;
    max-height: 85vh;
    overflow-y: auto;
    color: var(--text-main);
    box-shadow: 0 10px 30px rgba(0,0,0,0.8);
    position: relative;
}

.modal-title {
    font-size: 1.3rem;
    color: var(--gold);
    margin-bottom: 14px;
    text-align: center;
}

.modal-body {
    font-size: 0.9rem;
    line-height: 1.5;
    color: #e0d5c5;
    margin-bottom: 20px;
}

.modal-body h4 {
    color: var(--gold);
    margin: 12px 0 6px 0;
}

.modal-body ul {
    margin-left: 20px;
    margin-bottom: 10px;
}

.modal-body li {
    margin-bottom: 4px;
}

.modal-footer {
    display: flex;
    justify-content: center;
    gap: 12px;
}

.primary-btn {
    background: var(--gold);
    color: #1a1612;
    border: none;
    border-radius: 8px;
    padding: 10px 24px;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
    transition: transform 0.2s;
}

.primary-btn:hover {
    transform: scale(1.05);
}

.secondary-btn {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text-main);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    padding: 10px 22px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.secondary-btn:hover {
    background: rgba(255, 255, 255, 0.16);
    border-color: rgba(255, 255, 255, 0.35);
    transform: scale(1.03);
}

.modal-close-btn {
    position: absolute;
    top: 12px;
    right: 14px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s, background 0.2s, transform 0.15s;
}

.modal-close-btn:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.1);
    transform: scale(1.12);
}

.close-svg {
    display: block;
}

.option-group {
    margin-bottom: 16px;
}

.option-group label {
    display: block;
    margin-bottom: 6px;
    color: var(--gold);
    font-weight: 600;
}

.option-select {
    width: 100%;
    padding: 8px 12px;
    background: #1a1612;
    color: #fff;
    border: 1px solid #5a3e26;
    border-radius: 8px;
    font-size: 0.95rem;
}

/* Black Player Mode */
body.player-black {
    --p1-piece-bg: radial-gradient(circle at 35% 35%, #424242 0%, #212121 55%, #111111 100%);
    --p1-piece-border: #0d0d0d;
    --p2-piece-bg: radial-gradient(circle at 35% 35%, #fffdfa 0%, #ecdcc9 45%, #bca58c 100%);
    --p2-piece-border: #8c765c;
}

body.player-black .avatar-p1 {
    border-color: #666;
}

body.player-black .avatar-p2 {
    border-color: var(--p2-piece-border);
}

body.player-black .piece.p1 .crown-icon {
    color: #f1c40f;
}

body.player-black .piece.p2 .crown-icon {
    color: #9c6c10;
}

/* Stats Modal Styling */
.stats-modal-content {
    max-width: 440px;
}

.stats-summary-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    margin-bottom: 16px;
}

.stats-card {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(212, 175, 55, 0.22);
    border-radius: 10px;
    padding: 8px 10px;
    text-align: center;
}

.stats-card-val {
    font-size: 1.3rem;
    font-weight: 800;
    color: var(--gold);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.stat-mini-svg {
    display: inline-block;
    vertical-align: middle;
    flex-shrink: 0;
}

.flame-svg {
    color: #e67e22;
    fill: #e67e22;
    filter: drop-shadow(0 0 5px rgba(230, 126, 34, 0.7));
}

.star-svg {
    color: #f1c40f;
    fill: #f1c40f;
    filter: drop-shadow(0 0 5px rgba(241, 196, 15, 0.7));
}

.target-svg {
    stroke: var(--gold);
    fill: none;
    filter: drop-shadow(0 0 4px var(--gold-glow));
}

.stats-card-label {
    font-size: 0.76rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.stats-section-title {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--gold);
    margin: 14px 0 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.stats-diff-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.stats-diff-card {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.07);
    border-radius: 8px;
    padding: 8px 12px;
}

.stats-diff-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 5px;
    font-size: 0.85rem;
}

.stats-diff-name {
    font-weight: 700;
    color: #fff;
}

.stats-diff-record {
    font-size: 0.78rem;
    color: var(--text-muted);
}

.stats-diff-rate {
    font-weight: 700;
    color: var(--gold);
}

.stats-bar-track {
    width: 100%;
    height: 6px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 4px;
    overflow: hidden;
}

.stats-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, #e67e22, var(--gold));
    border-radius: 4px;
    transition: width 0.4s ease;
}
