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

:root {
    --accent: #3b5bdb;
    --accent-hover: #2f4ac5;
    --accent-light: #eef1ff;
    --text: #1a1a2e;
    --text-muted: #6b7280;
    --bg: #ffffff;
    --bg-light: #f8f9fa;
    --border: #e5e7eb;
    --success: #2f9e44;
    --success-bg: #ebfbee;
    --success-border: #b2f2bb;
    --error: #e03131;
    --error-bg: #fff5f5;
    --error-border: #ffc9c9;
    --warn-bg: #fff9db;
    --warn-border: #ffec99;
    --radius: 8px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    color: var(--text);
    background: var(--bg);
    line-height: 1.6;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ── Loading overlay ── */
.loading-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(255, 255, 255, 0.88);
    z-index: 999;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
}
.loading-overlay.active { display: flex; }

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-overlay p {
    color: var(--text-muted);
    font-size: 0.95rem;
}

/* ── Header ── */
header {
    border-bottom: 1px solid var(--border);
    background: var(--bg);
    position: sticky;
    top: 0;
    z-index: 100;
}

nav {
    max-width: 860px;
    margin: 0 auto;
    padding: 0 1.5rem;
    height: 58px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.logo {
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--accent);
    text-decoration: none;
    letter-spacing: -0.5px;
}

.nav-links {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.nav-links a {
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.9rem;
    transition: color 0.15s;
}
.nav-links a:hover { color: var(--text); }

/* ── Main ── */
main {
    flex: 1;
    max-width: 860px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    width: 100%;
}

/* ── Footer ── */
footer {
    border-top: 1px solid var(--border);
    padding: 1.25rem;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.82rem;
}

/* ── Buttons ── */
.btn-primary {
    background: var(--accent);
    color: #fff !important;
    padding: 0.55rem 1.25rem;
    border-radius: var(--radius);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    border: none;
    cursor: pointer;
    display: inline-block;
    transition: background 0.15s;
}
.btn-primary:hover { background: var(--accent-hover) !important; }

.btn-large {
    padding: 0.75rem 2rem;
    font-size: 1rem;
}

.btn-secondary {
    background: var(--bg-light);
    color: var(--text) !important;
    padding: 0.55rem 1.25rem;
    border-radius: var(--radius);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    border: 1px solid var(--border);
    cursor: pointer;
    display: inline-block;
    transition: background 0.15s;
}
.btn-secondary:hover { background: var(--border); }

/* ── Alerts ── */
.alert {
    padding: 0.875rem 1.25rem;
    border-radius: var(--radius);
    margin-bottom: 1.25rem;
    font-size: 0.9rem;
}
.alert-error  { background: var(--error-bg);   color: var(--error);   border: 1px solid var(--error-border); }
.alert-success { background: var(--success-bg); color: var(--success); border: 1px solid var(--success-border); }

/* ── Hero ── */
.hero {
    text-align: center;
    padding: 3rem 0 2.5rem;
}
.hero h1 {
    font-size: 2.75rem;
    font-weight: 700;
    letter-spacing: -1.5px;
    color: var(--text);
    margin-bottom: 0.5rem;
}
.hero p {
    color: var(--text-muted);
    font-size: 1.1rem;
    margin-bottom: 2rem;
}

.search-form {
    display: flex;
    gap: 0.5rem;
    max-width: 580px;
    margin: 0 auto;
}
.search-form input {
    flex: 1;
    padding: 0.75rem 1rem;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.97rem;
    outline: none;
    color: var(--text);
    background: var(--bg);
    transition: border-color 0.15s;
}
.search-form input:focus { border-color: var(--accent); }
.search-form button {
    padding: 0.75rem 1.5rem;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    font-size: 0.97rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s;
}
.search-form button:hover { background: var(--accent-hover); }

/* ── Section title ── */
.section-title {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 0.875rem;
    margin-top: 2rem;
}

/* ── Topic grid ── */
.topic-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.topic-card {
    padding: 0.875rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    text-decoration: none;
    color: var(--text);
    display: block;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.topic-card:hover {
    border-color: var(--accent);
    box-shadow: 0 2px 8px rgba(59, 91, 219, 0.08);
}
.topic-title {
    font-weight: 500;
    font-size: 0.93rem;
    display: block;
}
.topic-meta {
    font-size: 0.76rem;
    color: var(--text-muted);
    margin-top: 0.3rem;
    display: flex;
    gap: 0.4rem;
    align-items: center;
}
.badge-quiz {
    background: var(--success-bg);
    color: var(--success);
    font-size: 0.7rem;
    padding: 0.1rem 0.45rem;
    border-radius: 999px;
    font-weight: 600;
}

/* ── Topic page ── */
.back-link {
    display: inline-block;
    font-size: 0.85rem;
    color: var(--text-muted);
    text-decoration: none;
    margin-bottom: 1rem;
}
.back-link:hover { color: var(--accent); }

.topic-header {
    margin-bottom: 2rem;
    padding-bottom: 1.25rem;
    border-bottom: 1px solid var(--border);
}
.topic-header h1 {
    font-size: 2rem;
    font-weight: 700;
    letter-spacing: -0.5px;
    margin-bottom: 0.4rem;
}
.topic-header .meta {
    color: var(--text-muted);
    font-size: 0.83rem;
}

.topic-content {
    line-height: 1.8;
    color: #2c2c2c;
    font-size: 1rem;
}
.topic-content h2 {
    font-size: 1.2rem;
    font-weight: 600;
    margin: 1.75rem 0 0.6rem;
    color: var(--text);
}
.topic-content h3 {
    font-size: 1.05rem;
    font-weight: 600;
    margin: 1.25rem 0 0.4rem;
}
.topic-content p { margin-bottom: 0.9rem; }
.topic-content ul,
.topic-content ol { margin: 0.4rem 0 0.9rem 1.5rem; }
.topic-content li { margin-bottom: 0.25rem; }
.topic-content strong { font-weight: 600; color: var(--text); }

/* ── Quiz CTA ── */
.quiz-cta {
    margin-top: 2.5rem;
    padding: 1.75rem;
    background: var(--accent-light);
    border-radius: var(--radius);
    text-align: center;
}
.quiz-cta h3 {
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 0.3rem;
}
.quiz-cta p {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-bottom: 1rem;
}
.quiz-result-inline {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 0.75rem;
}

/* ── Quiz page ── */
.quiz-header {
    margin-bottom: 2rem;
}
.quiz-header h1 {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
}
.quiz-header p { color: var(--text-muted); font-size: 0.9rem; }

.question-block {
    margin-bottom: 1.5rem;
    padding: 1.25rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.question-number {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 0.4rem;
}
.question-text {
    font-weight: 600;
    font-size: 1rem;
    margin-bottom: 0.875rem;
}
.option-label {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    padding: 0.55rem 0.875rem;
    border-radius: 6px;
    cursor: pointer;
    margin-bottom: 0.35rem;
    border: 1px solid var(--border);
    font-size: 0.93rem;
    transition: background 0.1s, border-color 0.1s;
}
.option-label:hover {
    background: var(--bg-light);
    border-color: var(--accent);
}
.option-label input[type="radio"] {
    accent-color: var(--accent);
    width: 15px;
    height: 15px;
    flex-shrink: 0;
}

.quiz-actions {
    display: flex;
    gap: 0.875rem;
    align-items: center;
    flex-wrap: wrap;
    margin-top: 1.25rem;
}

/* ── Quiz result ── */
.score-banner {
    text-align: center;
    padding: 2rem;
    border-radius: var(--radius);
    margin-bottom: 2rem;
    border: 1px solid transparent;
}
.score-banner.good { background: var(--success-bg); border-color: var(--success-border); }
.score-banner.ok   { background: var(--warn-bg);    border-color: var(--warn-border); }
.score-banner.bad  { background: var(--error-bg);   border-color: var(--error-border); }

.score-number {
    font-size: 3rem;
    font-weight: 700;
    line-height: 1;
    margin-bottom: 0.25rem;
}
.score-label { font-size: 1rem; }

.results-list { margin-bottom: 1.75rem; }

.result-block {
    margin-bottom: 1rem;
    padding: 1.1rem 1.25rem;
    border-radius: var(--radius);
    border: 1px solid var(--border);
}
.result-block.correct { border-color: var(--success-border); background: var(--success-bg); }
.result-block.wrong   { border-color: var(--error-border);   background: var(--error-bg); }

.result-question {
    font-weight: 600;
    margin-bottom: 0.6rem;
    font-size: 0.97rem;
}
.result-option {
    padding: 0.35rem 0.7rem;
    border-radius: 5px;
    font-size: 0.88rem;
    margin-bottom: 0.25rem;
    display: inline-block;
}
.result-option.user-correct  { background: #b2f2bb; color: #1a7c34; font-weight: 600; }
.result-option.user-wrong    { background: #ffc9c9; color: #c92a2a; font-weight: 600; }
.result-option.correct-answer { background: #b2f2bb; color: #1a7c34; font-weight: 600; }
.correct-tag { font-size: 0.8em; opacity: 0.8; }

/* ── Auth ── */
.auth-container {
    max-width: 400px;
    margin: 2rem auto;
}
.auth-container h1 {
    font-size: 1.75rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
}
.subtitle {
    color: var(--text-muted);
    margin-bottom: 2rem;
    font-size: 0.93rem;
}

.form-group { margin-bottom: 1.25rem; }
.form-group label {
    display: block;
    font-size: 0.88rem;
    font-weight: 500;
    margin-bottom: 0.375rem;
}
.label-hint {
    color: var(--text-muted);
    font-weight: 400;
}
.form-group input {
    width: 100%;
    padding: 0.7rem 0.875rem;
    border: 1.5px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.95rem;
    outline: none;
    background: var(--bg);
    color: var(--text);
    transition: border-color 0.15s;
}
.form-group input:focus { border-color: var(--accent); }

.form-submit {
    width: 100%;
    padding: 0.75rem;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s;
    margin-top: 0.5rem;
}
.form-submit:hover { background: var(--accent-hover); }

.auth-switch {
    text-align: center;
    margin-top: 1.25rem;
    font-size: 0.9rem;
    color: var(--text-muted);
}
.auth-switch a { color: var(--accent); text-decoration: none; font-weight: 500; }

/* ── Profile ── */
.profile-header { margin-bottom: 2rem; }
.profile-header h1 {
    font-size: 1.75rem;
    font-weight: 700;
    margin-bottom: 0.2rem;
}
.profile-header p { color: var(--text-muted); font-size: 0.9rem; }

.history-table {
    width: 100%;
    border-collapse: collapse;
}
.history-table th {
    text-align: left;
    padding: 0.6rem 0.875rem;
    font-size: 0.76rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-bottom: 2px solid var(--border);
}
.history-table td {
    padding: 0.75rem 0.875rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
    vertical-align: middle;
}
.history-table tr:last-child td { border-bottom: none; }
.history-table tr:hover td { background: var(--bg-light); }
.history-table a { color: var(--accent); text-decoration: none; font-weight: 500; }
.history-table a:hover { text-decoration: underline; }

.score-pill {
    display: inline-block;
    padding: 0.15rem 0.55rem;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
}
.score-pill.good { background: var(--success-bg); color: var(--success); }
.score-pill.ok   { background: var(--warn-bg);    color: #e67700; }
.score-pill.bad  { background: var(--error-bg);   color: var(--error); }

.text-muted { color: var(--text-muted); }

/* ── Responsive ── */
@media (max-width: 600px) {
    .hero h1 { font-size: 2rem; }
    .search-form { flex-direction: column; }
    .topic-grid { grid-template-columns: 1fr 1fr; }
    .history-table th:nth-child(3),
    .history-table td:nth-child(3) { display: none; }
    .quiz-actions { flex-direction: column; align-items: stretch; }
    .quiz-actions .btn-primary,
    .quiz-actions .btn-secondary { text-align: center; }
}
