:root {
    --primary-color: #4361ee;
    --secondary-color: #3f37c9;
    --accent-color: #4895ef;
    --success-color: #4cc9f0;
    --warning-color: #f72585;
    --background-color: #f8f9fa;
    --card-bg: #ffffff;
    --text-color: #333;
}

body {
    font-family: 'sans-serif, Segoe UI', Tahoma, Geneva, Verdana;
    background-color: var(--background-color);
    /* color: var(--text-color); */
    margin: 0;
    padding: 20px;
}

.main-container {
    display: flex;
    max-width: 800px;
    margin: auto;
    align-items: center;
    flex-direction: column;
}

.quiz-container-wrapper {
    width: 100%;
    transition: width 0.3s ease-in-out;
}

.quiz-container-wrapper.expanded {
    width: 100%;
}

.container {
    width: 100%;
    background: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    text-align: center;
    box-sizing: border-box;
}

/* Modern Button Styles */
.button {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    background: white;
    color: var(--text-color);
    padding: 15px 20px;
    margin: 0; /* Margin handled by grid gap */
    border: 1px solid #e9ecef;
    border-radius: 16px;
    cursor: pointer;
    font-size: 1rem;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
    width: 100%; /* Fill grid cell */
    box-sizing: border-box;
    position: relative;
    overflow: hidden;
}

.button:hover {
    transform: translateY(-4px);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.button:active {
    transform: translateY(-1px);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

.button.secondary {
    background: #f8f9fa;
    color: #666;
    box-shadow: none;
    border: 1px solid #dee2e6;
    width: auto; /* Secondary buttons usually standalone */
}
.button.secondary:hover {
    background: #e9ecef;
    color: #333;
}

/* Primary Action Button (Gradient) */
.button.primary-gradient {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    border: none;
    box-shadow: 0 4px 15px rgba(67, 97, 238, 0.3);
}
.button.primary-gradient:hover {
    color: white;
    box-shadow: 0 8px 25px rgba(67, 97, 238, 0.5);
}

.hide {
    display: none !important;
}

.settings-container {
    background: #f1f3f5;
    padding: 15px;
    border-radius: 8px;
    margin-bottom: 20px;
    text-align: center;
}

select {
    padding: 8px 16px;
    border-radius: 20px;
    border: 1px solid #ced4da;
    font-size: 1rem;
    outline: none;
}

/* Quiz Area Styles */
#quiz-area {
    text-align: center;
}

.question {
    font-size: 1.8rem;
    margin: 30px 0;
    font-weight: bold;
    color: var(--text-color);
}

.user-input {
    width: 80%;
    padding: 12px 20px;
    font-size: 1.2rem;
    border: 2px solid #e9ecef;
    border-radius: 50px;
    margin-bottom: 20px;
    outline: none;
    transition: border-color 0.3s;
    text-align: center;
}

.user-input:focus {
    border-color: var(--primary-color);
}

.feedback {
    margin-top: 15px;
    font-weight: bold;
    font-size: 1.2rem;
    min-height: 1.8rem;
}

.feedback.correct {
    color: #2a9d8f;
}

.feedback.incorrect {
    color: var(--warning-color);
}

/* Specific Quiz Type Styles */
.assembly-area {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
    min-height: 60px;
    padding: 15px;
    border: 2px dashed #dee2e6;
    border-radius: 12px;
    margin: 15px 0;
}

.word-piece {
    background: white;
    border: 1px solid var(--accent-color);
    color: var(--primary-color);
    padding: 8px 16px;
    border-radius: 20px;
    cursor: pointer;
    font-weight: 500;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

/* Matching Game */
#matching-container {
    display: flex;
    justify-content: space-between;
    position: relative;
    margin: 20px 0;
    min-height: 400px;
}

.matching-col {
    display: flex;
    flex-direction: column;
    gap: 15px;
    width: 45%;
}

.match-item {
    background: white;
    border: 2px solid #e9ecef;
    padding: 15px;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s;
    min-height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-family: "Microsoft YaHei", "SimHei", sans-serif;
    font-size: 1.2rem;
}

.match-item.selected {
    border-color: var(--primary-color);
    background-color: #e7f1ff;
}

.match-item.matched {
    border-color: #2a9d8f;
    background-color: #e6fcf5;
    opacity: 0.6;
    cursor: default;
}

.match-item.correct {
    border-color: #28a745;
    background-color: #d4edda;
    cursor: default;
}

.match-item.incorrect {
    border-color: #dc3545;
    background-color: #f8d7da;
    animation: shake 0.5s;
}

#matching-svg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;
}

line.match-line {
    stroke: #28a745;
    stroke-width: 3;
    stroke-linecap: round;
}

@keyframes shake {
    0% {
        transform: translateX(0);
    }

    25% {
        transform: translateX(-5px);
    }

    50% {
        transform: translateX(5px);
    }

    75% {
        transform: translateX(-5px);
    }

    100% {
        transform: translateX(0);
    }
}

/* Memory Match */
/* Memory Match */
#memory-match-container {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 15px;
    max-width: 600px;
    margin: 0 auto;
    perspective: 1000px;
}

.memory-card {
    aspect-ratio: 1;
    background-color: transparent;
    cursor: pointer;
    position: relative;
    height: 120px; /* Fallback */
}

/* Unified 3D Flip Logic (Child Rotation) */
.memory-card-content,
.memory-card-back {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 12px; /* Rounded corners */
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    backface-visibility: hidden;
    transition: transform 0.6s cubic-bezier(0.4, 0.0, 0.2, 1);
    font-size: 1.2rem;
    padding: 10px;
    text-align: center;
    overflow: hidden;
    box-sizing: border-box;
}

/* BACK (Initial State: Visible at 0deg) */
.memory-card-back {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)); /* Beautified Gradient */
    color: white;
    transform: rotateY(0deg);
    z-index: 2;
    font-size: 2.5rem;
}

/* FRONT (Initial State: Hidden at 180deg) */
.memory-card-content {
    background: white;
    color: var(--text-color);
    border: 2px solid white; /* Clean border */
    transform: rotateY(180deg);
    z-index: 1;
    font-weight: bold;
}

/* FLIP STATE */
.memory-card.flipped .memory-card-back {
    transform: rotateY(-180deg);
}

.memory-card.flipped .memory-card-content {
    transform: rotateY(0deg);
}

/* MATCHED STATE */
.memory-card.matched {
    pointer-events: none;
}
.memory-card.matched .memory-card-content {
    background-color: #d1e7dd; /* Light green */
    color: #0f5132;
    border-color: #badbcc;
    box-shadow: none; /* Flatten */
    opacity: 0.8;
}
.memory-card.matched .memory-card-back {
    display: none; /* Ensure back is gone */
}

/* INTERACTIVE HOVER */
.memory-card:not(.flipped):not(.matched):hover .memory-card-back {
    transform: rotateY(0deg) scale(1.05); /* Pulse effect */
    box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}

/* Word Rain */
#word-rain-wrapper {
    position: relative;
    height: 600px;
    background: linear-gradient(to bottom, #1a1a2e, #16213e);
    border-radius: 12px;
    overflow: hidden;
    margin-bottom: 20px;
}

.raindrop {
    position: absolute;
    color: #fff;
    font-size: 1.2rem;
    font-weight: bold;
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.5);
}

/* Responsive Grid for Buttons */
.button-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 15px;
    margin-top: 15px;
}

@media (max-width: 600px) {
    .button-grid {
        grid-template-columns: repeat(2, 1fr); /* Force 2 columns on mobile */
    }
    
    #memory-match-container {
        grid-template-columns: repeat(3, 1fr);
    }
}

/* History Table Styles */
#history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    margin-top: 10px;
}

#history-table th,
#history-table td {
    padding: 8px;
    border: 1px solid #ddd;
    text-align: left;
}

#history-table th {
    background-color: #f8f8f8;
    color: #333;
}

#history-table tr.correct {
    background-color: #e9f7ef;
}

#history-table tr.incorrect {
    background-color: #fbe9eb;
}

#history-table .user-answer {
    color: #dc3545;
    font-style: italic;
}

.correct-answer-row {
    color: #28a745;
    margin-bottom: 4px;
}

.user-answer-row {
    color: #dc3545;
}

.history-container {
    width: 100%;
    margin-top: 20px;
    background: white;
    padding: 15px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.correct-text {
    color: #28a745;
    font-weight: bold;
}

/* Speaking Quiz */
#speaking-container {
    text-align: center;
    padding: 20px;
}

.speaking-controls {
    display: flex;
    justify-content: center;
    gap: 15px;
    margin: 20px 0;
}

.control-btn {
    background: white;
    border: 2px solid #e9ecef;
    border-radius: 50px;
    padding: 10px 20px;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 8px;
}

.control-btn:hover {
    background: #f8f9fa;
    transform: translateY(-2px);
}

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

#record-btn {
    border-color: #dc3545;
    color: white;
}

#record-btn.recording {
    background: #dc3545;
    color: white;
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(220, 53, 69, 0.4);
    }

    70% {
        box-shadow: 0 0 0 10px rgba(220, 53, 69, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(220, 53, 69, 0);
    }
}

#mic-visualizer-bar {
    width: 0%;
    height: 100%;
    background-color: #28a745;
    transition: width 0.1s;
}

/* Inline Dash Input for Completion Quiz */
.inline-input-dashed {
    font-family: Consolas, "Courier New", monospace !important;
    background-color: transparent;
    color: #333;
    outline: none;
    padding: 0;
    font-weight: 700;
    font-size: inherit;
    border: none;

    /* Dashed Underline Trick */
    /* 1ch black line, 0.5ch gap -> Total unit 1.5ch */
    letter-spacing: 0.5ch;
    background-image: linear-gradient(to right, #333 0%, #333 66%, transparent 66%);
    background-size: 1.5ch 3px;
    background-repeat: repeat-x;
    background-position: bottom left;

    text-align: left;
    margin: 0 5px;
}