/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #1A535C;
    --secondary-color: #4ECDC4;
    --accent-color: #F7FFF7;
    --warning-color: #FF6B6B;
    --success-color: #FFE66D;
    --gradient-bg: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
    --card-shadow: 0 4px 15px rgba(26, 83, 92, 0.2);
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    --border-radius: 12px;
    --font-family: 'Poppins', sans-serif;
    --z-modal: 1000;
    --z-loading: 9999;
    --z-toast: 10000;
}

body {
    font-family: var(--font-family);
    background: var(--gradient-bg);
    min-height: 100vh;
    color: var(--primary-color);
    overflow-x: hidden;
    position: relative;
    line-height: 1.6;
}

body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: 
        radial-gradient(circle at 20% 50%, rgba(78, 205, 196, 0.1) 0%, transparent 50%),
        radial-gradient(circle at 80% 20%, rgba(255, 230, 109, 0.1) 0%, transparent 50%),
        radial-gradient(circle at 40% 80%, rgba(255, 107, 107, 0.1) 0%, transparent 50%);
    pointer-events: none;
    z-index: -1;
}

.app-container {
    min-height: calc(100vh - 60px);
    padding: 20px;
    max-width: 1200px;
    margin: 0 auto;
}

/* Header Styles */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    padding: 20px;
    background: rgba(247, 255, 247, 0.95);
    backdrop-filter: blur(10px);
    border-radius: var(--border-radius);
    box-shadow: var(--card-shadow);
    position: relative;
}

.game-title {
    font-size: clamp(1.8rem, 4vw, 2.5rem);
    font-weight: 700;
    color: var(--primary-color);
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
    margin: 0;
}

.header-controls {
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
}

/* Button Styles */
.btn {
    padding: 12px 24px;
    border: none;
    border-radius: var(--border-radius);
    font-family: var(--font-family);
    font-weight: 500;
    font-size: 1rem;
    cursor: pointer;
    transition: var(--transition);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    text-decoration: none;
    position: relative;
    overflow: hidden;
    min-height: 44px; /* Improved touch target */
    white-space: nowrap;
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none !important;
}

.btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
    transition: left 0.5s;
}

.btn:hover:not(:disabled)::before {
    left: 100%;
}

.btn-primary {
    background: var(--primary-color);
    color: var(--accent-color);
    box-shadow: 0 4px 15px rgba(26, 83, 92, 0.3);
}

.btn-primary:hover:not(:disabled) {
    background: #0f3c42;
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(26, 83, 92, 0.4);
}

.btn-secondary {
    background: var(--secondary-color);
    color: var(--primary-color);
    box-shadow: 0 4px 15px rgba(78, 205, 196, 0.3);
}

.btn-secondary:hover:not(:disabled) {
    background: #3db8b0;
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(78, 205, 196, 0.4);
}

.btn-danger {
    background: var(--warning-color);
    color: var(--accent-color);
    box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
}

.btn-danger:hover:not(:disabled) {
    background: #ff5252;
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(255, 107, 107, 0.4);
}

/* Game Info Panel */
.game-info {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.info-card {
    background: rgba(247, 255, 247, 0.95);
    backdrop-filter: blur(10px);
    padding: 20px;
    border-radius: var(--border-radius);
    box-shadow: var(--card-shadow);
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: var(--transition);
    min-height: 80px;
}

.info-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(26, 83, 92, 0.25);
}

.info-label {
    font-weight: 500;
    color: var(--primary-color);
    opacity: 0.8;
    font-size: 0.9rem;
}

.info-value {
    font-weight: 700;
    font-size: 1.2rem;
    color: var(--primary-color);
}

.pvp-score {
    grid-column: 1 / -1;
    background: linear-gradient(135deg, var(--success-color), #ffd93d);
}

/* Game Board */
.game-container {
    display: flex;
    justify-content: center;
    margin-bottom: 30px;
}

.game-board {
    display: grid;
    gap: 15px;
    padding: 30px;
    background: rgba(247, 255, 247, 0.95);
    backdrop-filter: blur(10px);
    border-radius: var(--border-radius);
    box-shadow: var(--card-shadow);
    transition: var(--transition);
    width: 100%;
    max-width: 100%;
}

.game-board.difficulty-4x4 {
    grid-template-columns: repeat(4, 1fr);
    max-width: 500px;
}

.game-board.difficulty-6x6 {
    grid-template-columns: repeat(6, 1fr);
    max-width: 650px;
}

.game-board.difficulty-8x8 {
    grid-template-columns: repeat(8, 1fr);
    max-width: 800px;
}

.game-board.difficulty-10x10 {
    grid-template-columns: repeat(10, 1fr);
    max-width: 950px;
}

/* Card Styles */
.memory-card {
    aspect-ratio: 1;
    position: relative;
    cursor: pointer;
    perspective: 1000px;
    transition: var(--transition);
    border-radius: var(--border-radius);
    min-height: 60px;
}

.memory-card:hover:not(.matched):not(.flipped) {
    transform: scale(1.05);
}

.memory-card:focus {
    outline: 3px solid var(--success-color);
    outline-offset: 2px;
}

.memory-card.disabled {
    pointer-events: none;
    opacity: 0.7;
}

.card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    text-align: center;
    transition: transform 0.6s;
    transform-style: preserve-3d;
    border-radius: var(--border-radius);
}

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

.card-front,
.card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    border-radius: var(--border-radius);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(1.5rem, 3vw, 2rem);
    font-weight: 700;
    box-shadow: var(--card-shadow);
    transition: var(--transition);
}

.card-front {
    background: linear-gradient(135deg, var(--secondary-color), #3db8b0);
    color: var(--accent-color);
    transform: rotateY(180deg);
}

.card-back {
    background: linear-gradient(135deg, var(--primary-color), #0f3c42);
    color: var(--accent-color);
}

.card-back::before {
    content: '🧠';
    font-size: clamp(1.2rem, 2.5vw, 1.5rem);
    opacity: 0.8;
}

.memory-card.matched .card-front {
    background: linear-gradient(135deg, var(--success-color), #ffd93d);
    color: var(--primary-color);
    animation: matchPulse 0.6s ease-in-out;
}

@keyframes matchPulse {
    0%, 100% { transform: scale(1) rotateY(180deg); }
    50% { transform: scale(1.1) rotateY(180deg); }
}

.memory-card.wrong .card-inner {
    animation: wrongShake 0.5s ease-in-out;
}

@keyframes wrongShake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    75% { transform: translateX(5px); }
}

/* Modal Styles */
.modal {
    display: none;
    position: fixed;
    z-index: var(--z-modal);
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(5px);
    animation: fadeIn 0.3s ease-out;
    overflow-y: auto;
}

.modal.show {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background: var(--accent-color);
    margin: auto;
    padding: 0;
    border-radius: var(--border-radius);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    max-width: 600px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    animation: slideIn 0.3s ease-out;
    position: relative;
}

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

.modal-header {
    padding: 25px 30px;
    border-bottom: 2px solid var(--secondary-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: linear-gradient(135deg, var(--secondary-color), #3db8b0);
    color: var(--accent-color);
    border-radius: var(--border-radius) var(--border-radius) 0 0;
    position: sticky;
    top: 0;
    z-index: 1;
}

.modal-header h2 {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 600;
}

.close-btn {
    background: none;
    border: none;
    font-size: 2rem;
    cursor: pointer;
    color: var(--accent-color);
    transition: var(--transition);
    padding: 0;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
}

.close-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: rotate(90deg);
}

.modal-body {
    padding: 30px;
}

.modal-footer {
    padding: 20px 30px;
    border-top: 1px solid #e0e0e0;
    display: flex;
    justify-content: flex-end;
    gap: 15px;
    background: #f8f9fa;
    border-radius: 0 0 var(--border-radius) var(--border-radius);
    position: sticky;
    bottom: 0;
}

/* Settings Styles */
.settings-section {
    margin-bottom: 30px;
}

.settings-section h3 {
    margin-bottom: 15px;
    color: var(--primary-color);
    font-size: 1.2rem;
    font-weight: 600;
}

.difficulty-options,
.theme-options,
.gamemode-options {
    display: grid;
    gap: 12px;
}

.radio-option {
    display: flex;
    align-items: center;
    cursor: pointer;
    padding: 15px;
    border: 2px solid #e0e0e0;
    border-radius: var(--border-radius);
    transition: var(--transition);
    position: relative;
    min-height: 60px;
}

.radio-option:hover {
    border-color: var(--secondary-color);
    background: rgba(78, 205, 196, 0.05);
}

.radio-option:focus-within {
    border-color: var(--secondary-color);
    box-shadow: 0 0 0 3px rgba(78, 205, 196, 0.2);
}

.radio-option input[type="radio"] {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
}

.radio-custom {
    width: 20px;
    height: 20px;
    border: 2px solid #ccc;
    border-radius: 50%;
    margin-right: 15px;
    position: relative;
    transition: var(--transition);
    flex-shrink: 0;
}

.radio-option input[type="radio"]:checked + .radio-custom {
    border-color: var(--secondary-color);
    background: var(--secondary-color);
}

.radio-option input[type="radio"]:checked + .radio-custom::after {
    content: '';
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent-color);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.radio-option input[type="radio"]:checked ~ span {
    color: var(--primary-color);
    font-weight: 600;
}

/* Switch Styles */
.switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 34px;
}

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

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: var(--transition);
    border-radius: 34px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 26px;
    width: 26px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: var(--transition);
    border-radius: 50%;
}

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

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

input:focus + .slider {
    box-shadow: 0 0 0 3px rgba(78, 205, 196, 0.2);
}

/* Ranking Styles */
.ranking-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    border-bottom: 2px solid #e0e0e0;
    overflow-x: auto;
}

.tab-btn {
    padding: 12px 20px;
    border: none;
    background: none;
    cursor: pointer;
    font-family: var(--font-family);
    font-weight: 500;
    color: var(--primary-color);
    border-bottom: 3px solid transparent;
    transition: var(--transition);
    white-space: nowrap;
    min-height: 44px;
}

.tab-btn.active,
.tab-btn:hover {
    color: var(--secondary-color);
    border-bottom-color: var(--secondary-color);
}

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

.ranking-content {
    min-height: 300px;
}

.ranking-list {
    list-style: none;
}

.ranking-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    margin-bottom: 10px;
    background: linear-gradient(135deg, #f8f9fa, #e9ecef);
    border-radius: var(--border-radius);
    transition: var(--transition);
}

.ranking-item:hover {
    transform: translateX(5px);
    box-shadow: var(--card-shadow);
}

.ranking-item.top-3 {
    background: linear-gradient(135deg, var(--success-color), #ffd93d);
    font-weight: 600;
}

.ranking-position {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--primary-color);
    min-width: 40px;
    flex-shrink: 0;
}

.ranking-info {
    flex: 1;
    margin-left: 15px;
    min-width: 0; /* Allow text truncation */
}

.ranking-name {
    font-weight: 600;
    color: var(--primary-color);
    word-break: break-word;
}

.ranking-stats {
    font-size: 0.9rem;
    color: var(--primary-color);
    opacity: 0.7;
    word-break: break-word;
}

.ranking-actions {
    margin-top: 20px;
    text-align: center;
}

.empty-ranking {
    text-align: center;
    padding: 40px;
    color: var(--primary-color);
    opacity: 0.6;
}

/* Game Stats */
.game-stats {
    margin-bottom: 20px;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid #e0e0e0;
}

.stat-item:last-child {
    border-bottom: none;
}

.stat-label {
    font-weight: 500;
    color: var(--primary-color);
}

.stat-value {
    font-weight: 700;
    color: var(--secondary-color);
}

.name-input {
    margin-top: 20px;
}

.name-input label {
    display: block;
    margin-bottom: 10px;
    font-weight: 500;
    color: var(--primary-color);
}

.name-input input {
    width: 100%;
    padding: 12px;
    border: 2px solid #e0e0e0;
    border-radius: var(--border-radius);
    font-family: var(--font-family);
    font-size: 1rem;
    transition: var(--transition);
}

.name-input input:focus {
    outline: none;
    border-color: var(--secondary-color);
    box-shadow: 0 0 0 3px rgba(78, 205, 196, 0.2);
}

.name-input small {
    display: block;
    margin-top: 5px;
    font-size: 0.8rem;
    color: var(--primary-color);
    opacity: 0.7;
}

/* Loading Screen */
.loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--gradient-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: var(--z-loading);
    transition: opacity 0.5s ease-out;
}

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

.loading-content {
    text-align: center;
    color: var(--accent-color);
}

.loading-spinner {
    width: 60px;
    height: 60px;
    border: 4px solid rgba(247, 255, 247, 0.3);
    border-top: 4px solid var(--accent-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 20px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.loading-content p {
    font-size: 1.2rem;
    font-weight: 500;
}

/* Error Toast */
.error-toast {
    position: fixed;
    top: 20px;
    right: 20px;
    background: var(--warning-color);
    color: var(--accent-color);
    padding: 15px 20px;
    border-radius: var(--border-radius);
    box-shadow: var(--card-shadow);
    display: none;
    align-items: center;
    gap: 15px;
    z-index: var(--z-toast);
    max-width: 400px;
    animation: slideInRight 0.3s ease-out;
}

.error-toast.show {
    display: flex;
}

@keyframes slideInRight {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

.error-message {
    flex: 1;
    font-weight: 500;
}

.error-close {
    background: none;
    border: none;
    color: var(--accent-color);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: var(--transition);
}

.error-close:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* Footer */
.footer {
    background: rgba(26, 83, 92, 0.9);
    color: var(--accent-color);
    text-align: center;
    padding: 20px;
    margin-top: auto;
    backdrop-filter: blur(10px);
}

.footer p {
    margin: 0;
    font-size: 0.9rem;
}

.footer a {
    color: var(--secondary-color);
    text-decoration: none;
    transition: var(--transition);
}

.footer a:hover {
    color: var(--success-color);
    text-decoration: underline;
}

/* Responsive Design */
@media (max-width: 768px) {
    .app-container {
        padding: 15px;
    }

    .header {
        flex-direction: column;
        gap: 20px;
        text-align: center;
    }

    .header-controls {
        flex-wrap: wrap;
        justify-content: center;
        width: 100%;
    }

    .btn {
        padding: 10px 16px;
        font-size: 0.9rem;
        flex: 1;
        min-width: 120px;
    }

    .game-info {
        grid-template-columns: 1fr 1fr;
        gap: 15px;
    }

    .info-card {
        padding: 15px;
        flex-direction: column;
        gap: 5px;
        text-align: center;
        min-height: auto;
    }

    .info-label {
        font-size: 0.8rem;
    }

    .info-value {
        font-size: 1.1rem;
    }

    .game-board {
        padding: 20px;
        gap: 10px;
    }

    .game-board.difficulty-4x4,
    .game-board.difficulty-6x6,
    .game-board.difficulty-8x8,
    .game-board.difficulty-10x10 {
        max-width: 100%;
    }

    .game-board.difficulty-10x10 {
        gap: 5px;
    }

    .modal-content {
        margin: 10px;
        max-height: 95vh;
        width: calc(100% - 20px);
    }

    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 20px;
    }

    .ranking-tabs {
        flex-wrap: wrap;
    }

    .tab-btn {
        flex: 1;
        min-width: 100px;
    }

    .error-toast {
        top: 10px;
        right: 10px;
        left: 10px;
        max-width: none;
    }
}

@media (max-width: 480px) {
    .btn {
        padding: 8px 12px;
        font-size: 0.8rem;
        min-width: 100px;
    }

    .game-info {
        grid-template-columns: 1fr;
    }

    .game-board.difficulty-10x10 .card-front,
    .game-board.difficulty-10x10 .card-back {
        font-size: 1rem;
    }

    .modal-header h2 {
        font-size: 1.2rem;
    }

    .radio-option {
        padding: 12px;
        font-size: 0.9rem;
    }

    .ranking-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .ranking-position {
        align-self: center;
    }

    .ranking-info {
        margin-left: 0;
        text-align: center;
        width: 100%;
    }
}

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

.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;
}

/* Focus styles for accessibility */
.btn:focus,
.memory-card:focus,
input:focus,
.close-btn:focus,
.tab-btn:focus {
    outline: 3px solid var(--success-color);
    outline-offset: 2px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    :root {
        --primary-color: #000000;
        --secondary-color: #0066cc;
        --accent-color: #ffffff;
        --warning-color: #cc0000;
        --success-color: #006600;
    }
}

/* Print styles */
@media print {
    .header-controls,
    .modal,
    .loading-screen,
    .error-toast {
        display: none !important;
    }
    
    .app-container {
        max-width: none;
        padding: 0;
    }
    
    .game-board {
        break-inside: avoid;
    }
}
