/* ==========================================
   General Reset and Layout (Mobile Optimized)
   ========================================== */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: #f4f4f9;
    color: #333;
    display: flex;
    justify-content: center;
    padding: 20px 10px; /* Less padding on small screens */
}

.container {
    background: #ffffff;
    padding: 25px 15px;
    border-radius: 12px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
    text-align: center;
    width: 100%;
    max-width: 600px;
}

.analysis-layout {
    display: grid;
    gap: 20px;
    align-items: start;
}

.analysis-main,
.analysis-side {
    display: grid;
    gap: 16px;
}

h1 {
    margin-bottom: 20px;
    color: #2c3e50;
    font-size: clamp(1.5rem, 5vw, 2rem); /* Scales with screen width */
}

h2 {
    margin-bottom: 15px;
    color: #34495e;
    font-size: clamp(1.2rem, 4vw, 1.5rem);
}

/* ==========================================
   Controls & Buttons
   ========================================== */
.controls {
    margin-bottom: 25px;
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap; /* Allows wrapping on very narrow screens */
}

button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 15px; /* Larger touch targets for mobile */
    font-size: clamp(14px, 3.5vw, 15px);
    font-weight: 600;
    cursor: pointer;
    background-color: #3498db;
    color: white;
    border: none;
    border-radius: 6px;
    transition: background-color 0.2s ease, transform 0.1s ease;
    flex: 1 1 auto; /* Grow to fill space smoothly */
    min-width: 130px;
}

button:hover { background-color: #2980b9; }
button:active { transform: scale(0.98); }

/* Specific Button Colors */
#diceBtn { background-color: #9b59b6; }
#diceBtn:hover { background-color: #8e44ad; }
#restartGameBtn { background-color: #e67e22; }
#restartGameBtn:hover { background-color: #d35400; }
#copyBtn { background-color: #2ecc71; }
#copyBtn:hover { background-color: #27ae60; }
#copyBtn.copied { background-color: #f39c12; }

/* ==========================================
   Responsive Input Board (Dropdowns)
   ========================================== */
.board {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    border: 2px solid #2c3e50;
    margin: 0 auto 20px;
    width: 100%;
    max-width: 480px; /* Capped max width */
    border-radius: 4px;
    overflow: hidden;
}

.square {
    width: 100%;
    aspect-ratio: 1 / 1; /* Keeps dropdowns perfectly square automatically */
    font-size: clamp(18px, 6vw, 32px); /* Scales icon size down on small phones */
    text-align: center;
    text-align-last: center; /* Centers selected option text */
    border: none;
    outline: none;
    cursor: pointer;
    border-radius: 0; /* Prevents rounded iOS corners */
    
    /* Hides the native dropdown arrow */
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

.square > option { text-align: center; }

/* Board Colors */
.dark { background-color: #b58863; color: black; }
.light { background-color: #f0d9b5; color: black; }

/* ==========================================
   Status Panel
   ========================================== */
.status-panel {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 8px;
    border: 1px solid #ddd;
    margin-bottom: 20px;
    word-wrap: break-word; /* Prevent text overflow on small devices */
}

.status-panel p { margin: 8px 0; font-size: clamp(14px, 4vw, 16px); }
.success { color: #27ae60; }
.error { color: #c0392b; font-weight: bold; }
.hidden { display: none; }

.game-status-panel,
.moves-panel,
.notation-panel {
    background: #f8f9fa;
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 15px;
    margin: 0 auto 20px;
    width: 100%;
    max-width: 480px;
    text-align: left;
}

.moves-panel {
    padding: 15px 15px 12px;
    margin-bottom: 0;
}

.draw-panel {
    background: #fff7e6;
    border: 1px solid #f5c26b;
    border-radius: 8px;
    padding: 15px;
    margin: 0 auto 20px;
    width: 100%;
    max-width: 480px;
    text-align: left;
}

.promotion-panel {
    background: #eef5ff;
    border: 1px solid #8fb5e8;
    border-radius: 8px;
    padding: 15px;
    margin: 20px auto;
    width: 100%;
    max-width: 480px;
    text-align: left;
}

.promotion-hint {
    margin: 0 0 10px;
    font-size: 14px;
    color: #274c77;
    font-weight: 600;
}

.promotion-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.promotion-actions button {
    min-width: 0;
    flex: 1 1 140px;
    background-color: #3a6ea5;
}

.promotion-actions button:hover {
    background-color: #2d5784;
}

.promotion-cancel-btn {
    background-color: #7f8c8d;
}

.promotion-cancel-btn:hover {
    background-color: #687475;
}

.draw-hint {
    margin: 0 0 10px;
    font-size: 14px;
    color: #8a5a00;
    font-weight: 600;
}

.draw-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.draw-actions button {
    min-width: 0;
    flex: 1 1 180px;
    background-color: #c97a12;
}

.draw-actions button:hover {
    background-color: #aa650f;
}

.game-status-panel p {
    margin: 6px 0;
    font-size: clamp(14px, 4vw, 16px);
}

.history-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-top: 14px;
}

.history-actions button {
    min-width: 0;
    flex: 1 1 150px;
    background-color: #566573;
}

#historyForkBtn {
    background-color: #7d5fff;
}

#historyForkBtn:hover {
    background-color: #6846f2;
}

.history-actions button:hover {
    background-color: #45525d;
}

.history-actions button:disabled {
    background-color: #b8c2cc;
    cursor: not-allowed;
    transform: none;
}

.history-status {
    margin-top: 12px;
    font-size: 14px;
    color: #5d6d7e;
}

.history-help {
    margin-top: 8px;
    font-size: 13px;
    color: #7b8794;
}

hr {
    border: 0;
    height: 1px;
    background: #e0e0e0;
    margin: 25px 0;
}

/* ==========================================
   Responsive 8x8 Chessboard
   ========================================== */
.chessboard {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    border: 2px solid #2c3e50;
    margin: 0 auto;
    width: 100%;
    max-width: 480px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    border-radius: 4px;
    overflow: hidden;
}

.board-square {
    width: 100%;
    min-width: 0;
    aspect-ratio: 1 / 1;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0;
    margin: 0;
    flex: 0 0 auto;
    font-size: clamp(20px, 7vw, 38px); /* Dynamic piece size */
    cursor: pointer;
    user-select: none;
    border: none;
    border-radius: 0;
    min-height: 0;
    transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}

.dark-square { background-color: #b58863; color: #000; }
.light-square { background-color: #f0d9b5; color: #000; }
.board-square:hover { filter: brightness(1.05); }
.board-square:focus-visible {
    outline: 3px solid #1f6feb;
    outline-offset: -3px;
}
.selected-square {
    box-shadow: inset 0 0 0 4px rgba(31, 111, 235, 0.95);
}
.legal-target {
    box-shadow: inset 0 0 0 999px rgba(46, 204, 113, 0.25);
}
.piece-white,
.piece-black {
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.15);
}

.move-log {
    display: grid;
    gap: 8px;
    max-height: 280px;
    overflow-y: auto;
}

.move-log-empty {
    font-size: 15px;
    color: #2c3e50;
}

.move-log-empty {
    margin: 0;
}

.move-log-row {
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr) minmax(0, 1fr);
    gap: 8px;
    align-items: stretch;
}

.move-log-number {
    display: flex;
    align-items: center;
    justify-content: center;
    font: 700 13px/1 "Consolas", "Courier New", monospace;
    color: #6b7c93;
    background: #eef2f6;
    border-radius: 8px;
}

.move-log-cell {
    min-width: 0;
}

.move-log-item {
    width: 100%;
    min-width: 0;
    justify-content: flex-start;
    align-items: center;
    justify-content: space-between;
    border-radius: 6px;
    padding: 10px 12px;
    font-size: 14px;
    font-weight: 600;
    background: #ffffff;
    color: #2c3e50;
    border: 1px solid #d8e0e8;
    transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.move-log-item:hover {
    background-color: #e8eef7;
    border-color: #b7c6d8;
}

.move-log-item.active {
    background-color: #d7e7fb;
    color: #1f4f82;
    font-weight: 700;
    border-color: #7aa5d6;
}

.move-log-item.variation-line {
    background: #f7f0ff;
    border-color: #c8b5f6;
    color: #4f378b;
}

.move-log-item.branch-point {
    box-shadow: inset 3px 0 0 #7d5fff;
}

.move-log-item.empty {
    background: transparent;
    border: 1px dashed #d8e0e8;
    color: #9aa5b1;
    cursor: default;
}

.move-log-item.empty:hover {
    background: transparent;
    border-color: #d8e0e8;
}

.move-log-badge {
    flex: 0 0 auto;
    margin-left: 8px;
    padding: 3px 7px;
    border-radius: 999px;
    background: rgba(125, 95, 255, 0.12);
    color: #5b3fd1;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.02em;
}

@media (max-width: 560px) {
    .move-log-row {
        grid-template-columns: 38px minmax(0, 1fr) minmax(0, 1fr);
        gap: 6px;
    }

    .move-log-item {
        padding: 10px;
        font-size: 13px;
    }
}

.notation-label {
    display: block;
    margin: 12px 0 6px;
    font-size: 14px;
    font-weight: 700;
    color: #34495e;
}

.notation-textarea {
    width: 100%;
    resize: vertical;
    border: 1px solid #cfd8dc;
    border-radius: 8px;
    padding: 10px 12px;
    font: 14px/1.5 "Consolas", "Courier New", monospace;
    color: #1f2933;
    background: #ffffff;
}

.notation-textarea-large {
    min-height: 180px;
}

.notation-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-top: 10px;
}

.notation-actions button {
    min-width: 0;
    flex: 1 1 180px;
}

.notation-status {
    margin-top: 12px;
    font-size: 14px;
    color: #34495e;
}

@media (min-width: 920px) {
    .container {
        max-width: 1080px;
        padding: 28px 24px;
    }

    .analysis-layout {
        grid-template-columns: minmax(0, 520px) minmax(280px, 1fr);
        gap: 24px;
    }

    .game-status-panel,
    .draw-panel,
    .promotion-panel,
    .chessboard,
    .moves-panel,
    .notation-panel,
    .board,
    .status-panel {
        max-width: none;
    }

    .moves-panel {
        position: sticky;
        top: 20px;
        max-height: calc(100vh - 60px);
        overflow: hidden;
    }

    .move-log {
        max-height: calc(100vh - 190px);
    }
}
