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

:root {
    --bg-primary: #0c0c1d;
    --bg-secondary: #14142b;
    --bg-card: #1c1c3a;
    --text-primary: #e8e8f0;
    --text-secondary: #9090b0;
    --accent: #c9a84c;
    --accent-glow: rgba(201, 168, 76, 0.4);
    --sq-light: #f0d9b5;
    --sq-dark: #b58863;
    --sq-selected: rgba(255, 255, 100, 0.45);
    --sq-last-move: rgba(255, 255, 0, 0.2);
    --sq-valid: rgba(0, 0, 0, 0.15);
    --sq-check: rgba(255, 0, 0, 0.55);
    --board-border: #3d2b1f;
    --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

html {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font);
    background: linear-gradient(160deg, var(--bg-primary) 0%, #0a0a28 50%, #10102a 100%);
    color: var(--text-primary);
    min-height: 100%;
    min-height: 100dvh;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

#app {
    width: 100%;
    max-width: 500px;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 12px 8px env(safe-area-inset-bottom, 12px);
    gap: 8px;
}

/* ===== Header ===== */
#header {
    text-align: center;
    padding: 8px 0 4px;
}

.title-row {
    display: flex;
    align-items: baseline;
    gap: 10px;
    justify-content: center;
}

#header h1 {
    font-size: 1.5rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--accent) 0%, #e8c868 50%, var(--accent) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    letter-spacing: 1px;
}

.subtitle {
    font-size: 0.75rem;
    color: var(--text-secondary);
    letter-spacing: 2px;
    text-transform: uppercase;
}

/* ===== Status Bar ===== */
#status-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 20px;
    background: var(--bg-card);
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.06);
    min-width: 200px;
    justify-content: center;
}

#turn-indicator {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.2);
    transition: all 0.3s ease;
}

#turn-indicator.turn-white {
    background: #fff;
    box-shadow: 0 0 8px rgba(255, 255, 255, 0.5);
}

#turn-indicator.turn-black {
    background: #222;
    box-shadow: 0 0 8px rgba(0, 0, 0, 0.5);
}

#status-text {
    font-size: 0.9rem;
    font-weight: 600;
    letter-spacing: 0.5px;
}

/* ===== Board ===== */
#board-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
}

.file-labels {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    width: calc(min(88vw, 440px));
    margin-left: 20px;
    padding: 2px 0;
}

.file-labels span {
    text-align: center;
    font-size: 0.65rem;
    color: var(--text-secondary);
    font-weight: 500;
}

#board-row {
    display: flex;
    align-items: stretch;
}

.rank-labels {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    width: 20px;
}

.rank-labels span {
    text-align: center;
    font-size: 0.65rem;
    color: var(--text-secondary);
    font-weight: 500;
    line-height: 1;
}

#board {
    width: min(88vw, 440px);
    height: min(88vw, 440px);
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    border-radius: 6px;
    overflow: hidden;
    box-shadow:
        0 0 0 3px var(--board-border),
        0 8px 32px rgba(0, 0, 0, 0.5),
        0 2px 8px rgba(0, 0, 0, 0.3);
}

/* ===== Squares ===== */
.square {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.15s ease;
}

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

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

.square.selected {
    background-color: var(--sq-selected) !important;
    box-shadow: inset 0 0 12px rgba(255, 255, 100, 0.3);
}

.square.last-move.light {
    background-color: #f5f682;
}

.square.last-move.dark {
    background-color: #b9ca43;
}

.square.in-check {
    background: radial-gradient(ellipse at center, rgba(255, 0, 0, 0.7) 0%, rgba(255, 0, 0, 0.3) 40%, transparent 70%);
}

/* ===== Valid Move Indicators ===== */
.move-dot {
    width: 28%;
    height: 28%;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.18);
    pointer-events: none;
}

.square.valid-capture::after {
    content: '';
    position: absolute;
    inset: 3px;
    border-radius: 50%;
    border: 3px solid rgba(0, 0, 0, 0.2);
    pointer-events: none;
}

/* ===== Pieces ===== */
.piece {
    width: 85%;
    height: 85%;
    pointer-events: none;
    z-index: 1;
    object-fit: contain;
    transition: transform 0.1s ease;
    filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.25));
}

.square:active .piece {
    transform: scale(1.15);
}

/* ===== Captured Pieces Bar ===== */
.captured-bar {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 12px;
    min-height: 28px;
    width: 100%;
    max-width: 480px;
}

.captured-label {
    font-size: 0.65rem;
    color: var(--text-secondary);
    white-space: nowrap;
}

.captured-pieces {
    display: flex;
    flex-wrap: wrap;
    gap: 1px;
    line-height: 1;
    align-items: center;
}

.cap-piece {
    width: 1.2rem;
    height: 1.2rem;
    object-fit: contain;
    filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.3));
}

/* ===== Controls ===== */
#controls {
    display: flex;
    gap: 12px;
    padding: 4px 0 8px;
}

#controls>button {
    font-family: var(--font);
    font-size: 0.85rem;
    font-weight: 600;
    padding: 10px 24px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    background: var(--bg-card);
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 6px;
}

#controls>button:hover {
    background: rgba(201, 168, 76, 0.15);
    border-color: var(--accent);
    color: var(--accent);
}

#controls>button:active {
    transform: scale(0.96);
}

.btn-icon {
    font-size: 1.1rem;
}

/* ===== Difficulty Selector ===== */
#difficulty-selector {
    display: flex;
    border-radius: 10px;
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: var(--bg-card);
}

.diff-btn {
    font-family: var(--font);
    font-size: 0.8rem;
    font-weight: 600;
    padding: 8px 16px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
}

.diff-btn+.diff-btn {
    border-left: 1px solid rgba(255, 255, 255, 0.06);
}

.diff-btn.active {
    background: linear-gradient(135deg, var(--accent), #e8c868);
    color: #1a1a2e;
}

.diff-btn:not(.active):hover {
    background: rgba(201, 168, 76, 0.12);
    color: var(--text-primary);
}

/* ===== Modals ===== */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    animation: fadeIn 0.2s ease;
}

.overlay.hidden {
    display: none;
}

.modal {
    background: var(--bg-card);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 20px;
    padding: 24px 32px;
    text-align: center;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
    animation: slideUp 0.25s ease;
}

.modal h3 {
    font-size: 1.1rem;
    margin-bottom: 16px;
    color: var(--accent);
}

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

.modal button {
    font-family: var(--font);
    font-size: 0.9rem;
    font-weight: 600;
    padding: 10px 28px;
    border: none;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--accent), #e8c868);
    color: #1a1a2e;
    cursor: pointer;
    transition: all 0.2s ease;
}

.modal button:hover {
    transform: scale(1.03);
    box-shadow: 0 4px 16px var(--accent-glow);
}

#promotion-choices {
    display: flex;
    gap: 12px;
    justify-content: center;
}

.promo-btn {
    width: 60px;
    height: 60px;
    border-radius: 14px;
    border: 2px solid rgba(255, 255, 255, 0.1);
    background: var(--bg-secondary);
    font-size: 2.2rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.promo-btn:hover {
    border-color: var(--accent);
    background: rgba(201, 168, 76, 0.15);
    transform: scale(1.08);
}

.promo-btn:active {
    transform: scale(0.95);
}

/* ===== AI Thinking Indicator ===== */
#status-bar.thinking {
    border-color: var(--accent);
}

#status-bar.thinking #status-text {
    animation: pulse 1.2s ease-in-out infinite;
}

/* ===== Animations ===== */
@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

/* ===== Responsive ===== */
@media (max-height: 700px) {
    #header {
        padding: 4px 0 2px;
    }

    #header h1 {
        font-size: 1.2rem;
    }

    #status-bar {
        padding: 6px 16px;
    }

    .captured-bar {
        padding: 2px 12px;
        min-height: 22px;
    }

    #controls {
        padding: 2px 0 4px;
    }

    #controls button {
        padding: 8px 18px;
        font-size: 0.8rem;
    }

    #app {
        gap: 4px;
        padding: 8px 8px;
    }
}

@media (max-height: 600px) {
    #header {
        display: none;
    }

    .captured-bar {
        display: none;
    }
}

/* disable text selection and callout on iOS */
body {
    -webkit-touch-callout: none;
}