:root {
    --bg: #f0f4f8;
    --card: #ffffff;
    --primary: #2563eb;
    --primary-light: #dbeafe;
    --primary-dark: #1d4ed8;
    --success: #16a34a;
    --success-light: #dcfce7;
    --danger: #dc2626;
    --danger-light: #fee2e2;
    --warn: #f59e0b;
    --warn-light: #fef3c7;
    --text: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
    --shadow-md: 0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg); color: var(--text); line-height: 1.6;
}

/* ── Header ─────────────────────────────────────────────── */
header { background: linear-gradient(135deg, #1e40af, #7c3aed); color: white; padding: 1rem 2rem; box-shadow: var(--shadow-md); }
.header-content { display: flex; align-items: baseline; gap: 1rem; max-width: 1200px; margin: 0 auto; }
header h1 { font-size: 1.5rem; font-weight: 700; }
.subtitle { font-size: 0.9rem; opacity: 0.8; }
.user-info { font-size: 0.9rem; margin-left: auto; font-weight: 500; }
.logout-link { color: rgba(255,255,255,.7); font-size: 0.8rem; margin-left: 0.5rem; }
.logout-link:hover { color: white; }

/* ── Login ──────────────────────────────────────────────── */
.login-container { display: flex; justify-content: center; align-items: center; min-height: calc(100vh - 64px); }
.login-card { background: var(--card); padding: 2.5rem; border-radius: var(--radius); box-shadow: var(--shadow-md); text-align: center; }
.login-card h2 { margin-bottom: 1.25rem; font-size: 1.4rem; }
.login-card form { display: flex; gap: 0.75rem; }
.login-input {
    padding: 0.65rem 1rem; font-size: 1.1rem; font-family: inherit;
    border: 2px solid var(--border); border-radius: 8px; outline: none; width: 200px;
}
.login-input:focus { border-color: var(--primary); }

/* ── Layout ─────────────────────────────────────────────── */
#main { display: flex; max-width: 1200px; margin: 0 auto; padding: 1.5rem; gap: 1.5rem; min-height: calc(100vh - 64px); }
#content { flex: 1; min-width: 0; }
#exercise-area { animation: fadeIn 0.2s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

/* ── Skill navigation ───────────────────────────────────── */
#skill-nav { display: flex; flex-direction: column; gap: 0.25rem; width: 260px; flex-shrink: 0; }

.skill-group-label {
    font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em;
    color: var(--text-muted); padding: 0.6rem 0.5rem 0.2rem; margin-top: 0.3rem;
}
.skill-group-label:first-child { margin-top: 0; }

.skill-btn {
    display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
    padding: 0.5rem 0.7rem; background: var(--card); border: 1.5px solid var(--border);
    border-radius: 8px; cursor: pointer; font-family: inherit; font-size: 0.82rem;
    color: var(--text); text-align: left; transition: all 0.15s; box-shadow: var(--shadow);
}
.skill-btn:hover { border-color: var(--primary); background: var(--primary-light); }
.skill-btn.active { border-color: var(--primary); background: var(--primary-light); font-weight: 600; }
.skill-btn.mastered { border-color: var(--success); }
.skill-btn.mastered .skill-name { color: var(--success); }

.skill-name { flex: 1; }
.skill-tiers { display: flex; gap: 3px; flex-shrink: 0; }

.tier-dot {
    width: 8px; height: 8px; border-radius: 50%; background: var(--border); transition: all 0.2s;
}
.tier-dot.done { background: var(--success); }
.tier-dot.current { background: var(--primary); box-shadow: 0 0 0 2px rgba(37,99,235,.2); }

/* ── Topic header ───────────────────────────────────────── */
.topic-header { margin-bottom: 1rem; }
.topic-header h2 { font-size: 1.3rem; font-weight: 700; color: var(--text); margin-bottom: 0.25rem; }

.tier-indicator { display: flex; align-items: center; gap: 0.5rem; margin-top: 0.3rem; }
.tier-label {
    font-size: 0.75rem; font-weight: 600; padding: 0.15rem 0.6rem; border-radius: 999px;
}
.tier-label.tier-0 { background: #dcfce7; color: #166534; }
.tier-label.tier-1 { background: #fef3c7; color: #92400e; }
.tier-label.tier-2 { background: #fee2e2; color: #991b1b; }
.tier-dots { display: flex; gap: 4px; }

/* ── Skill description ("man page") ────────────────────── */
.skill-description {
    margin-bottom: 1rem; border-radius: 8px; background: var(--card);
    border: 1px solid var(--border); font-size: 0.92rem;
}
.skill-description summary {
    padding: 0.6rem 1rem; cursor: pointer; font-weight: 600; color: var(--primary);
    user-select: none; list-style: none;
}
.skill-description summary::before { content: "📖 "; }
.skill-description summary::-webkit-details-marker { display: none; }
.skill-description[open] summary { border-bottom: 1px solid var(--border); }
.skill-description-body { padding: 0.75rem 1rem; line-height: 1.65; color: var(--text-light); }
.skill-description-body p { margin: 0.4rem 0; }
.skill-description-body strong { color: var(--text); }

/* ── Redirect notice ────────────────────────────────────── */
.redirect-notice {
    padding: 0.75rem 1rem; margin-bottom: 1rem; border-radius: 8px;
    background: var(--warn-light); border: 1.5px solid var(--warn);
    font-size: 0.9rem; color: #78350f;
}

/* ── Data tables (delta exercises) ──────────────────────── */
.data-table {
    border-collapse: collapse; margin: 0.75rem 0; font-size: 0.9rem;
}
.data-table th, .data-table td {
    border: 1px solid var(--border); padding: 0.3rem 0.75rem; text-align: right;
}
.data-table th { background: var(--bg); font-weight: 600; }
.data-table td:first-child, .data-table th:first-child { text-align: left; }

/* ── Gap detected ───────────────────────────────────────── */
.gap-detected {
    margin-top: 0.75rem; padding: 0.6rem 1rem; border-radius: 8px;
    background: var(--warn-light); border: 1.5px solid var(--warn);
    font-size: 0.88rem; color: #78350f;
}
.btn-link {
    background: none; border: none; color: var(--primary); cursor: pointer;
    font-weight: 600; font-size: inherit; font-family: inherit; text-decoration: underline;
    padding: 0;
}

/* ── Exercise card ──────────────────────────────────────── */
.exercise-card { background: var(--card); border-radius: var(--radius); padding: 1.75rem; box-shadow: var(--shadow-md); border: 1px solid var(--border); }
.question { font-size: 1.05rem; line-height: 1.7; margin-bottom: 1.25rem; }
.question strong { color: var(--primary-dark); }
.question em { display: block; margin-top: 0.5rem; color: var(--text-muted); font-size: 0.9rem; }

/* ── Graph ──────────────────────────────────────────────── */
.graph-container { background: #fafbfc; border: 1px solid var(--border); border-radius: 8px; padding: 1rem; margin-bottom: 1.25rem; display: flex; justify-content: center; }
.physics-graph { max-width: 100%; height: auto; cursor: crosshair; }
.graph-story { margin-top: 0.75rem; padding: 0.65rem 1rem; background: linear-gradient(135deg, #eff6ff, #f0fdf4); border-left: 3px solid var(--primary); border-radius: 0 6px 6px 0; font-size: 0.9rem; line-height: 1.5; font-style: italic; }

/* ── Hint ───────────────────────────────────────────────── */
.hint-wrapper { margin-bottom: 1rem; }
.btn-hint {
    background: none; border: 1.5px dashed var(--border); border-radius: 6px;
    padding: 0.4rem 0.8rem; font-size: 0.85rem; color: var(--text-muted);
    cursor: pointer; font-family: inherit; transition: all 0.15s;
}
.btn-hint:hover { border-color: var(--warn); color: var(--warn); }
.hint-content {
    margin-top: 0.5rem; padding: 0.6rem 1rem; border-radius: 6px;
    background: var(--warn-light); border: 1px solid var(--warn);
    font-size: 0.9rem; color: #78350f; line-height: 1.5;
}

/* ── Answer form ────────────────────────────────────────── */
.answer-form { margin-top: 0.5rem; }
.answer-row { display: flex; align-items: center; gap: 0.75rem; }
.answer-input {
    flex: 1; max-width: 200px; padding: 0.65rem 1rem; font-size: 1.1rem; font-family: inherit;
    border: 2px solid var(--border); border-radius: 8px; outline: none; transition: border-color 0.15s;
    -moz-appearance: textfield;
}
.answer-input::-webkit-inner-spin-button, .answer-input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.answer-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }
.unit-label { font-size: 1rem; color: var(--text-muted); font-weight: 500; min-width: 3rem; }

/* ── Buttons ────────────────────────────────────────────── */
.btn { display: inline-flex; align-items: center; gap: 0.4rem; padding: 0.65rem 1.5rem; font-size: 0.95rem; font-family: inherit; font-weight: 600; border: none; border-radius: 8px; cursor: pointer; transition: all 0.15s; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-next { margin-top: 1.25rem; }

/* ── Feedback ───────────────────────────────────────────── */
.feedback-card { background: var(--card); border-radius: var(--radius); padding: 1.75rem; box-shadow: var(--shadow-md); border: 2px solid var(--border); animation: fadeIn 0.2s ease; }
.feedback-card.correct { border-color: var(--success); background: var(--success-light); }
.feedback-card.wrong { border-color: var(--danger); background: var(--danger-light); }
.feedback-header { display: flex; align-items: center; gap: 0.5rem; font-size: 1.2rem; margin-bottom: 1rem; }
.feedback-icon { font-size: 1.4rem; }
.feedback-answer { background: rgba(255,255,255,0.7); padding: 0.75rem 1rem; border-radius: 8px; margin-bottom: 1rem; font-size: 0.95rem; line-height: 1.8; }
.feedback-explanation { padding: 0.75rem 1rem; background: rgba(255,255,255,0.7); border-radius: 8px; margin-bottom: 1rem; font-size: 0.95rem; line-height: 1.7; }
.feedback-explanation em { display: block; margin-top: 0.5rem; color: var(--text-muted); }
.feedback-progress { margin-top: 0.25rem; }
.feedback-score { font-size: 0.9rem; color: var(--text-muted); }
.score-pct { font-weight: 600; color: var(--text); }
.stage-advance { margin-top: 0.5rem; padding: 0.4rem 0.75rem; background: var(--success-light); color: var(--success); border-radius: 6px; font-size: 0.85rem; font-weight: 600; }
.stage-hint { margin-top: 0.5rem; font-size: 0.85rem; color: var(--text-muted); font-style: italic; }

/* ── Admin modal ────────────────────────────────────────── */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 100;
    display: flex; align-items: center; justify-content: center;
}
.modal-box {
    background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow-md);
    padding: 1.5rem; width: 340px; max-width: 90vw;
}
.modal-head {
    display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem;
}
.btn-close {
    background: none; border: none; font-size: 1.4rem; cursor: pointer; color: var(--text-muted);
    padding: 0; line-height: 1;
}
.admin-field { margin-bottom: 0.75rem; }
.admin-field label { display: block; font-size: 0.8rem; color: var(--text-muted); margin-bottom: 0.2rem; }
.admin-field select, .admin-field input {
    width: 100%; padding: 0.45rem 0.6rem; font-size: 0.9rem; font-family: inherit;
    border: 1.5px solid var(--border); border-radius: 6px;
}
.admin-done { padding: 1rem; text-align: center; font-size: 0.95rem; }

/* ── Multiple choice grid ───────────────────────────────── */
.mc-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
    margin: 1rem 0;
}
.mc-option {
    display: flex; flex-direction: column; align-items: center;
    padding: 0.5rem; border: 2px solid #d1d5db; border-radius: 0.5rem;
    background: #fff; cursor: pointer; transition: border-color 0.15s, box-shadow 0.15s;
}
.mc-option:hover { border-color: #3b82f6; box-shadow: 0 0 0 2px rgba(59,130,246,0.2); }
.mc-option:active { border-color: #2563eb; background: #eff6ff; }
.mc-label {
    font-weight: 700; font-size: 1.1rem; color: #374151;
    margin-bottom: 0.25rem;
}
.mc-text { font-size: 0.9rem; color: #4b5563; text-align: center; margin-top: 0.25rem; }
.mc-graph { display: block; max-width: 100%; height: auto; }

/* ── Draw-a-graph controls ─────────────────────────────── */
.draw-controls {
    display: flex; gap: 0.5rem; margin-top: 0.5rem;
}
.draw-graph { cursor: crosshair; }

/* ── Responsive ─────────────────────────────────────────── */
@media (max-width: 768px) {
    #main { flex-direction: column; padding: 1rem; }
    #skill-nav { width: 100%; flex-direction: row; flex-wrap: wrap; gap: 0.3rem; }
    .skill-group-label { width: 100%; margin: 0.3rem 0 0; }
    .skill-btn { min-width: 120px; flex: 1; padding: 0.4rem 0.5rem; font-size: 0.75rem; }
    .header-content { flex-direction: column; gap: 0.25rem; }
    .exercise-card { padding: 1.25rem; }
    .answer-row { flex-wrap: wrap; }
    .answer-input { max-width: none; }
    .mc-grid { grid-template-columns: 1fr; }
}
