:root {
    --bg-color: #f8fbff;
    --text-color: #2c3e50;
    --primary-color: #3498db;
    --card-bg: #ffffff;
    --card-border: #e2e8f0;
    --btn-text: #475569;
    --correct-bg: #d1fae5;
    --correct-border: #10b981;
    --correct-text: #065f46;
    --wrong-bg: #ffe4e6;
    --wrong-border: #f43f5e;
    --teacher-bg: #f1f5f9;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    overflow-x: hidden;
}

body.game-active {
    height: 100dvh;
    min-height: 100dvh;
    overflow: hidden; 
}

header {
    padding: clamp(8px, 1.5vh, 12px) 20px;
    background-color: #ffffff;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
    flex-shrink: 0;
}

header a {
    text-decoration: none;
    color: var(--primary-color);
    font-weight: 600;
    font-size: clamp(0.9rem, 2.5vw, 1rem);
}

.start-screen, .game-stage, .result-screen, .teacher-panel {
    display: none;
}

body[data-screen="start"] .start-screen { display: flex; }
body[data-screen="game"] .game-stage { display: flex; }
body[data-screen="result"] .result-screen { display: flex; }
body[data-screen="teacher"] .teacher-panel { display: flex; }

.start-screen {
    flex: 1;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 15px;
    text-align: center;
    gap: clamp(10px, 3vh, 20px);
}

.start-screen h1 {
    font-size: clamp(2rem, 6vw, 3rem);
    color: var(--text-color);
    line-height: 1.1;
    margin: 0;
}

.start-screen p {
    font-size: clamp(1rem, 3vw, 1.25rem);
    max-width: 600px;
    color: #64748b;
    margin: 0;
}

.settings-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 100%;
    max-width: 450px;
    background: #fff;
    padding: 16px;
    border-radius: 16px;
    border: 2px solid var(--card-border);
    margin: 10px 0;
}

.settings-title {
    font-size: clamp(1rem, 3vw, 1.1rem);
    color: var(--text-color);
    font-weight: 700;
}

.settings-buttons {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: center;
}

.btn-setting {
    flex: 1;
    min-width: 80px;
    padding: 12px 8px;
    font-size: 0.95rem;
    font-weight: bold;
    color: var(--btn-text);
    background-color: #f8fafc;
    border: 2px solid #cbd5e1;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
    touch-action: manipulation;
}

.btn-setting.selected {
    border-color: var(--primary-color);
    background-color: #f0f9ff;
    color: var(--primary-color);
}

.btn-setting.selected::after {
    content: "✓";
    position: absolute;
    top: -8px;
    right: -8px;
    background: var(--primary-color);
    color: white;
    border-radius: 50%;
    width: 22px;
    height: 22px;
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
}

.btn-start, .btn-primary, .btn-outline, .btn-teacher {
    padding: 14px 40px;
    font-size: clamp(1.1rem, 4vw, 1.3rem);
    font-weight: bold;
    border-radius: 12px;
    cursor: pointer;
    transition: transform 0.2s, background-color 0.2s, opacity 0.2s;
    border: none;
    text-align: center;
    text-decoration: none;
    touch-action: manipulation;
}

.btn-start, .btn-primary {
    color: #fff;
    background-color: var(--primary-color);
    box-shadow: 0 4px 6px rgba(52, 152, 219, 0.2);
}

.btn-start:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.btn-teacher {
    background-color: #64748b;
    color: #fff;
    box-shadow: 0 4px 6px rgba(100, 116, 139, 0.2);
}

.btn-outline {
    color: var(--primary-color);
    background-color: transparent;
    border: 2px solid var(--primary-color);
}

.btn-outline.secondary-link {
    border-color: #94a3b8;
    color: #64748b;
}

.btn-start:not(:disabled):active, .btn-primary:active, .btn-outline:active, .btn-teacher:active {
    transform: scale(0.96);
}

.game-stage {
    flex: 1;
    flex-direction: column;
    align-items: center;
    justify-content: center; 
    width: 100%;
    max-width: 900px; 
    margin: 0 auto;
    padding: clamp(5px, 2vh, 15px);
    min-height: 0;
    gap: clamp(8px, 2vh, 16px); 
}

.instruction {
    font-size: clamp(1.1rem, 4vw, 1.4rem);
    font-weight: 700;
    text-align: center;
    flex-shrink: 0;
    margin: 0;
    color: var(--text-color);
    line-height: 1.1;
}

.memory-grid-container {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1;
    min-height: 0;
}

.memory-grid {
    display: grid;
    gap: clamp(6px, 2vw, 12px);
    width: 100%;
    justify-content: center;
    align-content: center;
}

.grid-4 { grid-template-columns: repeat(2, 1fr); max-width: 280px; }
.grid-6 { grid-template-columns: repeat(3, 1fr); max-width: 380px; }
.grid-8 { grid-template-columns: repeat(4, 1fr); max-width: 450px; }
.grid-12 { grid-template-columns: repeat(4, 1fr); max-width: 500px; }

@media (max-width: 360px) {
    .grid-6 { grid-template-columns: repeat(2, 1fr); max-width: 260px; }
}

.memory-card {
    width: 100%;
    aspect-ratio: 1 / 1;
    perspective: 1000px; 
    cursor: pointer;
    touch-action: manipulation;
    min-height: 0;
    background: transparent;
    border: none;
    padding: 0;
}

.memory-card:focus-visible {
    outline: 3px solid var(--primary-color);
    outline-offset: 4px;
    border-radius: 12px;
}

.card-inner {
    width: 100%;
    height: 100%;
    position: relative;
    transition: transform 0.4s cubic-bezier(0.4, 0.2, 0.2, 1);
    transform-style: preserve-3d;
}

.memory-card.flipped .card-inner,
.memory-card.matched .card-inner,
.memory-card.wrong .card-inner {
    transform: rotateY(180deg);
}

@media (prefers-reduced-motion: reduce) {
    .card-inner { transition: none; }
}

.card-front, .card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    border-radius: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 3px solid var(--card-border); 
    transition: border-color 0.2s, background-color 0.2s;
}

.card-front {
    background-color: #f1f5f9; 
    font-size: clamp(2rem, 5vw, 3rem);
    color: #cbd5e1;
    font-weight: bold;
}

.card-back {
    background-color: var(--card-bg);
    transform: rotateY(180deg); 
    padding: clamp(6px, 1.5vw, 12px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.05);
    position: relative;
}

.card-back img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
}

.memory-card.matched .card-back {
    border-color: var(--correct-border);
    background-color: var(--correct-bg);
}

.memory-card.matched .card-back::after {
    content: "✓";
    position: absolute;
    top: 4px;
    right: 6px;
    color: var(--correct-text);
    font-size: 1rem;
    font-weight: bold;
}

.memory-card.wrong .card-back {
    border-color: var(--wrong-border);
    background-color: var(--wrong-bg);
}

.memory-card.wrong .card-back::after {
    content: "✕";
    position: absolute;
    top: 4px;
    right: 6px;
    color: var(--wrong-border);
    font-size: 1rem;
    font-weight: bold;
}

.feedback-area {
    min-height: clamp(24px, 4vh, 32px); 
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(1rem, 4vw, 1.25rem);
    font-weight: 700;
    flex-shrink: 0;
    text-align: center;
    line-height: 1.2;
}

.feedback-area.correct-text { color: var(--correct-border); }
.feedback-area.wrong-text { color: var(--wrong-border); }

.result-screen {
    flex: 1;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    text-align: center;
    gap: 15px;
}

.result-screen h1 {
    font-size: clamp(2.5rem, 6vw, 3.5rem);
    color: var(--correct-border);
    margin: 0;
}

.result-screen p {
    font-size: clamp(1.2rem, 4vw, 1.5rem);
    color: var(--text-color);
    font-weight: bold;
    margin: 0;
}

.result-screen .sub-text {
    font-size: clamp(1rem, 3vw, 1.2rem);
    color: #64748b;
    font-weight: normal;
    margin-bottom: 20px;
}

.result-actions {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 100%;
    max-width: 400px;
}

.teacher-panel {
    flex: 1;
    flex-direction: column;
    align-items: center;
    padding: 20px;
    background-color: var(--teacher-bg);
    overflow-y: auto;
    gap: 20px;
}

.teacher-container {
    width: 100%;
    max-width: 800px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.teacher-header {
    text-align: center;
    margin-bottom: 10px;
}
.teacher-header h2 { font-size: 1.8rem; color: var(--text-color); margin: 0; }
.teacher-header p { color: #64748b; font-size: 1rem; font-weight: 600; margin-top: 6px; margin-bottom: 0;}

.teacher-section-title {
    font-size: 1.2rem;
    color: var(--text-color);
    border-bottom: 2px solid #e2e8f0;
    padding-bottom: 8px;
    margin-bottom: 12px;
}

.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 12px; }
.grid-1 { display: grid; grid-template-columns: 1fr; gap: 12px; }

.stat-card {
    background: #fff;
    padding: 16px;
    border-radius: 12px;
    border: 1px solid #e2e8f0;
    text-align: center;
    box-shadow: 0 2px 4px rgba(0,0,0,0.02);
}
.stat-title { font-size: 0.9rem; color: #64748b; font-weight: 600; margin-bottom: 8px; }
.stat-value { font-size: 1.5rem; font-weight: 800; color: var(--text-color); }
.stat-value.success { color: var(--correct-border); }

.object-card {
    background: #fff;
    padding: 12px 16px;
    border-radius: 12px;
    border: 1px solid #e2e8f0;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.obj-title { font-weight: bold; color: var(--text-color); }
.obj-status { font-size: 0.9rem; font-weight: 600; }
.obj-status.success { color: var(--correct-text); }
.obj-detail { font-size: 0.9rem; color: #64748b; }

.observation-box {
    background: #fff;
    border-left: 4px solid var(--primary-color);
    padding: 16px;
    border-radius: 0 12px 12px 0;
    font-size: 1rem;
    color: #334155;
    line-height: 1.5;
    box-shadow: 0 2px 4px rgba(0,0,0,0.02);
}

.btn-back-teacher {
    align-self: center;
    padding: 12px 32px;
    font-size: 1rem;
    font-weight: bold;
    color: var(--text-color);
    background-color: #e2e8f0;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    transition: background-color 0.2s;
    margin-top: 10px;
}
.btn-back-teacher:active { background-color: #cbd5e1; }

@media (max-height: 600px) {
    header { padding: 4px 15px; }
    .start-screen { padding: 10px; gap: 8px; }
    .start-screen h1 { font-size: 1.8rem; }
    .settings-group { padding: 8px; margin: 5px 0; }
    .btn-setting { padding: 8px; font-size: 0.9rem; min-width: 60px; }
    
    .game-stage { gap: 4px; padding: 4px 8px; }
    .memory-grid { gap: 4px; }
    .card-front, .card-back { border-width: 2px; border-radius: 8px; padding: 4px;}
    .feedback-area { min-height: 20px; font-size: 0.95rem; }
    
    .result-actions { gap: 8px; }
    .btn-start, .btn-primary, .btn-outline, .btn-teacher { padding: 10px 20px; font-size: 1rem; }
}