/* ===== CSS Variables & Reset ===== */
:root {
    --color-bg: #0a0e27;
    --color-bg-light: #1a1f3a;
    --color-surface: rgba(255, 255, 255, 0.05);
    --color-surface-hover: rgba(255, 255, 255, 0.1);
    --color-primary: #6366f1;
    --color-primary-dark: #4f46e5;
    --color-secondary: #ec4899;
    --color-success: #10b981;
    --color-error: #ef4444;
    --color-text: #e5e7eb;
    --color-text-dim: #9ca3af;
    --color-card-red: #dc2626;
    --color-card-black: #1f2937;
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.4);
    --border-radius: 12px;
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: linear-gradient(135deg, var(--color-bg) 0%, #1a1f3a 100%);
    color: var(--color-text);
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    overflow-x: hidden;
}

/* ===== Container ===== */
.container {
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
}

/* ===== Header ===== */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    padding: 1.5rem;
    background: var(--color-surface);
    backdrop-filter: blur(10px);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-sm);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.title {
    font-size: 2rem;
    font-weight: 700;
    background: linear-gradient(135deg, #6366f1 0%, #ec4899 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.title-emoji {
    font-size: 2.5rem;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
}

.header-buttons {
    display: flex;
    gap: 0.5rem;
}

.icon-btn {
    background: var(--color-surface);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--color-text);
    width: 48px;
    height: 48px;
    border-radius: 50%;
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover {
    background: var(--color-surface-hover);
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

#settingsBtn:hover svg {
    transform: rotate(90deg);
    transition: var(--transition);
}

/* ===== Game Area ===== */
.game-area {
    background: var(--color-surface);
    backdrop-filter: blur(10px);
    border-radius: var(--border-radius);
    padding: 2rem;
    box-shadow: var(--shadow-lg);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

/* ===== Stats Bar ===== */
.stats-bar {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 1rem;
    margin-bottom: 2rem;
}

.stat-item {
    background: var(--color-bg-light);
    padding: 1rem;
    border-radius: 8px;
    text-align: center;
    border: 1px solid rgba(255, 255, 255, 0.05);
    transition: var(--transition);
}

.stat-item:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.stat-label {
    display: block;
    font-size: 0.875rem;
    color: var(--color-text-dim);
    margin-bottom: 0.5rem;
}

.stat-value {
    display: block;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--color-primary);
}

.stat-correct {
    color: var(--color-success);
}

.stat-wrong {
    color: var(--color-error);
}

/* ===== Card Container ===== */
.card-container {
    min-height: 280px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 2rem;
    position: relative;
    perspective: 1000px;

    /* Collapsing behavior */
    max-height: 50px;
    /* Collapsed height */
    overflow: hidden;
    transition: max-height 0.4s ease-out;
    /* Smooth transition */
}

.card-container.is-playing {
    max-height: 280px;
    /* Expanded height when playing */
}

.card-placeholder {
    text-align: center;
    color: var(--color-text-dim);
    font-size: 1.125rem;
    position: absolute;
    /* Ensures it stays visible during collapse/expand */
    width: 100%;
    padding: 0 1rem;
}

.card {
    width: 180px;
    height: 250px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: absolute;
    animation: cardAppear 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
    border: 3px solid #f0f0f0;
}

@keyframes cardAppear {
    0% {
        opacity: 0;
        transform: scale(0.5) rotateY(180deg);
    }

    100% {
        opacity: 1;
        transform: scale(1) rotateY(0deg);
    }
}

.card-suit {
    font-size: 3rem;
    margin-bottom: 0.5rem;
}

.card-value {
    font-size: 2.5rem;
    font-weight: 700;
}

.card.red {
    color: var(--color-card-red);
}

.card.black {
    color: var(--color-card-black);
}

/* ===== Progress Bar ===== */
.progress-container {
    margin-bottom: 2rem;
    position: relative;
}

.progress-bar {
    width: 100%;
    height: 12px;
    background: var(--color-bg-light);
    border-radius: 6px;
    overflow: hidden;
    position: relative;
}

.progress-bar::after {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: var(--progress, 0%);
    background: linear-gradient(90deg, var(--color-primary) 0%, var(--color-secondary) 100%);
    transition: width 0.3s ease-out;
    border-radius: 6px;
}

.progress-text {
    text-align: center;
    margin-top: 0.5rem;
    font-size: 0.875rem;
    color: var(--color-text-dim);
    font-weight: 500;
}

/* ===== Answer Section ===== */
.answer-section {
    text-align: center;
}

.answer-section h2 {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
    color: var(--color-text);
}

.input-group {
    display: flex;
    gap: 1rem;
    margin-bottom: 1rem;
    justify-content: center;
}

.input-group input {
    flex: 0 1 200px;
    padding: 1rem;
    font-size: 1.125rem;
    background: var(--color-bg-light);
    border: 2px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    color: var(--color-text);
    transition: var(--transition);
    font-family: inherit;
}

.input-group input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

.feedback {
    margin-top: 1rem;
    padding: 1rem;
    border-radius: 8px;
    font-size: 1.125rem;
    font-weight: 600;
    min-height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.feedback.correct {
    background: rgba(16, 185, 129, 0.2);
    color: var(--color-success);
    border: 2px solid var(--color-success);
}

.feedback.wrong {
    background: rgba(239, 68, 68, 0.2);
    color: var(--color-error);
    border: 2px solid var(--color-error);
}

/* ===== Start Section ===== */
.start-section {
    text-align: center;
}

/* ===== Buttons ===== */
.btn {
    padding: 1rem 2rem;
    font-size: 1.125rem;
    font-weight: 600;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: var(--transition);
    font-family: inherit;
    position: relative;
    overflow: hidden;
}

.btn::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    transform: translate(-50%, -50%);
    transition: width 0.6s, height 0.6s;
}

.btn:hover::before {
    width: 300px;
    height: 300px;
}

.btn-primary {
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
    color: white;
    box-shadow: var(--shadow-md);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.btn-start {
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-secondary) 100%);
    color: white;
    padding: 1.5rem 3rem;
    font-size: 1.25rem;
    box-shadow: var(--shadow-md);
}

.btn-start:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow-lg);
}

.btn-secondary {
    background: var(--color-surface);
    color: var(--color-text);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.btn-secondary:hover {
    background: var(--color-surface-hover);
}

/* ===== Focus Styles for Keyboard Navigation ===== */
button:focus-visible,
input:focus-visible {
    outline: 3px solid var(--color-primary);
    outline-offset: 2px;
}

.btn:focus-visible {
    outline: 3px solid var(--color-secondary);
    outline-offset: 3px;
}

input[type="checkbox"]:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

input[type="range"]:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 4px;
    border-radius: 4px;
}


/* ===== Game Mode Switcher ===== */
.game-mode-switcher {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    background: var(--color-bg-light);
    padding: 0.5rem;
    border-radius: 8px;
    margin-bottom: 2rem;
}

.btn-mode {
    flex: 1;
    padding: 0.75rem 1rem;
    background: transparent;
    color: var(--color-text-dim);
    border: none;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
}

.btn-mode:hover {
    background: var(--color-surface);
    color: var(--color-text);
}

.btn-mode.active {
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-secondary) 100%);
    color: white;
    box-shadow: var(--shadow-sm);
}

/* ===== Memory Answer Section ===== */
.memory-answer-section {
    text-align: center;
}

.memory-answer-section h2 {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
    color: var(--color-text);
}

.memory-input-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.memory-input-item {
    background: var(--color-bg-light);
    padding: 1rem;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.memory-input-item label {
    display: block;
    font-weight: 600;
    margin-bottom: 0.75rem;
}

.memory-input-item input {
    width: 100%;
    padding: 0.75rem;
    font-size: 1.25rem;
    text-align: center;
    background: var(--color-surface);
    border: 2px solid transparent;
    border-radius: 6px;
    color: var(--color-text);
    font-family: inherit;
    transition: var(--transition);
}

.memory-input-item input:focus {
    outline: none;
    border-color: var(--color-primary);
}

.memory-submit-container {
    margin-bottom: 1rem;
}


/* ===== Modal ===== */
.modal {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(5px);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.3s ease-out;
}

.modal.active {
    display: flex;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

.modal-content {
    background: var(--color-bg-light);
    border-radius: var(--border-radius);
    width: 90%;
    max-width: 500px;
    box-shadow: var(--shadow-lg);
    border: 1px solid rgba(255, 255, 255, 0.1);
    animation: slideUp 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes slideUp {
    from {
        transform: translateY(50px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.5rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.modal-header h2 {
    font-size: 1.5rem;
}

.close-btn {
    background: none;
    border: none;
    color: var(--color-text);
    font-size: 2rem;
    cursor: pointer;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: var(--transition);
}

.close-btn:hover {
    background: var(--color-surface);
    transform: rotate(90deg);
}

.modal-body {
    padding: 2rem;
}

.modal-footer {
    padding: 1.5rem;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    text-align: right;
}

/* ===== Settings ===== */
.setting-group {
    margin-bottom: 2rem;
}

.setting-group label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
}

.setting-label {
    font-weight: 500;
    color: var(--color-text);
}

.setting-value {
    font-weight: 700;
    color: var(--color-primary);
    background: var(--color-surface);
    padding: 0.25rem 0.75rem;
    border-radius: 6px;
}

input[type="range"] {
    width: 100%;
    height: 8px;
    background: var(--color-surface);
    border-radius: 4px;
    outline: none;
    -webkit-appearance: none;
    margin-bottom: 0.5rem;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-secondary) 100%);
    cursor: pointer;
    border-radius: 50%;
    box-shadow: var(--shadow-sm);
    transition: var(--transition);
}

input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.2);
    box-shadow: var(--shadow-md);
}

input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-secondary) 100%);
    cursor: pointer;
    border-radius: 50%;
    border: none;
    box-shadow: var(--shadow-sm);
    transition: var(--transition);
}

input[type="range"]::-moz-range-thumb:hover {
    transform: scale(1.2);
    box-shadow: var(--shadow-md);
}

.range-labels {
    display: flex;
    justify-content: space-between;
    font-size: 0.875rem;
    color: var(--color-text-dim);
}

/* ===== Card Selection in Modal ===== */
.card-selection-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    /* Wider items */
    gap: 0.75rem;
    max-height: 220px;
    overflow-y: auto;
    padding: 1rem;
    background: var(--color-bg);
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.card-selection-item {
    background: var(--color-surface);
    padding: 0.75rem;
    border-radius: 6px;
    border: 1px solid transparent;
    transition: var(--transition);
}

.card-selection-item:hover {
    background: var(--color-surface-hover);
    border-color: var(--color-primary);
}

.card-selection-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.card-selection-value {
    font-weight: 700;
    color: var(--color-text-dim);
}

.card-selection-controls {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.card-control {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
}

.card-control input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--color-primary);
    cursor: pointer;
}

.card-control label {
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
}

.card-control.disabled label {
    color: var(--color-text-dim);
    cursor: not-allowed;
}


/* ===== Responsive Design ===== */
@media (max-width: 900px) {
    body {
        padding: 10px;
        align-items: flex-start;
    }
}

@media (max-width: 768px) {
    .header {
        margin-bottom: 1.5rem;
        padding: 1rem;
    }

    .title {
        font-size: 1.5rem;
    }

    .title-emoji {
        font-size: 2rem;
    }

    .icon-btn {
        width: 44px;
        height: 44px;
    }

    .game-area {
        padding: 1rem;
    }

    .stats-bar {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.75rem;
    }

    .stat-item {
        padding: 0.75rem;
    }

    .stat-value {
        font-size: 1.25rem;
    }

    .card {
        width: 150px;
        height: 210px;
    }

    .card-container.is-playing {
        max-height: 240px;
        /* corresponds to card height + buffer */
    }

    .card-suit {
        font-size: 2.5rem;
    }

    .card-value {
        font-size: 2rem;
    }

    .input-group {
        flex-direction: column;
    }

    .input-group input {
        flex: 1;
        width: 100%;
    }

    .feedback {
        font-size: 1rem;
        min-height: 50px;
        padding: 0.75rem;
    }
}

@media (max-width: 480px) {
    body {
        padding: 5px;
    }

    .header {
        margin-bottom: 1rem;
    }

    .title {
        font-size: 1.2rem;
    }

    .title-emoji {
        font-size: 1.8rem;
    }

    .game-area {
        padding: 0.75rem;
    }

    .card-container {
        min-height: 190px;
        /* Placeholder area still takes this much if content dictates */
        margin-bottom: 1.5rem;
    }

    .card-container.is-playing {
        max-height: 198px;
        /* corresponds to card height + buffer */
    }

    .card {
        width: 120px;
        height: 168px;
    }


    .card-suit {
        font-size: 1.8rem;
    }

    .card-value {
        font-size: 1.5rem;
    }

    .btn-start {
        padding: 1rem 1.5rem;
        font-size: 1rem;
    }

    .answer-section h2,
    .memory-answer-section h2 {
        font-size: 1.1rem;
        margin-bottom: 1rem;
    }

    .input-group input {
        font-size: 1rem;
        padding: 0.8rem;
    }

    .btn {
        padding: 0.8rem 1.5rem;
        font-size: 0.9rem;
    }

    .modal-content {
        width: calc(100% - 10px);
        margin: 5px;
        max-height: 95vh;
        display: flex;
        flex-direction: column;
    }

    .modal-body {
        padding: 1rem;
        overflow-y: auto;
    }

    .modal-header,
    .modal-footer {
        padding: 1rem;
    }

    .modal-header h2 {
        font-size: 1.25rem;
    }

    .range-labels {
        font-size: 0.75rem;
    }

    .range-labels .long-text {
        display: none;
    }

    .card-selection-list {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        max-height: 180px;
    }

    .memory-input-list {
        grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
        gap: 0.75rem;
    }
}

@media (max-width: 360px) {
    .stats-bar {
        grid-template-columns: 1fr 1fr;
    }

    .stat-label {
        font-size: 0.7rem;
    }

    .stat-value {
        font-size: 1rem;
    }

    .game-mode-switcher {
        padding: 0.4rem;
        margin-bottom: 1.5rem;
    }

    .btn-mode {
        font-size: 0.8rem;
        padding: 0.6rem 0.5rem;
    }
}