/* CSS Variables for theming */
:root {
    --bg: #ffffff;
    --panel: #f5f5f5;
    --tile-dark: #b58863;
    --tile-light: #f0d9b5;
    --piece-dark: #1a1a1a;
    --piece-light: #ffffff;
    --accent: #4a90e2;
    --text: #1a1a1a;
    --muted: #666666;
    --border: #ddd;
    --shadow: rgba(0, 0, 0, 0.1);
    --shadow-strong: rgba(0, 0, 0, 0.2);
    --success: #4caf50;
    --warning: #ff9800;
    --error: #f44336;
}



[data-theme="dark"] {
    --bg: #1a1a1a;
    --panel: #2a2a2a;
    --tile-dark: #769656;
    --tile-light: #eeeed2;
    --piece-dark: #1a1a1a;
    --piece-light: #ffffff;
    --accent: #5a9de2;
    --text: #ffffff;
    --muted: #aaaaaa;
    --border: #444;
    --shadow: rgba(0, 0, 0, 0.3);
    --shadow-strong: rgba(0, 0, 0, 0.5);
}


/* Reset and base styles */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

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

body {
    font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Screen reader only */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* Game wrapper - constrained for iframe */
.game-wrapper {
    display: flex;
    flex-direction: column;
    width: 480px;
    height: 600px;
    min-height: 600px;
    margin: 0 auto;
    overflow: hidden;
    position: relative;
    background: var(--bg);
}

/* Header */
.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0.75rem;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.game-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
}

.game-status {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.875rem;
}

.turn-indicator {
    font-weight: 500;
}

.halfmove-counter {
    color: var(--muted);
    font-size: 0.75rem;
}

.header-right {
    display: flex;
    gap: 0.5rem;
}

/* Main game area */
.game-main {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    min-height: 0;
}

/* Panels container */
.panels-container {
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    flex-grow: 0;
    border-bottom: 1px solid var(--border);
    background: var(--panel);
    min-height: fit-content;
    max-height: 120px;
    overflow-y: auto;
}

/* Panels */
.panel-left,
.panel-right {
    width: 100%;
    padding: 0.5rem 0.75rem;
    background: var(--panel);
    overflow-y: auto;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
    min-height: fit-content;
    flex-shrink: 0;
}

.panel-left {
    border-right: none;
}

.panel-right {
    border-left: none;
}

/* Board container */
.board-container {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.25rem;
    min-width: 0;
    min-height: 0;
    position: relative;
    overflow: hidden;
}

.board {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    aspect-ratio: 1;
    width: 100%;
    max-width: min(100%, calc(100vh - 200px), 480px);
    max-height: min(100%, calc(100vh - 200px), 480px);
    border: 2px solid var(--border);
    box-shadow: 0 4px 12px var(--shadow);
    position: relative;
    background: var(--tile-light);
    transform: rotate(180deg);
    transform-origin: center center;
    margin: 0;
    box-sizing: border-box;
    flex-shrink: 0;
}

.board-square {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    border: none;
    background: transparent;
    padding: 0;
    margin: 0;
    min-height: 32px;
    min-width: 32px;
    touch-action: none;
}

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

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

.board-square:focus {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
    z-index: 10;
}

.board-square.highlight {
    background: var(--accent);
    opacity: 0.6;
}

.board-square.last-move {
    background: rgba(255, 255, 0, 0.4);
}

.board-square.capture-target {
    background: rgba(255, 0, 0, 0.5);
}

.board-square.selected {
    background: var(--accent);
    opacity: 0.8;
}

/* Piece SVG */
.piece {
    width: 75%;
    height: 75%;
    cursor: grab;
    user-select: none;
    pointer-events: auto;
    transition: transform 0.15s ease;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    /* Counter-rotate pieces so they appear upright when board is flipped */
    transform: rotate(180deg);
    /* Prevent white background on SVG */
    background: transparent !important;
    overflow: visible;
    display: block;
    vertical-align: middle;
    /* Ensure SVG renders correctly */
    shape-rendering: geometricPrecision;
}

.piece:active,
.piece.dragging {
    cursor: grabbing;
}

.piece.dragging {
    opacity: 0.7;
    z-index: 100;
    pointer-events: auto;
    /* Prevent SVG from stretching when position: fixed */
    /* Size is set via inline styles in JavaScript */
    background: transparent !important;
    overflow: visible;
}

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

/* Board overlay for drag preview */
.board-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 50;
}


/* Buttons */
.btn {
    padding: 0.4rem 0.75rem;
    font-size: 0.8125rem;
    font-weight: 500;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--bg);
    color: var(--text);
    cursor: pointer;
    transition: all 0.15s ease;
    font-family: inherit;
    min-height: 36px;
    touch-action: manipulation;
}

.btn:hover:not(:disabled) {
    background: var(--panel);
    border-color: var(--accent);
}

.btn:active:not(:disabled) {
    transform: scale(0.98);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

.btn-primary:hover:not(:disabled) {
    background: #3a7dd2;
}

.btn-secondary {
    background: var(--panel);
}

/* Hide Undo, Redo, and Hint buttons */
#undo-btn,
#redo-btn,
#hint-btn {
    display: none;
}

.btn-small {
    padding: 0.375rem 0.75rem;
    font-size: 0.75rem;
    min-height: 36px;
}

.icon-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    border: none;
    background: transparent;
    color: var(--text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background 0.15s ease;
    min-height: 32px;
    min-width: 32px;
    touch-action: manipulation;
}

.icon-btn:hover {
    background: var(--panel);
}

.icon-btn.small {
    width: 28px;
    height: 28px;
    min-height: 28px;
    min-width: 28px;
}

.btn-group {
    display: flex;
    gap: 0.5rem;
}

.btn-group .btn {
    flex: 1;
}

/* Control groups */
.control-group {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
}

.info-panel {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 0.75rem;
    padding: 0.5rem;
    background: var(--bg);
    border-radius: 4px;
    border: 1px solid var(--border);
}

.info-item {
    display: flex;
    justify-content: space-between;
    gap: 0.5rem;
    font-size: 0.8125rem;
    white-space: nowrap;
}

.info-label {
    color: var(--muted);
}

.info-value {
    font-weight: 500;
}


/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
}

.modal[hidden] {
    display: none;
}

.modal-content {
    background: var(--bg);
    border-radius: 8px;
    padding: 1.5rem;
    max-width: 400px;
    width: 100%;
    box-shadow: 0 8px 24px var(--shadow-strong);
    max-height: 90vh;
    overflow-y: auto;
}

.modal-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 1rem;
}

.modal-message {
    margin-bottom: 1.5rem;
    line-height: 1.6;
}

.modal-buttons {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
}

/* Rules */
.rules-content {
    max-height: 60vh;
    overflow-y: auto;
    line-height: 1.6;
}

.rules-content h3 {
    font-size: 1.1rem;
    font-weight: 600;
    margin-top: 1.5rem;
    margin-bottom: 0.75rem;
    color: var(--text);
}

.rules-content h3:first-child {
    margin-top: 0;
}

.rules-content p {
    margin-bottom: 0.5rem;
}

.rules-content ul {
    margin-left: 1.5rem;
    margin-bottom: 1rem;
}

.rules-content li {
    margin-bottom: 0.25rem;
}

/* Settings */
.settings-content {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}

.setting-group h3 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
}

.radio-group,
.checkbox-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.radio-group label,
.checkbox-group label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    font-size: 0.875rem;
    padding: 0.5rem;
    border-radius: 4px;
    transition: background 0.15s ease;
}

.radio-group label:hover,
.checkbox-group label:hover {
    background: var(--panel);
}

.radio-group input[type="radio"],
.checkbox-group input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.setting-item {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 0.75rem;
}

.setting-item label {
    font-size: 0.875rem;
    font-weight: 500;
}

.setting-item input[type="number"] {
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--bg);
    color: var(--text);
    font-family: inherit;
    font-size: 0.875rem;
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Mobile responsive */
@media (max-width: 768px) {
    .panels-container {
        flex-direction: column;
    }

    .panel-left,
    .panel-right {
        width: 100%;
        border-right: none;
        border-left: none;
        border-top: 1px solid var(--border);
        max-height: 120px;
    }

    .panel-left {
        border-right: none;
    }

    .board-container {
        padding: 0.5rem;
    }

    .game-header {
        padding: 0.5rem;
    }

    .game-title {
        font-size: 1.25rem;
    }

    .game-status {
        font-size: 0.75rem;
    }

    .control-group {
        flex-direction: row;
        flex-wrap: wrap;
    }

    .control-group .btn {
        flex: 1;
        min-width: 80px;
    }

}

/* Very small screens */
@media (max-width: 580px) {
    .panel-left,
    .panel-right {
        max-height: 100px;
        padding: 0.5rem;
    }

    .board {
        max-width: 100%;
    }

    .board-square {
        min-height: 32px;
        min-width: 32px;
    }
}

/* Safe area support for notched devices */
@supports (padding: max(0px)) {
    .game-wrapper {
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
        padding-top: max(0px, env(safe-area-inset-top));
        padding-bottom: max(0px, env(safe-area-inset-bottom));
    }
}

