:root {
    /* Professional Palette */
    --primary: #4338ca;       
    --primary-hover: #3730a3; 
    --bg-app: #f1f5f9;        
    --bg-sidebar: #0f172a;    
    --bg-card: #ffffff;
    
    --text-main: #1e293b;     
    --text-body: #334155;     
    --text-muted: #64748b;    
    
    --border: #e2e8f0;        
    
    --success: #166534;       
    --success-bg: #dcfce7;    
    --success-border: #86efac;
    
    --error: #991b1b;         
    --error-bg: #fee2e2;      
    --error-border: #fca5a5;

    --font-ui: 'Inter', sans-serif;
    --font-read: 'Merriweather', serif;
    
    --sidebar-width: 280px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: var(--font-ui);
    background-color: var(--bg-app);
    color: var(--text-main);
    height: 100vh;
    overflow: hidden; /* Prevent body scroll, internal divs scroll */
}

/* --- 1. LEVEL SELECTION OVERLAY --- */
.level-overlay {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: linear-gradient(135deg, #1e1b4b 0%, #312e81 100%);
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.level-card {
    background: white;
    padding: 3rem;
    border-radius: 16px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    max-width: 500px;
    width: 100%;
    text-align: center;
}

.logo-large {
    font-size: 2rem;
    font-weight: 800;
    color: var(--primary);
    margin-bottom: 0.5rem;
}

.level-subtitle { color: var(--text-muted); margin-bottom: 2.5rem; }

.level-grid { display: flex; flex-direction: column; gap: 16px; }

.level-btn {
    display: flex;
    align-items: center;
    padding: 16px 20px;
    background: #f8fafc;
    border: 2px solid var(--border);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s;
    text-align: left;
}

.level-btn:hover {
    border-color: var(--primary);
    background: #eef2ff;
    transform: translateY(-2px);
}

.lvl-icon { font-size: 1.5rem; margin-right: 16px; }
.lvl-name { font-weight: 600; font-size: 1.1rem; color: var(--text-main); flex: 1; }
.lvl-desc { font-size: 0.85rem; color: var(--text-muted); font-weight: 500; background: white; padding: 4px 8px; border-radius: 6px; }

/* --- 2. LAYOUT --- */
.app-container {
    display: flex; /* Changed from Grid to Flex for better Mobile control */
    height: 100vh;
    position: relative;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: var(--bg-sidebar);
    color: #f8fafc;
    display: flex;
    flex-direction: column;
    border-right: 1px solid #1e293b;
    flex-shrink: 0;
    transition: margin-left 0.3s ease;
}

/* Desktop Collapse Logic */
.app-container.collapsed .sidebar {
    margin-left: calc(var(--sidebar-width) * -1);
}

.sidebar-header {
    padding: 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-content {
    padding: 0 1.5rem 1.5rem 1.5rem;
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow-y: hidden;
}

.logo {
    font-size: 1.25rem;
    font-weight: 700;
    color: #818cf8;
}

#close-sidebar-mobile { display: none; background: none; border: none; color: white; font-size: 1.2rem; cursor: pointer; }

.nav-label {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: #475569;
    margin-bottom: 0.75rem;
    margin-top: 1rem;
    font-weight: 600;
}

#lesson-list { 
    list-style: none; 
    padding: 0; 
    margin: 0;
    overflow-y: auto;
    flex: 1;
}

/* Scrollbar styles */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
.sidebar ::-webkit-scrollbar-thumb { background: #334155; }

.lesson-item {
    padding: 12px 16px;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 6px;
    font-size: 0.95rem;
    color: #cbd5e1;
    transition: all 0.2s ease;
}
.lesson-item:hover { background: #1e293b; color: white; }
.lesson-item.active { background: #1e293b; color: #818cf8; font-weight: 600; border-left: 3px solid #818cf8; }

.change-level-link {
    background: none; border: none; color: #94a3b8;
    cursor: pointer; padding: 12px 0; margin-top: auto;
    text-align: left; font-size: 0.9rem;
    border-top: 1px solid #1e293b;
    transition: color 0.2s;
}
.change-level-link:hover { color: white; }

/* Main Content */
.content-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg-app);
}

header {
    background: var(--bg-card);
    height: 64px;
    padding: 0 2rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

.header-left { display: flex; align-items: center; gap: 16px; }
h1 { margin: 0; font-size: 1.1rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 300px;}

.btn-icon {
    background: transparent; border: 1px solid var(--border);
    cursor: pointer; color: var(--text-muted); padding: 6px;
    border-radius: 6px; display: flex;
}
.btn-icon:hover { background: var(--bg-app); color: var(--text-main); }

.actions button {
    padding: 8px 16px; border: none; border-radius: 6px;
    font-size: 0.9rem; font-weight: 500; cursor: pointer;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; }

.btn-secondary { background: white; color: var(--text-muted); border: 1px solid var(--border) !important; margin-right: 8px; }

/* Split View */
.split-view {
    display: flex;
    flex: 1;
    overflow: hidden;
    position: relative;
}

.reading-panel, .exercise-panel {
    flex: 1;
    overflow-y: auto;
    padding: 2rem;
    -webkit-overflow-scrolling: touch; /* Smooth scroll on iOS */
}

.reading-panel { border-right: 1px solid var(--border); background-color: #f8fafc; }
.exercise-panel { background: var(--bg-app); }

.reading-paper {
    max-width: 700px; margin: 0 auto; background: white;
    padding: 3rem; border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

#reading-content { font-family: var(--font-read); font-size: 1.15rem; line-height: 1.9; color: var(--text-main); }
#reading-content p { margin-bottom: 1.5em; }

/* Exercise Styles */
.exercise-section {
    background: var(--bg-card); padding: 1.5rem;
    border-radius: 12px; margin-bottom: 1.5rem;
    border: 1px solid var(--border);
}

.section-title {
    font-size: 0.85rem; text-transform: uppercase; font-weight: 700;
    color: var(--primary); margin-bottom: 1rem;
    border-bottom: 1px solid #eef2ff; padding-bottom: 0.5rem;
}

.input-group { margin-bottom: 1rem; }
.input-group label { display: block; margin-bottom: 6px; font-size: 0.9rem; color: var(--text-body); }

input[type="text"] {
    border: 1px solid #cbd5e1; border-radius: 8px;
    padding: 10px; width: 100%; /* Full width for mobile friendly */
    font-family: var(--font-ui); font-size: 1rem; /* 16px prevents zoom on iOS */
    background: #f8fafc;
}
input[type="text"]:focus { border-color: var(--primary); outline: none; background: white; }

/* Inline inputs for Fill Blanks */
input[data-type="fill"] {
    width: 120px; display: inline-block; margin: 0 4px; text-align: center; padding: 6px;
}

input.correct { border-color: var(--success); background: var(--success-bg); color: var(--success); }
input.incorrect { border-color: var(--error); background: var(--error-bg); color: var(--error); }

.mcq-item { margin-bottom: 2rem; }
.mcq-question { font-weight: 600; margin-bottom: 0.75rem; display: block; }
.mcq-options { display: flex; flex-direction: column; gap: 8px; }
.mcq-options label {
    padding: 12px; border: 1px solid var(--border); border-radius: 8px;
    display: flex; align-items: center; cursor: pointer; background: white;
}
.mcq-options input { margin-right: 12px; transform: scale(1.2); }
.mcq-options label.correct-opt { background: var(--success-bg); border-color: var(--success); color: var(--success); }
.mcq-options label.wrong-opt { background: var(--error-bg); border-color: var(--error); color: var(--error); }

.score-card { background: white; border-top: 4px solid var(--primary); padding: 1.5rem; border-radius: 8px; text-align: center; margin-top: 2rem; }
.hidden { display: none; }

/* --- MOBILE SPECIFIC STYLES --- */
.mobile-tabs, .mobile-actions { display: none; }
.sidebar-backdrop { display: none; }

@media (max-width: 768px) {
    /* Sidebar as Overlay */
    .sidebar {
        position: fixed; top: 0; left: 0; height: 100%; z-index: 50;
        margin-left: -100%; /* Hidden by default */
        box-shadow: 5px 0 15px rgba(0,0,0,0.3);
    }
    .sidebar.open { margin-left: 0; }
    
    /* Backdrop */
    .sidebar-backdrop {
        position: fixed; top: 0; left: 0; width: 100%; height: 100%;
        background: rgba(0,0,0,0.5); z-index: 40;
        display: none; /* Toggled via JS */
    }
    .sidebar-backdrop.visible { display: block; }
    
    #close-sidebar-mobile { display: block; }

    /* Collapse logic reset for mobile */
    .app-container.collapsed .sidebar { margin-left: -100%; }

    /* Header Adjustments */
    header { padding: 0 1rem; height: 60px; }
    .desktop-actions { display: none; } /* Hide top buttons on mobile */

    /* Tab System for Reading vs Exercises */
    .mobile-tabs {
        display: flex; background: white; border-bottom: 1px solid var(--border);
    }
    .tab-btn {
        flex: 1; padding: 12px; border: none; background: none;
        font-weight: 600; color: var(--text-muted); border-bottom: 2px solid transparent;
    }
    .tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }

    /* Stack Panels and Toggle Visibility */
    .split-view { flex-direction: column; }
    
    .reading-panel, .exercise-panel {
        display: none; /* Hidden unless active */
        height: calc(100vh - 60px - 45px); /* Height - header - tabs */
        padding: 1rem;
    }
    .reading-panel.active-panel, .exercise-panel.active-panel { display: block; }

    .reading-paper { padding: 1.5rem; box-shadow: none; border: none; }
    
    /* Mobile Bottom Actions */
    .mobile-actions {
        display: flex; padding: 1rem 0; gap: 10px; margin-top: 2rem;
    }
    .mobile-actions button { flex: 1; padding: 12px; border-radius: 8px; border: none; font-weight: 600; }
    #check-btn-mobile { background: var(--primary); color: white; }
    #reset-btn-mobile { background: white; border: 1px solid #cbd5e1; }

    .mobile-only { display: block; }
    h1 { font-size: 1rem; }
}