/* --- General & Layout --- */
body {
    font-family: 'Microsoft YaHei', 'PingFang TC', sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    margin: 0;
    background-color: #e4cda9;
    overflow: hidden;
}

#game-wrapper {
    position: relative;
}

.hidden {
    display: none !important;
}

/* --- Screens & Overlays --- */
.screen-overlay {
    position: absolute;
    background: rgba(40, 20, 0, 0.85);
    backdrop-filter: blur(8px);
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 100;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    color: white;
}

#checkmate-overlay {
    background: rgba(40, 20, 0, 0.75);
}

.screen-overlay h1 {
    font-size: 3em;
    color: #ffc;
    text-shadow: 0 0 15px #f90, 0 0 30px #f60;
    margin-bottom: 15px;
}

#checkmate-overlay h1 {
    font-size: 6em;
    animation: checkmate-thump 0.5s ease-out;
}

@keyframes checkmate-thump {
    from {
        transform: scale(0.5);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.screen-overlay h2 {
    font-size: 2em;
    color: #fff;
    margin-top: 10px;
}

.screen-overlay button {
    padding: 10px 20px;
    font-size: 16px;
    margin: 8px;
    cursor: pointer;
    background-color: #8b694b;
    color: white;
    border: 2px solid #b99976;
    border-radius: 5px;
    box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
    transition: all 0.2s;
}

.screen-overlay button:hover {
    background-color: #6d543c;
    border-color: #fff;
}

.pve-setup {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    margin-top: 10px;
}

/* --- Main Game & Setup Layout --- */
#game-container,
#setup-container {
    display: flex;
    align-items: flex-start;
    gap: 20px;
}

.main-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* --- Final Layered Board --- */
.board-wrapper {
    width: 570px;
    height: 630px;
    padding: 0;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 8px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
    background-color: #382d26;
}

#board,
#setup-board {
    width: 540px;
    height: 600px;
    display: grid;
    grid-template-columns: repeat(9, 60px);
    grid-template-rows: repeat(10, 60px);
    position: relative;
    background-color: #f3dcb3;
    background-image: url('images/board.png');
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
}

.square {
    width: 60px;
    height: 60px;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* --- Image-based Pieces --- */
.piece {
    width: 54px;
    height: 54px;
    cursor: pointer;
    transition: transform 0.2s, filter 0.2s;
    z-index: 10;
}

.piece img {
    width: 100%;
    height: 100%;
    display: block;
    pointer-events: none;
}

.selected {
    transform: scale(1.1);
    filter: drop-shadow(0 0 8px #0f0);
}

.valid-move-indicator {
    width: 56px;
    height: 56px;
    background-color: rgba(0, 255, 0, 0.3);
    border: 2px dashed #0a0;
    border-radius: 50%;
    position: absolute;
    z-index: 5;
    box-sizing: border-box;
}

.hint-square {
    width: 50px;
    height: 50px;
    background-color: rgba(0, 255, 128, 0.4);
    border: 4px solid #00ff80;
    border-radius: 8px;
    position: absolute;
    z-index: 6;
    box-sizing: border-box;
    animation: hint-pulse 1s infinite alternate;
    pointer-events: none;
}

@keyframes hint-pulse {
    0% {
        transform: scale(0.95);
        opacity: 0.8;
    }

    100% {
        transform: scale(1.05);
        opacity: 1;
    }
}

.moving-piece {
    z-index: 1000;
    position: absolute;
    transition: transform 0.25s ease-in-out;
    pointer-events: none;
}

.piece.is-moving {
    visibility: hidden;
}

.in-check {
    animation: pulse-red 1s infinite;
    border-radius: 50%;
}

@keyframes pulse-red {
    0% {
        box-shadow: 0 0 0 0 rgba(255, 0, 0, 0.7);
    }

    70% {
        box-shadow: 0 0 15px 15px rgba(255, 0, 0, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(255, 0, 0, 0);
    }
}

.capture-effect-standalone {
    position: absolute;
    width: 70px;
    height: 70px;
    border-radius: 50%;
    z-index: 20;
    border: 4px solid #f00;
    animation: shockwave 0.4s ease-out;
    pointer-events: none;
    transform: translate(-50%, -50%);
}

@keyframes shockwave {
    from {
        transform: translate(-50%, -50%) scale(0.5);
        opacity: 1;
    }

    to {
        transform: translate(-50%, -50%) scale(1.5);
        opacity: 0;
    }
}

#controls {
    display: flex;
    gap: 20px;
    margin-top: 20px;
}

#controls button {
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
    background-color: #8b694b;
    color: white;
    border: none;
    border-radius: 5px;
    box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
}

#controls button:disabled {
    background-color: #9e8a78;
    cursor: not-allowed;
}

#setup-panel {
    background: #f3dcb3;
    border: 2px solid #66432d;
    padding: 10px;
    text-align: center;
}

#piece-palette {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    margin-top: 10px;
}

#piece-palette .piece {
    margin: auto;
}

#piece-palette .piece.selected-for-placement {
    filter: drop-shadow(0 0 8px #0f0);
}

.setup-controls {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.setup-controls label {
    font-weight: bold;
}

.setup-controls button,
#delete-piece-btn {
    background-color: #5a9;
    color: white;
    border: none;
    padding: 10px;
    border-radius: 5px;
    cursor: pointer;
}

.setup-controls button:hover,
#delete-piece-btn:hover {
    background-color: #498;
}

.setup-controls .start-btn {
    background-color: #5a5;
}

.setup-controls .start-btn:hover {
    background-color: #494;
}

#delete-piece-btn.active-tool {
    background-color: #e74c3c;
    box-shadow: 0 0 10px #e74c3c;
    border: 1px solid #fff;
}

/* --- Evaluation Panel --- */
#eval-panel {
    width: 225px;
    height: 630px;
    background: #f3dcb3;
    border: 2px solid #66432d;
    border-radius: 8px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
    padding: 10px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
}

#eval-panel h3 {
    text-align: center;
    margin-top: 0;
    margin-bottom: 8px;
    color: #4a382a;
    border-bottom: 1px solid #8b694b;
    padding-bottom: 5px;
}

#eval-bar-container {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
}

#eval-bar {
    flex: 1;
    height: 24px;
    border-radius: 12px;
    overflow: hidden;
    display: flex;
    border: 2px solid #66432d;
    background: #333;
}

#eval-bar-red {
    width: 50%;
    background: linear-gradient(90deg, #c0392b, #e74c3c);
    transition: width 0.6s ease;
}

#eval-bar-black {
    flex: 1;
    background: linear-gradient(90deg, #2c3e50, #1a252f);
    transition: width 0.6s ease;
}

#eval-bar-label {
    font-size: 13px;
    font-weight: bold;
    color: #4a382a;
    min-width: 42px;
    text-align: center;
}

#eval-current {
    text-align: center;
    padding: 8px;
    background: rgba(139, 105, 75, 0.15);
    border-radius: 8px;
    margin-bottom: 8px;
}

#eval-emoji {
    font-size: 36px;
    line-height: 1.1;
    margin-bottom: 2px;
}

#eval-text {
    font-size: 15px;
    font-weight: bold;
    color: #4a382a;
    margin-bottom: 2px;
}

#eval-delta {
    font-size: 13px;
    color: #666;
}

#eval-history-header {
    font-size: 13px;
    font-weight: bold;
    color: #4a382a;
    border-bottom: 1px solid #8b694b;
    padding-bottom: 4px;
    margin-bottom: 4px;
}

#eval-history {
    flex: 1;
    overflow-y: auto;
    font-size: 13px;
}

.eval-history-item {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 3px 4px;
    border-bottom: 1px solid rgba(139, 105, 75, 0.2);
}

.eval-history-item .eval-h-num {
    font-weight: bold;
    min-width: 22px;
    color: #666;
}

.eval-history-item .eval-h-emoji {
    font-size: 14px;
    min-width: 20px;
    text-align: center;
}

.eval-history-item .eval-h-text {
    flex: 1;
    color: #4a382a;
}

.eval-history-item .eval-h-delta {
    font-size: 12px;
    padding: 1px 5px;
    border-radius: 8px;
    font-weight: bold;
}

.eval-cat-good .eval-h-delta {
    background: #27ae60;
    /* Vibrant Green */
    color: #fff;
}

.eval-cat-normal .eval-h-delta {
    background: #2ecc71;
    /* Lighter Green */
    color: #fff;
}

.eval-cat-inaccuracy .eval-h-delta {
    background: #f39c12;
    /* Orange */
    color: #fff;
}

.eval-cat-mistake .eval-h-delta {
    background: #e74c3c;
    /* Red */
    color: #fff;
}

.eval-cat-blunder .eval-h-delta {
    background: #8b0000;
    /* Dark Red */
    color: #fff;
}

.eval-cat-good {
    color: #27ae60;
}

.eval-cat-normal {
    color: #2ecc71;
}

.eval-cat-inaccuracy {
    color: #f39c12;
}

.eval-cat-mistake {
    color: #e74c3c;
}

.eval-cat-blunder {
    color: #8b0000;
}

@keyframes eval-pulse {
    0% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.03);
    }

    100% {
        transform: scale(1);
    }
}

/* --- Notation Panel --- */
#notation-panel {
    width: 225px;
    height: 630px;
    /* Same as board-wrapper */
    background: #f3dcb3;
    border: 2px solid #66432d;
    border-radius: 8px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
    padding: 10px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
}

#notation-panel h3 {
    text-align: center;
    margin-top: 0;
    margin-bottom: 10px;
    color: #4a382a;
    border-bottom: 1px solid #8b694b;
    padding-bottom: 5px;
}

#notation-list {
    flex-grow: 1;
    overflow-y: auto;
    font-size: 16px;
    color: #333;
    padding-right: 5px;
    /* For scrollbar spacing */
}

.notation-tabs {
    display: flex;
    border-bottom: 1px solid #8b694b;
    margin-bottom: 10px;
}

.notation-tabs button {
    flex: 1;
    padding: 8px;
    border: none;
    background-color: transparent;
    cursor: pointer;
    font-size: 16px;
    color: #66432d;
    border-bottom: 3px solid transparent;
    transition: all 0.2s;
}

.notation-tabs button:hover {
    background-color: rgba(139, 105, 75, 0.1);
}

.notation-tabs button.active {
    color: #4a382a;
    border-bottom-color: #8b694b;
    font-weight: bold;
}

#notation-content {
    flex-grow: 1;
    overflow: hidden;
    position: relative;
}

#notation-list,
#pgn-preview {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow-y: auto;
    font-size: 16px;
    color: #333;
    padding-right: 5px;
    /* For scrollbar spacing */
    background-color: #f3dcb3;
    /* Ensure background covers content underneath */
}

#pgn-preview {
    white-space: pre-wrap;
    font-family: 'Courier New', Courier, monospace;
    font-size: 14px;
}

#notation-export-buttons {
    display: flex;
    gap: 10px;
    margin-top: 10px;
}

#exportPgnBtn,
#exportTextBtn {
    flex: 1;
    padding: 8px;
    background-color: #8b694b;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

#exportPgnBtn:hover,
#exportTextBtn:hover {
    background-color: #6d543c;
}

.red-move {
    /* styles for red's moves */
}

.black-move {
    /* styles for black's moves */
}


.notation-move {
    display: flex;
    margin-bottom: 5px;
}

.notation-move-number {
    font-weight: bold;
    width: 30px;
}

.notation-move-red {
    width: 80px;
}

.notation-move-black {
    width: 80px;
}

/* --- Move Navigation Highlights --- */
.red-move:hover,
.black-move:hover {
    background-color: rgba(139, 105, 75, 0.25);
    border-radius: 3px;
}

.notation-active {
    background-color: rgba(52, 152, 219, 0.35) !important;
    border-radius: 3px;
    padding: 1px 2px;
    font-weight: bold;
}

.eval-history-item:hover {
    background-color: rgba(139, 105, 75, 0.2);
}

.eval-history-active {
    background-color: rgba(52, 152, 219, 0.3) !important;
    border-left: 3px solid #3498db;
}

/* ==============================
   RESPONSIVE / MOBILE STYLES
   ============================== */

/* Tablet: slight panel narrowing */
@media (max-width: 1080px) {
    #eval-panel,
    #notation-panel {
        width: 185px;
    }
}

/* iPad portrait and small tablets (769px–1024px):
   3-column horizontal layout overflows here, so switch to
   board-first + panels side-by-side below, like mobile but roomier. */
@media (min-width: 769px) and (max-width: 1024px) {
    body {
        align-items: flex-start;
        overflow-y: auto;
        overflow-x: hidden;
        padding: 12px;
        box-sizing: border-box;
    }

    .screen-overlay {
        position: fixed;
        overflow-y: auto;
        padding: 20px;
        box-sizing: border-box;
    }

    #game-container,
    #setup-container {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        gap: 12px;
        align-items: flex-start;
        width: 100%;
    }

    .main-panel {
        flex: 0 0 100%;
        order: 1;
        width: 100%;
    }

    .board-wrapper {
        align-self: center;
    }

    #controls {
        flex-wrap: wrap;
        justify-content: center;
        gap: 10px;
        margin-top: 12px;
    }

    #eval-panel {
        flex: 1 1 45%;
        width: auto;
        min-width: 200px;
        max-width: calc(50% - 6px);
        height: 290px;
        order: 2;
    }

    #notation-panel {
        flex: 1 1 45%;
        width: auto;
        min-width: 200px;
        max-width: calc(50% - 6px);
        height: 290px;
        order: 3;
    }

    #notation-export-buttons {
        flex-wrap: wrap;
        gap: 6px;
        margin-top: 8px;
    }

    #notation-export-buttons button {
        font-size: 13px;
        padding: 7px 8px;
        flex: 1 1 auto;
    }

    /* Setup: panel goes below board */
    #setup-container {
        flex-direction: column;
    }

    #setup-panel {
        width: 100%;
        order: 2;
    }

    #piece-palette {
        grid-template-columns: repeat(4, 1fr);
    }

    .setup-controls {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
    }

    .setup-controls button,
    #delete-piece-btn {
        flex: 1 1 auto;
        min-width: 140px;
    }

    #settings-panel {
        width: 420px;
        padding: 20px 30px;
    }

    .ai-coach-panel {
        width: calc(100vw - 48px);
        max-height: 80dvh;
        padding: 20px;
    }
}

/* Mobile: full responsive layout */
@media (max-width: 768px) {

    body {
        align-items: flex-start;
        overflow-y: auto;
        overflow-x: hidden;
        padding: 8px;
        box-sizing: border-box;
        min-height: 100dvh;
    }

    /* Overlays must be fixed so they cover the whole viewport */
    .screen-overlay {
        position: fixed;
        overflow-y: auto;
        padding: 12px 8px;
        box-sizing: border-box;
    }

    .screen-overlay h1 {
        font-size: 1.9em;
        margin-bottom: 8px;
    }

    .screen-overlay h2 {
        font-size: 1.3em;
    }

    .screen-overlay button {
        padding: 9px 13px;
        font-size: 14px;
        margin: 4px 3px;
    }

    #checkmate-overlay h1 {
        font-size: 3.5em;
    }

    /* Mode/opening selection table: tighter spacing */
    .screen-overlay table {
        border-spacing: 4px;
    }

    /* Game + Setup containers: wrap so board is full-width first,
       eval and notation panels go side-by-side below */
    #game-container,
    #setup-container {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
        align-items: flex-start;
        width: 100%;
    }

    /* Main panel (board + status + controls): full width, appears first */
    .main-panel {
        flex: 0 0 100%;
        order: 1;
        width: 100%;
    }

    /* Status text: readable size above scaled board */
    #status {
        font-size: 18px;
        margin-bottom: 4px;
    }

    /* Board wrapper: JS will apply transform scale.
       Keep original 570x630 so transform works, but allow it
       to anchor at top-center. */
    .board-wrapper {
        align-self: center;
    }

    /* Controls below board */
    #controls {
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
        margin-top: 8px;
    }

    #controls button {
        padding: 11px 14px;
        font-size: 14px;
    }

    /* Eval panel: left half below board */
    #eval-panel {
        flex: 1 1 45%;
        min-width: 140px;
        max-width: calc(50% - 4px);
        height: 220px;
        order: 2;
    }

    /* Notation panel: right half below board */
    #notation-panel {
        flex: 1 1 45%;
        min-width: 140px;
        max-width: calc(50% - 4px);
        height: 220px;
        order: 3;
    }

    /* Eval panel internals */
    #eval-panel h3,
    #notation-panel h3 {
        font-size: 13px;
        margin-bottom: 4px;
    }

    #eval-emoji {
        font-size: 24px;
    }

    #eval-text {
        font-size: 13px;
    }

    /* Notation export buttons: wrap */
    #notation-export-buttons {
        flex-wrap: wrap;
        gap: 5px;
        margin-top: 6px;
    }

    #notation-export-buttons button {
        font-size: 12px;
        padding: 6px 7px;
        flex: 1 1 auto;
    }

    .notation-tabs button {
        font-size: 14px;
        padding: 6px;
    }

    /* Setup container: setup-panel goes below board */
    #setup-container {
        flex-direction: column;
    }

    #setup-panel {
        width: 100%;
        order: 2;
    }

    #piece-palette {
        grid-template-columns: repeat(4, 1fr);
    }

    .setup-controls {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
    }

    .setup-controls button,
    #delete-piece-btn {
        flex: 1 1 auto;
        min-width: 120px;
    }

    /* Settings panel: fit screen */
    #settings-panel {
        width: calc(100vw - 32px);
        max-width: 400px;
        padding: 16px;
        box-sizing: border-box;
    }

    .setting-item {
        font-size: 16px;
    }

    /* AI coach: fit screen */
    .ai-coach-panel {
        width: calc(100vw - 24px);
        max-height: 85dvh;
        padding: 15px;
    }
}

/* Small phones */
@media (max-width: 400px) {
    .screen-overlay button {
        padding: 8px 10px;
        font-size: 13px;
    }

    #eval-panel,
    #notation-panel {
        height: 180px;
    }

    #eval-emoji {
        font-size: 20px;
    }
}

#notification {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    background-color: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 15px 25px;
    border-radius: 8px;
    z-index: 2000;
    font-size: 18px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    opacity: 0;
    transition: opacity 0.5s, top 0.5s;
    pointer-events: none;
}

#notification.show {
    top: 50px;
    opacity: 1;
}

/* --- Settings Overlay --- */
#settings-panel {
    background: #c9a87c;
    padding: 20px 40px;
    border-radius: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
    border: 2px solid #8b694b;
    color: #4a382a;
    width: 400px;
}

#settings-panel h2 {
    color: #5a483a;
    text-align: center;
    margin-bottom: 25px;
}

.setting-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    font-size: 18px;
}

#settings-close-btn {
    display: block;
    margin: 20px auto 0;
    background-color: #8b694b !important;
}

/* Toggle Switch */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 34px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 26px;
    width: 26px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: .4s;
}

input:checked+.slider {
    background-color: #2196F3;
}

input:focus+.slider {
    box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before {
    transform: translateX(26px);
}

.slider.round {
    border-radius: 34px;
}

.slider.round:before {
    border-radius: 50%;
}

/* --- AI Coach Panel --- */
.ai-coach-panel {
    background: #f3dcb3;
    color: #382d26;
    padding: 20px 30px;
    border-radius: 10px;
    width: 600px;
    max-width: 90vw;
    max-height: 80vh;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    text-align: left;
}

.ai-coach-panel h2 {
    margin-top: 0;
    text-align: center;
    border-bottom: 2px solid #8b694b;
    padding-bottom: 10px;
}

#ai-coach-content {
    flex-grow: 1;
    overflow-y: auto;
    margin: 15px 0;
    padding-right: 10px;
    font-size: 16px;
    line-height: 1.6;
}

#ai-coach-content h3 {
    color: #8b694b;
    margin-top: 20px;
    margin-bottom: 10px;
}

#ai-coach-content p {
    margin: 10px 0;
}

#ai-coach-content ul,
#ai-coach-content ol {
    margin-left: 20px;
    margin-bottom: 10px;
}

#ai-coach-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 30px;
    color: #8b694b;
    font-weight: bold;
}

.spinner {
    border: 5px solid rgba(139, 105, 75, 0.3);
    border-top: 5px solid #8b694b;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    animation: spin 1s linear infinite;
    margin-bottom: 15px;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.ai-coach-panel button {
    align-self: center;
    min-width: 120px;
    margin-top: 10px;
    background-color: #8b694b;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 16px;
    transition: background-color 0.2s;
}

.ai-coach-panel button:hover {
    background-color: #6d543c;
}