:root {
    /* Light Theme Colors */
    --bg-color: #f0f2f5;
    --bg-secondary: #ffffff;
    --text-color: #333;
    --text-muted: #666;
    --primary-color: #360185;
    --secondary-color: #8F0177;
    --accent-color: #DE1A58;
    --highlight-color: #F4B342;
    --card-bg: #fff;
    --card-back-bg: linear-gradient(135deg, #360185, #8F0177);
    --card-border-radius: 8px;
    --gap-size: 1rem;
    --shadow-color: rgba(0, 0, 0, 0.1);
    --shadow-strong: rgba(0, 0, 0, 0.2);
    --border-color: rgba(0, 0, 0, 0.1);
}

/* Dark Theme Colors */
body.dark-mode {
    --bg-color: #0f0f1a;
    --bg-secondary: #1a1a2e;
    --text-color: #e4e4e7;
    --text-muted: #a1a1aa;
    --primary-color: #a855f7;
    --secondary-color: #ec4899;
    --accent-color: #f472b6;
    --highlight-color: #fbbf24;
    --card-bg: #27273a;
    --card-back-bg: linear-gradient(135deg, #6366f1, #a855f7);
    --shadow-color: rgba(0, 0, 0, 0.3);
    --shadow-strong: rgba(0, 0, 0, 0.5);
    --border-color: rgba(255, 255, 255, 0.1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 20px 0;
    transition: background-color 0.3s ease, color 0.3s ease;
}

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

/* Font Awesome Icon Spacing */
h1 i,
h2 i,
h3 i,
.btn i,
.label i {
    margin-right: 0.4em;
}

/* Section heading icons */
.matched-container h3 i {
    color: #22c55e;
}

.leaderboard-section h3 i {
    color: #fbbf24;
}

.modal-content h2 i {
    color: #fbbf24;
}

/* Skip Link for Keyboard Users */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--accent-color);
    color: white;
    padding: 8px 16px;
    z-index: 1000;
    text-decoration: none;
    border-radius: 0 0 4px 0;
    transition: top 0.3s;
}

.skip-link:focus {
    top: 0;
}

.container {
    width: 90%;
    max-width: 800px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

header {
    width: 100%;
    text-align: center;
}

.header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

h1 {
    color: var(--primary-color);
    font-size: 1.8rem;
}

/* Icon Button (Theme Toggle) */
.icon-btn {
    background: var(--bg-secondary);
    border: 2px solid var(--border-color);
    border-radius: 50%;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    color: var(--primary-color);
}

.icon-btn:hover {
    background: var(--primary-color);
    color: white;
    transform: rotate(15deg);
}

.icon-btn:focus {
    outline: 3px solid var(--accent-color);
    outline-offset: 2px;
}

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

.hidden {
    display: none !important;
}

.stats-bar {
    display: flex;
    justify-content: space-around;
    background: var(--bg-secondary);
    padding: 15px;
    border-radius: 12px;
    box-shadow: 0 2px 10px var(--shadow-color);
    width: 100%;
    transition: background-color 0.3s ease;
}

.stat-item {
    font-size: 1.1rem;
    font-weight: 500;
    color: var(--primary-color);
}

.label {
    color: var(--secondary-color);
    margin-right: 5px;
    font-weight: bold;
}

/* Game Board Grid */
.game-board {
    display: grid;
    grid-gap: var(--gap-size);
    width: 100%;
    grid-template-columns: repeat(var(--cols, 4), 1fr);
    perspective: 1000px;
}

/* Card Styles */
.card {
    background-color: transparent;
    aspect-ratio: 1;
    cursor: pointer;
    position: relative;
    transform-style: preserve-3d;
    transition: transform 0.6s cubic-bezier(0.4, 0.0, 0.2, 1);
}

/* Focus state for keyboard navigation */
.card:focus {
    outline: 3px solid var(--accent-color);
    outline-offset: 3px;
    border-radius: var(--card-border-radius);
}

.card:focus:not(:focus-visible) {
    outline: none;
}

.card:focus-visible {
    outline: 3px solid var(--accent-color);
    outline-offset: 3px;
}

.card.flipped {
    transform: rotateY(180deg);
}

.card.matched {
    transform: rotateY(180deg) scale(0.95);
    opacity: 1;
    cursor: default;
}

.card.matched .card-front {
    border: 3px solid var(--highlight-color);
    box-shadow: 0 0 10px var(--highlight-color);
}

.card-face {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    border-radius: var(--card-border-radius);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2.5rem;
    box-shadow: 0 4px 6px var(--shadow-color);
    user-select: none;
    transition: background-color 0.3s ease;
}

.card-front {
    background-color: var(--card-bg);
    transform: rotateY(180deg);
}

.card-back {
    background: var(--card-back-bg);
    color: var(--highlight-color);
    font-size: 1.5rem;
    border: 2px solid var(--primary-color);
}

/* Controls */
.controls {
    margin-top: 20px;
    display: flex;
    align-items: center;
    gap: 20px;
    flex-wrap: wrap;
    justify-content: center;
}

.sound-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--bg-secondary);
    padding: 8px 15px;
    border-radius: 6px;
    box-shadow: 0 2px 5px var(--shadow-color);
    transition: background-color 0.3s ease;
}

.volume-control {
    display: flex;
    align-items: center;
    gap: 8px;
}

.volume-icon {
    width: 24px;
    height: 24px;
    fill: var(--primary-color);
}

#volume-slider {
    width: 100px;
    accent-color: var(--accent-color);
    cursor: pointer;
    height: 4px;
}

.btn {
    padding: 12px 24px;
    background-color: var(--accent-color);
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s, box-shadow 0.2s;
    font-weight: bold;
}

.btn:hover {
    background-color: var(--secondary-color);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px var(--shadow-strong);
}

.btn:active {
    transform: translateY(0);
}

.btn:focus {
    outline: 3px solid var(--highlight-color);
    outline-offset: 2px;
}

.btn-secondary {
    background-color: transparent;
    color: var(--text-muted);
    border: 2px solid var(--border-color);
    padding: 8px 16px;
    font-size: 0.9rem;
}

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

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    opacity: 1;
    transition: opacity 0.3s;
}

.modal.hidden {
    opacity: 0;
    pointer-events: none;
}

.modal-content {
    background: var(--bg-secondary);
    padding: 30px;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 10px 25px var(--shadow-strong);
    max-width: 90%;
    transition: background-color 0.3s ease;
}

.modal-content h2 {
    margin-bottom: 15px;
    color: var(--primary-color);
}

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

/* Matched Cards Container */
.matched-container {
    width: 100%;
    margin-top: 20px;
    background: var(--bg-secondary);
    padding: 15px;
    border-radius: 12px;
    box-shadow: 0 2px 10px var(--shadow-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: background-color 0.3s ease;
}

.matched-container h3 {
    color: var(--primary-color);
    margin-bottom: 10px;
    font-size: 1.1rem;
}

.matched-deck {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    justify-content: center;
    width: 100%;
    min-height: 80px;
}

.matched-pair {
    position: relative;
    width: 60px;
    height: 60px;
}

.matched-pair .card {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    cursor: default;
    transform: rotateY(180deg);
    transition: transform 0.3s ease;
}

.matched-pair .card:nth-child(1) {
    transform: rotateY(180deg) translate(-2px, -2px);
    z-index: 1;
}

.matched-pair .card:nth-child(2) {
    transform: rotateY(180deg) translate(2px, 2px);
    z-index: 2;
    box-shadow: 2px 2px 5px var(--shadow-color);
}

.matched-pair .card-face {
    font-size: 1.5rem;
    border-radius: 4px;
}

.card-placeholder {
    visibility: hidden;
    aspect-ratio: 1;
}

/* Leaderboard Section */
.leaderboard-section {
    width: 100%;
    margin-top: 20px;
    background: var(--bg-secondary);
    padding: 20px;
    border-radius: 12px;
    box-shadow: 0 2px 10px var(--shadow-color);
    transition: background-color 0.3s ease;
}

.leaderboard-section h3 {
    color: var(--primary-color);
    margin-bottom: 15px;
    font-size: 1.2rem;
    text-align: center;
}

.leaderboard-list {
    list-style: none;
    padding: 0;
    margin: 0 0 15px 0;
}

.leaderboard-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: var(--bg-color);
    border-radius: 8px;
    margin-bottom: 8px;
    transition: transform 0.2s, background-color 0.3s ease;
}

.leaderboard-item:hover {
    transform: translateX(4px);
}

.leaderboard-item .rank {
    font-size: 1.5rem;
    min-width: 40px;
    text-align: center;
}

/* Medal rank colors */
.rank-gold {
    color: #FFD700;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.rank-silver {
    color: #C0C0C0;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.rank-bronze {
    color: #CD7F32;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.rank-number {
    font-weight: bold;
    color: var(--text-muted);
    font-size: 1rem;
}

.leaderboard-item .score-info {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.leaderboard-item .score-info strong {
    color: var(--primary-color);
    font-size: 1rem;
}

.leaderboard-item .details {
    color: var(--text-muted);
    font-size: 0.85rem;
    margin-top: 2px;
}

.leaderboard-item .date {
    color: var(--text-muted);
    font-size: 0.8rem;
}

.empty-message {
    text-align: center;
    color: var(--text-muted);
    padding: 20px;
    font-style: italic;
}

.leaderboard-section .btn-secondary {
    display: block;
    margin: 0 auto;
}

/* Responsive adjustments */
@media (max-width: 600px) {
    .game-board {
        grid-template-columns: repeat(4, 1fr);
    }
    
    .card-face {
        font-size: 1.8rem;
    }
    
    .stats-bar {
        font-size: 0.9rem;
        padding: 10px;
    }
    
    .header-top {
        flex-direction: row;
    }
    
    h1 {
        font-size: 1.4rem;
    }
    
    .leaderboard-item {
        padding: 10px;
        gap: 8px;
    }
    
    .leaderboard-item .rank {
        font-size: 1.2rem;
        min-width: 32px;
    }
    
    .leaderboard-item .score-info strong {
        font-size: 0.9rem;
    }
    
    .leaderboard-item .details {
        font-size: 0.75rem;
    }
}

/* Focus visible for all interactive elements */
button:focus-visible,
input:focus-visible,
a:focus-visible {
    outline: 3px solid var(--accent-color);
    outline-offset: 2px;
}

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

/* High contrast mode support */
@media (prefers-contrast: high) {
    .card:focus {
        outline-width: 4px;
    }
    
    .btn {
        border: 2px solid currentColor;
    }
}
