/* ===== CSS Variables ===== */
:root {
    --primary: #4361ee;
    --primary-dark: #3451d1;
    --primary-light: #eef0fd;
    --secondary: #7209b7;
    --success: #2ecc71;
    --warning: #f39c12;
    --danger: #e74c3c;
    --bg: #f0f2f8;
    --surface: #ffffff;
    --surface2: #f8f9fe;
    --border: #e2e8f0;
    --text: #1a202c;
    --text-muted: #64748b;
    --sidebar-bg: #1e1b4b;
    --sidebar-text: #c7d2fe;
    --sidebar-active: #4361ee;
    --sidebar-hover: rgba(255,255,255,0.08);
    --shadow: 0 4px 20px rgba(0,0,0,0.08);
    --shadow-lg: 0 10px 40px rgba(0,0,0,0.12);
    --radius: 12px;
    --radius-sm: 8px;
    --transition: all 0.25s ease;
}

[data-theme="dark"] {
    --bg: #0f0e1a;
    --surface: #1a1829;
    --surface2: #221f35;
    --border: #2d2b45;
    --text: #e2e8f0;
    --text-muted: #94a3b8;
    --primary-light: #1e1b4b;
    --shadow: 0 4px 20px rgba(0,0,0,0.4);
    --shadow-lg: 0 10px 40px rgba(0,0,0,0.5);
}

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; }
body { font-family: 'Segoe UI', system-ui, -apple-system, sans-serif; background: var(--bg); color: var(--text); transition: var(--transition); min-height: 100vh; }
a { text-decoration: none; color: inherit; }
button { cursor: pointer; border: none; background: none; font-family: inherit; }
input, select, textarea { font-family: inherit; }

/* ===== Layout ===== */
.app-layout { display: flex; min-height: 100vh; }

/* ===== Sidebar ===== */
.sidebar {
    width: 240px; min-width: 240px; background: var(--sidebar-bg);
    display: flex; flex-direction: column; position: fixed; top: 0; left: 0;
    height: 100vh; z-index: 100; transition: var(--transition); overflow-y: auto;
}
.sidebar-brand {
    display: flex; align-items: center; gap: 10px; padding: 24px 20px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    position: relative;
}
.sidebar-close {
    display: none; position: absolute; top: 50%; right: 14px; transform: translateY(-50%);
    width: 36px; height: 36px; border-radius: 50%;
    background: rgba(255,255,255,0.1); color: #fff; font-size: 16px;
    align-items: center; justify-content: center;
}
.sidebar-close:hover { background: rgba(255,255,255,0.2); }
.sidebar-overlay {
    display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.5);
    z-index: 99; backdrop-filter: blur(2px);
}
.sidebar-overlay.show { display: block; }
body.no-scroll { overflow: hidden; }
.brand-icon { font-size: 24px; }
.brand-name { color: #fff; font-size: 18px; font-weight: 700; letter-spacing: 0.5px; }
.sidebar-nav { padding: 12px 0; flex: 1; }
.nav-section {
    font-size: 10px; font-weight: 700; letter-spacing: 1.5px;
    color: rgba(255,255,255,0.3); padding: 16px 20px 6px; text-transform: uppercase;
}
.nav-item {
    display: flex; align-items: center; gap: 12px; padding: 11px 20px;
    color: var(--sidebar-text); font-size: 14px; font-weight: 500;
    transition: var(--transition); border-radius: 0;
}
.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active { background: var(--sidebar-active); color: #fff; }
.nav-item i { width: 18px; text-align: center; font-size: 15px; }
.nav-logout { color: #f87171 !important; margin-top: 8px; }
.nav-logout:hover { background: rgba(248,113,113,0.1) !important; }

/* ===== Main Content ===== */
.main-content { margin-left: 240px; flex: 1; display: flex; flex-direction: column; min-height: 100vh; }

/* ===== Topbar ===== */
.topbar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 0 24px; height: 64px; background: var(--surface);
    border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 50;
    box-shadow: var(--shadow);
}
.topbar-right { display: flex; align-items: center; gap: 12px; }
.sidebar-toggle { display: none; color: var(--text); font-size: 18px; padding: 8px; }
.theme-toggle {
    width: 36px; height: 36px; border-radius: 50%; background: var(--surface2);
    border: 1px solid var(--border); color: var(--text-muted); font-size: 15px;
    display: flex; align-items: center; justify-content: center; transition: var(--transition);
}
.theme-toggle:hover { background: var(--primary); color: #fff; border-color: var(--primary); }
.agent-btn {
    width: 40px; height: 40px; border-radius: 50%; background: var(--primary);
    color: #fff; font-size: 16px; display: flex; align-items: center; justify-content: center;
    transition: var(--transition); box-shadow: 0 0 0 0 rgba(67,97,238,0.4);
    animation: pulse-ring 2s infinite;
}
.agent-btn:hover { background: var(--primary-dark); transform: scale(1.05); }
@keyframes pulse-ring {
    0% { box-shadow: 0 0 0 0 rgba(67,97,238,0.4); }
    70% { box-shadow: 0 0 0 8px rgba(67,97,238,0); }
    100% { box-shadow: 0 0 0 0 rgba(67,97,238,0); }
}
.user-pill {
    display: flex; align-items: center; gap: 8px; padding: 6px 12px;
    background: var(--surface2); border: 1px solid var(--border); border-radius: 99px;
}
.user-avatar {
    width: 28px; height: 28px; border-radius: 50%; background: var(--primary);
    color: #fff; font-size: 13px; font-weight: 700;
    display: flex; align-items: center; justify-content: center;
}
.user-name { font-size: 14px; font-weight: 500; color: var(--text); }

/* ===== Page Content ===== */
.page-content { padding: 24px; flex: 1; }

/* ===== Cards ===== */
.card {
    background: var(--surface); border-radius: var(--radius);
    border: 1px solid var(--border); box-shadow: var(--shadow); padding: 24px;
}
.card-sm { padding: 16px; }
.card-grid { display: grid; gap: 20px; }
.card-grid-2 { grid-template-columns: repeat(2, 1fr); }
.card-grid-3 { grid-template-columns: repeat(3, 1fr); }
.card-grid-4 { grid-template-columns: repeat(4, 1fr); }

/* ===== Stats ===== */
.stat-card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 20px; display: flex;
    align-items: center; gap: 16px; box-shadow: var(--shadow);
}
.stat-icon {
    width: 52px; height: 52px; border-radius: 12px;
    display: flex; align-items: center; justify-content: center; font-size: 22px;
}
.stat-icon.blue { background: #eef0fd; }
.stat-icon.green { background: #ecfdf5; }
.stat-icon.purple { background: #fdf4ff; }
.stat-icon.orange { background: #fff7ed; }
.stat-label { font-size: 13px; color: var(--text-muted); margin-bottom: 4px; }
.stat-value { font-size: 24px; font-weight: 700; color: var(--text); }

/* ===== Page Header ===== */
.page-header { margin-bottom: 24px; }
.page-header h1 { font-size: 26px; font-weight: 700; color: var(--text); }
.page-header p { color: var(--text-muted); margin-top: 4px; font-size: 15px; }

/* ===== Buttons ===== */
.btn {
    display: inline-flex; align-items: center; gap: 8px; padding: 10px 20px;
    border-radius: var(--radius-sm); font-size: 14px; font-weight: 600;
    transition: var(--transition); cursor: pointer; border: none;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); transform: translateY(-1px); box-shadow: 0 4px 12px rgba(67,97,238,0.3); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #27ae60; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #c0392b; }
.btn-outline { background: transparent; border: 1.5px solid var(--border); color: var(--text); }
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-sm { padding: 7px 14px; font-size: 13px; }
.btn-lg { padding: 14px 28px; font-size: 16px; }

/* ===== Forms ===== */
.form-group { margin-bottom: 18px; }
.form-label { display: block; margin-bottom: 6px; font-size: 14px; font-weight: 500; color: var(--text); }
.form-input {
    width: 100%; padding: 10px 14px; border: 1.5px solid var(--border);
    border-radius: var(--radius-sm); background: var(--surface2); color: var(--text);
    font-size: 14px; transition: var(--transition); outline: none;
}
.form-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(67,97,238,0.1); background: var(--surface); }
.form-select { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2364748b' d='M6 8L1 3h10z'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; padding-right: 36px; }

/* ===== Flash Messages ===== */
.flash-container { padding: 0 24px; margin-top: 16px; }
.flash {
    display: flex; align-items: center; gap: 10px; padding: 12px 16px;
    border-radius: var(--radius-sm); margin-bottom: 8px; font-size: 14px; font-weight: 500;
}
.flash button { margin-left: auto; background: none; border: none; cursor: pointer; font-size: 18px; opacity: 0.6; }
.flash-success { background: #ecfdf5; color: #065f46; border: 1px solid #a7f3d0; }
.flash-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }

/* ===== Chapter Cards ===== */
.chapter-card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 20px; transition: var(--transition);
    cursor: pointer; position: relative; overflow: hidden;
}
.chapter-card:hover { transform: translateY(-3px); box-shadow: var(--shadow-lg); border-color: var(--primary); }
.chapter-card.completed { border-left: 4px solid var(--success); }
.chapter-icon { font-size: 32px; margin-bottom: 12px; }
.chapter-title { font-size: 15px; font-weight: 700; color: var(--text); margin-bottom: 6px; }
.chapter-desc { font-size: 13px; color: var(--text-muted); line-height: 1.5; }
.chapter-badge {
    position: absolute; top: 12px; right: 12px; padding: 3px 10px;
    border-radius: 99px; font-size: 11px; font-weight: 600;
}
.badge-done { background: #ecfdf5; color: #065f46; }
.badge-progress { background: #fff7ed; color: #92400e; }

/* ===== Progress Bar ===== */
.progress-bar { height: 6px; background: var(--border); border-radius: 99px; overflow: hidden; margin-top: 10px; }
.progress-fill { height: 100%; background: var(--primary); border-radius: 99px; transition: width 0.5s ease; }

/* ===== Chat ===== */
.chat-container { display: flex; flex-direction: column; height: calc(100vh - 180px); }
.chat-messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.chat-bubble { max-width: 75%; padding: 14px 18px; border-radius: 18px; font-size: 14px; line-height: 1.6; }
.chat-bubble.user { background: var(--primary); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.chat-bubble.assistant { background: var(--surface2); color: var(--text); align-self: flex-start; border-bottom-left-radius: 4px; border: 1px solid var(--border); }
.chat-bubble .bubble-meta { font-size: 11px; opacity: 0.6; margin-top: 6px; }
.chat-input-area {
    display: flex; gap: 10px; padding: 16px 24px;
    background: var(--surface); border-top: 1px solid var(--border);
}
.chat-input { flex: 1; padding: 12px 16px; border: 1.5px solid var(--border); border-radius: 99px; background: var(--surface2); color: var(--text); font-size: 14px; outline: none; resize: none; }
.chat-input:focus { border-color: var(--primary); }
.chat-send-btn { width: 46px; height: 46px; border-radius: 50%; background: var(--primary); color: #fff; font-size: 16px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; transition: var(--transition); }
.chat-send-btn:hover { background: var(--primary-dark); transform: scale(1.05); }
.mic-btn { width: 46px; height: 46px; border-radius: 50%; background: var(--surface2); border: 1.5px solid var(--border); color: var(--text-muted); font-size: 16px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; transition: var(--transition); }
.mic-btn.recording { background: var(--danger); color: #fff; border-color: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.05); } }

/* ===== Voice Agent Modal ===== */
.agent-modal {
    display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.7);
    z-index: 1000; align-items: center; justify-content: center; backdrop-filter: blur(4px);
}
.agent-modal.show { display: flex; }
.agent-modal-inner {
    background: var(--surface); border-radius: 24px; padding: 40px;
    width: 380px; text-align: center; position: relative; box-shadow: var(--shadow-lg);
}
.agent-orb {
    width: 120px; height: 120px; border-radius: 50%; background: var(--primary);
    display: flex; align-items: center; justify-content: center;
    font-size: 42px; color: #fff; margin: 0 auto 20px;
    box-shadow: 0 0 40px rgba(67,97,238,0.4);
    transition: var(--transition);
}
.agent-orb.listening {
    background: var(--danger);
    animation: orb-pulse 1.5s infinite;
    box-shadow: 0 0 40px rgba(231,76,60,0.5);
}
@keyframes orb-pulse {
    0%, 100% { transform: scale(1); box-shadow: 0 0 40px rgba(231,76,60,0.4); }
    50% { transform: scale(1.08); box-shadow: 0 0 60px rgba(231,76,60,0.7); }
}
.agent-status { color: var(--text-muted); font-size: 15px; margin-bottom: 16px; }
.agent-response { background: var(--surface2); border-radius: var(--radius-sm); padding: 14px; font-size: 14px; color: var(--text); min-height: 60px; line-height: 1.6; border: 1px solid var(--border); }
.agent-close { position: absolute; top: 16px; right: 16px; width: 32px; height: 32px; border-radius: 50%; background: var(--surface2); color: var(--text-muted); font-size: 14px; display: flex; align-items: center; justify-content: center; }
.agent-close:hover { background: var(--danger); color: #fff; }

/* ===== Auth Pages ===== */
.auth-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg); }
.auth-card { background: var(--surface); border-radius: 20px; padding: 48px 40px; width: 420px; box-shadow: var(--shadow-lg); border: 1px solid var(--border); }
.auth-logo { text-align: center; margin-bottom: 32px; }
.auth-logo .logo-icon { font-size: 48px; display: block; margin-bottom: 12px; }
.auth-logo h1 { font-size: 26px; font-weight: 800; color: var(--text); }
.auth-logo p { color: var(--text-muted); font-size: 14px; margin-top: 4px; }
.auth-divider { text-align: center; color: var(--text-muted); font-size: 13px; margin: 16px 0; }

/* ===== Tables ===== */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; padding: 12px 16px; background: var(--surface2); color: var(--text-muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; border-bottom: 1px solid var(--border); }
td { padding: 14px 16px; border-bottom: 1px solid var(--border); color: var(--text); }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--surface2); }

/* ===== Badges ===== */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 3px 10px; border-radius: 99px; font-size: 12px; font-weight: 600; }
.badge-primary { background: var(--primary-light); color: var(--primary); }
.badge-success { background: #ecfdf5; color: #065f46; }
.badge-warning { background: #fffbeb; color: #92400e; }
.badge-danger { background: #fef2f2; color: #991b1b; }

/* ===== Quiz ===== */
.quiz-option {
    display: block; width: 100%; padding: 12px 16px; margin-bottom: 10px;
    background: var(--surface2); border: 1.5px solid var(--border);
    border-radius: var(--radius-sm); font-size: 14px; text-align: left;
    transition: var(--transition); cursor: pointer; color: var(--text);
}
.quiz-option:hover { border-color: var(--primary); background: var(--primary-light); }
.quiz-option.correct { border-color: var(--success); background: #ecfdf5; color: #065f46; }
.quiz-option.wrong { border-color: var(--danger); background: #fef2f2; color: #991b1b; }
.quiz-option.selected { border-color: var(--primary); background: var(--primary-light); }

/* ===== Tabs ===== */
.tabs { display: flex; gap: 4px; background: var(--surface2); padding: 4px; border-radius: var(--radius-sm); margin-bottom: 24px; border: 1px solid var(--border); }
.tab-btn { flex: 1; padding: 9px 16px; border-radius: 6px; font-size: 14px; font-weight: 500; color: var(--text-muted); transition: var(--transition); }
.tab-btn.active { background: var(--surface); color: var(--primary); font-weight: 600; box-shadow: var(--shadow); }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ===== Utility ===== */
.text-muted { color: var(--text-muted); }
.text-center { text-align: center; }
.mt-4 { margin-top: 16px; }
.mt-6 { margin-top: 24px; }
.mb-4 { margin-bottom: 16px; }
.flex { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-2 { gap: 8px; }
.gap-4 { gap: 16px; }
.w-full { width: 100%; }
.hidden { display: none !important; }

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .sidebar { transform: translateX(-100%); width: 80vw; max-width: 320px; }
    .sidebar.open { transform: translateX(0); }
    .sidebar-close { display: flex; }
    .main-content { margin-left: 0; }
    .sidebar-toggle { display: flex; }
    .card-grid-2, .card-grid-3, .card-grid-4 { grid-template-columns: 1fr; }

    /* Topbar slimmer on phone */
    .topbar { padding: 0 12px; height: 56px; }
    .topbar-right { gap: 8px; }
    .user-name { display: none; }    /* avatar only, save space */
    .page-content { padding: 16px 12px; }
    .page-header h1 { font-size: 20px; }

    /* Tutor mobile layout lives inside ai_tutor.html (so its drawer rules can override
       the inline default). Don't duplicate them here. */

    /* Chat: keep input visible above keyboard, more room for messages */
    .chat-messages { padding: 14px; }
    .chat-bubble { max-width: 88%; font-size: 14px; }
    .chat-input-area {
        padding: 10px 12px;
        padding-bottom: calc(10px + env(safe-area-inset-bottom));
        gap: 8px;
        position: sticky; bottom: 0;
    }
    .chat-input { padding: 10px 14px; font-size: 16px; }  /* 16px stops iOS auto-zoom */
    .mic-btn, .chat-send-btn { width: 42px; height: 42px; font-size: 14px; }

    /* Voice agent modal narrower on phone */
    .agent-modal-inner { width: 92vw; padding: 28px 20px; }
    .agent-orb { width: 100px; height: 100px; font-size: 36px; }
}

/* ===== Typing indicator ===== */
.typing-indicator { display: flex; gap: 4px; align-items: center; padding: 14px 18px; }
.typing-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); animation: typing 1.2s infinite; }
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing { 0%, 60%, 100% { opacity: 0.3; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-4px); } }

/* ===== Section headers ===== */
.section-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.section-title { font-size: 18px; font-weight: 700; color: var(--text); }

/* ===== Chapter Detail ===== */
.chapter-detail-header { background: linear-gradient(135deg, var(--primary), var(--secondary)); color: #fff; padding: 32px; border-radius: var(--radius); margin-bottom: 24px; }
.chapter-detail-header h1 { font-size: 28px; font-weight: 800; }
.chapter-detail-header p { opacity: 0.85; margin-top: 6px; }
.theory-section { margin-bottom: 28px; }
.theory-section h3 { font-size: 17px; font-weight: 700; color: var(--primary); margin-bottom: 12px; padding-bottom: 8px; border-bottom: 2px solid var(--primary-light); }
.theory-section p { color: var(--text); line-height: 1.8; font-size: 15px; }
.example-list { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.example-item { background: var(--surface2); border-left: 3px solid var(--primary); padding: 10px 14px; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; font-size: 14px; color: var(--text); font-style: italic; }
.key-points-list { display: flex; flex-direction: column; gap: 8px; }
.key-point { display: flex; align-items: flex-start; gap: 10px; padding: 10px; background: var(--surface2); border-radius: var(--radius-sm); font-size: 14px; }
.key-point i { color: var(--success); margin-top: 2px; flex-shrink: 0; }
.mistake-list { display: flex; flex-direction: column; gap: 8px; }
.mistake-item { display: flex; align-items: flex-start; gap: 10px; padding: 10px; background: #fef2f2; border-radius: var(--radius-sm); font-size: 14px; color: #991b1b; }
[data-theme="dark"] .mistake-item { background: rgba(239,68,68,0.1); color: #fca5a5; }
.mistake-item i { color: var(--danger); margin-top: 2px; flex-shrink: 0; }
