:root {
    /* Color System */
    --color-primary: #6366f1; /* Indigo-500 */
    --color-primary-hover: #4f46e5; /* Indigo-600 */
    --color-secondary: #e2e8f0; /* Slate-200 */
    --color-secondary-hover: #cbd5e1; /* Slate-300 */
    --color-secondary-text: #334155; /* Slate-700 */
    
    --color-success: #10b981; /* Emerald-500 */
    --color-warning: #f59e0b; /* Amber-500 */
    --color-danger: #ef4444; /* Red-500 */
    
    --color-background: #f1f5f9; /* Slate-100 */
    --color-surface: #ffffff;
    
    --color-text-main: #0f172a; /* Slate-900 */
    --color-text-muted: #64748b; /* Slate-500 */
    --color-border: #e2e8f0; /* Slate-200 */

    /* Spacing & Layout */
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
    --spacing-xl: 32px;
    
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 24px;
    
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    
    --font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

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

body {
    font-family: var(--font-family);
    background-color: var(--color-background);
    color: var(--color-text-main);
    line-height: 1.5;
    height: 100vh;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
}

button, input, select {
    font-family: inherit;
    font-size: 100%;
}

/* Icons */
.icon {
    width: 20px;
    height: 20px;
    display: inline-block;
    vertical-align: middle;
}

.icon-sm {
    width: 16px;
    height: 16px;
}

.icon-md {
    width: 24px;
    height: 24px;
}

/* Animations */
@keyframes spin {
    to { transform: rotate(360deg); }
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translate3d(0, 30px, 0);
    }
    to {
        opacity: 1;
        transform: translate3d(0, 0, 0);
    }
}

@keyframes fadeOut {
    to { opacity: 0; visibility: hidden; }
}

/* Page Loader */
.page-loader {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--color-background);
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: opacity 0.5s ease, visibility 0.5s ease;
}

.page-loader.loaded {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.loader-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--color-secondary);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.loader-content p {
    color: var(--color-text-muted);
    font-size: 0.9rem;
    font-weight: 500;
    letter-spacing: 0.05em;
    animation: pulse 2s infinite ease-in-out;
}

/* Main Menu */
.main-menu {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--color-background);
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: opacity 0.5s ease, transform 0.5s ease;
}

.main-menu.hidden {
    opacity: 0;
    pointer-events: none;
    transform: scale(1.05);
}

.menu-content {
    text-align: center;
    animation: fadeInUp 0.8s cubic-bezier(0.2, 0.8, 0.2, 1) both;
    animation-delay: 0.2s;
}

.logo-area {
    margin-bottom: 40px;
}

.logo-icon {
    display: flex;
    justify-content: center;
    gap: -10px;
    margin-bottom: 20px;
}

.stone {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

.stone.black {
    background: radial-gradient(circle at 30% 30%, #666, #000);
    z-index: 2;
}

.stone.white {
    background: radial-gradient(circle at 30% 30%, #fff, #ddd);
    margin-left: -15px;
    z-index: 1;
}

.menu-title {
    font-size: 3rem;
    font-weight: 900;
    color: var(--color-text-main);
    letter-spacing: -0.05em;
    margin-bottom: 8px;
}

.menu-title .highlight {
    color: var(--color-primary);
    position: relative;
    display: inline-block;
}

.menu-subtitle {
    font-size: 1.1rem;
    color: var(--color-text-muted);
    font-weight: 500;
    letter-spacing: 0.1em;
    text-transform: uppercase;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 16px;
    width: 280px;
    margin: 0 auto;
}

.btn-xl {
    padding: 16px 32px;
    font-size: 1.1rem;
    border-radius: var(--radius-md);
}

.menu-footer {
    margin-top: 60px;
    color: var(--color-text-muted);
    font-size: 0.85rem;
    opacity: 0.6;
}

/* App Container (Game Interface) */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: var(--spacing-lg);
    transition: opacity 0.5s ease;
}

.app-container.hidden {
    display: none;
    opacity: 0;
}

.game-header {
    text-align: center;
    margin-bottom: var(--spacing-lg);
    flex-shrink: 0;
    position: relative; /* For back button positioning */
    animation: fadeInUp 0.8s cubic-bezier(0.2, 0.8, 0.2, 1) both;
}

.game-header h1 {
    font-size: 2rem;
    font-weight: 800;
    color: var(--color-text-main);
    letter-spacing: -0.03em;
}

.game-main {
    display: flex;
    flex: 1;
    gap: var(--spacing-xl);
    justify-content: center;
    align-items: flex-start;
    overflow: hidden;
}

/* Board Area */
.board-container {
    flex: 0 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: var(--spacing-md);
    background: var(--color-surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    position: relative;
    border: 1px solid white;
    animation: fadeInUp 0.8s cubic-bezier(0.2, 0.8, 0.2, 1) both;
    animation-delay: 0.1s;
}

canvas {
    border-radius: 4px;
    cursor: crosshair;
}

/* Sidebar */
.game-sidebar {
    flex: 0 0 320px;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-lg);
    height: 100%;
    overflow-y: auto;
    padding: 2px;
    animation: fadeInUp 0.8s cubic-bezier(0.2, 0.8, 0.2, 1) both;
    animation-delay: 0.2s;
}

/* Components: Card */
.card {
    background: var(--color-surface);
    border-radius: var(--radius-md);
    padding: var(--spacing-lg);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--color-border);
}

/* Status Panel */
.status-panel {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--spacing-md);
}

.status-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
}

.status-item .label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: var(--spacing-xs);
}

.status-item .value {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--color-text-main);
    font-variant-numeric: tabular-nums;
}

.status-item .value.highlight {
    color: var(--color-primary);
}

/* Control Panel */
.control-panel {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

/* Components: Button */
.btn {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    gap: 8px; /* Icon gap */
    padding: 12px 20px;
    font-size: 0.95rem;
    font-weight: 600;
    border-radius: var(--radius-sm);
    border: none;
    cursor: pointer;
    transition: all 0.2s ease;
    width: 100%; /* Full width in sidebar */
}

.btn:active {
    transform: translateY(1px);
}

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

.btn-primary {
    background-color: var(--color-text-main);
    color: white;
}

.btn-primary:hover:not(:disabled) {
    background-color: #000;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

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

.btn-secondary:hover:not(:disabled) {
    background-color: var(--color-secondary-hover);
}

.btn-icon {
    background-color: transparent;
    color: var(--color-text-muted);
    border: 1px dashed var(--color-border);
    justify-content: center;
}

.btn-icon:hover {
    color: var(--color-primary);
    border-color: var(--color-primary);
    background-color: rgba(99, 102, 241, 0.05);
}

/* Difficulty Selector */
.difficulty-selector {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 0;
}

.difficulty-selector label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--color-text-muted);
}

.difficulty-selector select {
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--color-border);
    background-color: white;
    color: var(--color-text-main);
    font-weight: 500;
    cursor: pointer;
    outline: none;
    transition: all 0.2s;
}

.difficulty-selector select:focus {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

/* Modals */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    opacity: 1;
    transition: opacity 0.3s ease;
}

.modal-overlay.hidden {
    display: none;
    opacity: 0;
}

.modal {
    background: var(--color-surface);
    padding: var(--spacing-xl);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 450px; /* Increased from 400px */
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    border: 1px solid var(--color-border);
    animation: slideUp 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal.hidden {
    display: none;
}

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

/* Settings Modal Specifics */
.settings-modal {
    padding: 0;
    overflow: hidden;
}

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--spacing-lg);
    border-bottom: 1px solid var(--color-border);
    background-color: #fafafa;
}

.settings-header h3 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--color-text-main);
}

.close-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--color-text-muted);
    padding: 4px;
    border-radius: 4px;
    transition: all 0.2s;
    display: flex;
}

.close-btn:hover {
    background-color: rgba(0,0,0,0.05);
    color: var(--color-text-main);
}

.settings-group {
    border-bottom: 1px solid var(--color-border);
}

.settings-group:last-child {
    border-bottom: none;
}

.group-title {
    padding: var(--spacing-md) var(--spacing-lg);
    margin: 0;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--color-text-main);
    transition: background 0.2s;
}

.group-title:hover {
    background-color: #f8fafc;
}

.toggle-icon {
    transition: transform 0.3s ease;
    color: var(--color-text-muted);
}

.toggle-icon.rotate-180 {
    transform: rotate(180deg);
}

.group-content {
    padding: 0 var(--spacing-lg) var(--spacing-md);
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-height: 500px; /* Large enough */
    transition: max-height 0.3s ease, padding 0.3s ease, opacity 0.3s ease;
    overflow: hidden;
    opacity: 1;
}

.group-content.collapsed {
    max-height: 0;
    padding-bottom: 0;
    opacity: 0;
}

.setting-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.setting-item span {
    font-size: 0.9rem;
    color: var(--color-text-muted);
}

.settings-footer {
    padding: var(--spacing-md) var(--spacing-lg);
    background-color: #fafafa;
    border-top: 1px solid var(--color-border);
    text-align: right;
}

/* Pause Overlay */
.pause-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 900;
}

.pause-overlay.hidden {
    display: none;
}

.pause-content {
    background: rgba(15, 23, 42, 0.95);
    color: white;
    padding: 40px;
    border-radius: var(--radius-lg);
    text-align: center;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    animation: scaleIn 0.3s ease;
}

@keyframes scaleIn {
    from { transform: scale(0.9); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.pause-content h2 {
    font-size: 1.5rem;
    margin-bottom: var(--spacing-md);
    font-weight: 700;
}

/* Switch */
.switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 22px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--color-secondary);
    transition: .3s;
    border-radius: 22px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: .3s;
    border-radius: 50%;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

input:checked + .slider {
    background-color: var(--color-primary);
}

input:checked + .slider:before {
    transform: translateX(18px);
}

/* Toast */
.toast-container {
    position: fixed;
    top: 24px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}

.toast {
    background: #000;
    color: white;
    padding: 10px 20px;
    border-radius: 99px;
    font-size: 0.9rem;
    font-weight: 500;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    animation: slideDown 0.3s ease;
}

@keyframes slideDown {
    from { transform: translateY(-20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

/* Responsive */
@media (max-width: 900px) {
    body {
        height: auto;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .app-container {
        height: auto;
        min-height: 100vh;
        overflow: visible;
        padding-bottom: 40px;
    }

    .game-main {
        flex-direction: column;
        align-items: center;
        gap: var(--spacing-lg);
        overflow: visible;
        flex: 0 0 auto; /* Don't force flex basis */
    }

    .game-sidebar {
        flex: 0 0 auto;
        width: 100%;
        max-width: 600px;
        flex-direction: row;
        flex-wrap: wrap;
        padding: 0;
        height: auto;
        overflow: visible;
        margin-bottom: 20px;
    }
    
    .card {
        flex: 1 1 250px;
    }

    .board-container {
        flex: 0 0 auto; /* Prevent compression */
        width: 100%;
        max-width: 500px; /* Limit max width on tablet */
        aspect-ratio: 1; /* Maintain square aspect ratio */
    }
}

@media (max-width: 600px) {
    :root {
        --spacing-lg: 16px;
        --spacing-xl: 20px;
    }

    .app-container {
        padding: var(--spacing-md);
    }

    .game-header {
        margin-bottom: var(--spacing-md);
    }

    .game-header h1 {
        font-size: 1.5rem;
    }

    .board-container {
        padding: 10px;
        width: 100%;
        max-width: 100%;
        border-radius: var(--radius-md);
    }

    /* Adjust sidebar for mobile */
    .game-sidebar {
        flex-direction: column;
        max-width: 100%;
        gap: var(--spacing-md);
    }

    .card {
        padding: var(--spacing-md);
        min-width: 0;
        flex: 0 0 auto; /* Don't stretch vertically */
    }

    .status-panel {
        grid-template-columns: repeat(4, 1fr);
        gap: 8px;
    }

    .status-item .label {
        font-size: 0.65rem;
    }

    .status-item .value {
        font-size: 1rem;
    }

    .control-panel {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }

    /* Make Difficulty full width or fit in grid */
    .difficulty-selector {
        grid-column: 1 / -1;
        justify-content: center;
        background: var(--color-background);
        padding: 8px;
        border-radius: var(--radius-sm);
    }

    /* Make Settings/Back buttons compact */
    #btnSettings, #btnBackToMenu {
        grid-column: span 1;
    }
    
    /* Main Menu Mobile Adjustments */
    .menu-title {
        font-size: 2.2rem;
    }
    
    .menu-subtitle {
        font-size: 0.9rem;
    }
    
    .menu-buttons {
        width: 100%;
        max-width: 280px;
    }
    
    /* Modal Mobile */
    .modal {
        width: 90%;
        max-height: 85vh;
        overflow-y: auto;
        padding: var(--spacing-md);
    }
    
    .group-content {
        padding: 0 var(--spacing-md) var(--spacing-sm);
    }
}

@media (max-width: 380px) {
    .status-panel {
        grid-template-columns: repeat(2, 1fr);
    }
}
