/* Shared styles for the student pages. Mobile first; logical properties keep
   the Arabic (right-to-left) layout mirrored without separate rules.
   Look: warm paper background, white panels, near-black ink, no borders or
   shadows on panels, light headings, colour only as small status dots/tints. */

:root {
    --bg: #f3f1ec;
    --surface: #ffffff;
    --tile: #f3f1ec;
    --tile-hover: #e9e6df;
    --ink: #171717;
    --muted: #716d65;
    --line: #e7e3db;
    --ok: #2f6b4f;
    --ok-soft: #eaf1ec;
    --bad: #b3372c;
    --bad-soft: #f8ebe8;
    --warn: #a8641a;
    --warn-soft: #fbf1e3;
    --radius: 12px;
    --radius-lg: 18px;
}

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

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Arabic", Tahoma, Arial, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }
h1, h2, h3 { font-weight: 400; letter-spacing: -.01em; }
a { color: var(--ink); }
img, video { max-width: 100%; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.icon {
    width: 1.25em;
    height: 1.25em;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.6;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex: none;
}

/* ---- Top bar ------------------------------------------------------------ */
.topbar-inner {
    max-width: 1040px;
    margin: 0 auto;
    padding: 18px 16px 6px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}
.brand {
    font-weight: 500;
    font-size: 1.1rem;
    letter-spacing: -.01em;
    color: var(--ink);
    text-decoration: none;
}
.topbar-links {
    display: flex;
    align-items: center;
    gap: 4px 18px;
    flex-wrap: wrap;
    font-size: .95rem;
}
.topbar-links > a {
    color: var(--muted);
    text-decoration: none;
    padding: 6px 0;
}
.topbar-links > a:hover { color: var(--ink); }

.lang-switch {
    display: inline-flex;
    gap: 2px;
    padding: 3px;
    border-radius: 10px;
    background: var(--tile-hover);
}
.lang-switch a {
    padding: 4px 10px;
    border-radius: 7px;
    font-size: .8rem;
    font-weight: 500;
    color: var(--muted);
    text-decoration: none;
}
.lang-switch a.active { background: var(--surface); color: var(--ink); }

/* ---- Layout ------------------------------------------------------------- */
.page {
    max-width: 1040px;
    margin: 0 auto;
    padding: 20px 16px 48px;
}
.page-narrow { max-width: 720px; }
.page-title { font-size: 2rem; line-height: 1.2; margin-bottom: 20px; }
.section-title {
    font-size: .75rem;
    font-weight: 500;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--muted);
    margin: 32px 0 12px;
}
.muted { color: var(--muted); }

.card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 22px;
}

/* ---- Buttons ------------------------------------------------------------ */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 46px;
    padding: 10px 20px;
    border: 0;
    border-radius: var(--radius);
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    background: none;
    transition: background-color .12s;
}
.btn:disabled { opacity: .35; cursor: default; }
.btn-primary, .btn-success { background: var(--ink); color: #fff; }
.btn-primary:hover, .btn-success:hover { background: #000; }
.btn-ghost { background: var(--tile); color: var(--ink); }
.btn-ghost:hover:not(:disabled) { background: var(--tile-hover); }
.btn-danger-ghost { background: var(--tile); color: var(--bad); }
.btn-danger-ghost:hover { background: var(--bad-soft); }
.btn-small { min-height: 36px; padding: 6px 14px; font-size: .9rem; border-radius: 10px; }
.btn-icon { padding: 10px 13px; }
.btn-block { width: 100%; }
.btn-grow { flex: 1; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; }

/* ---- Forms & alerts ----------------------------------------------------- */
.field { margin-bottom: 16px; }
.field label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 6px; }
.input, .select {
    width: 100%;
    min-height: 46px;
    padding: 10px 14px;
    border: 2px solid transparent;
    border-radius: var(--radius);
    background: var(--tile);
    font: inherit;
    color: inherit;
}
.input:focus, .select:focus {
    outline: none;
    border-color: var(--ink);
    background: var(--surface);
}
.btn:focus-visible, .answer-main:focus-visible, .nav-cell:focus-visible, .speak:focus-visible {
    outline: 2px solid var(--ink);
    outline-offset: 2px;
}
.alert {
    padding: 12px 16px;
    border-radius: var(--radius);
    margin-bottom: 16px;
    font-size: .95rem;
}
.alert-error { background: var(--bad-soft); color: var(--bad); }
.alert-ok { background: var(--ok-soft); color: var(--ok); }

/* ---- Login / register --------------------------------------------------- */
.auth {
    max-width: 400px;
    margin: 0 auto;
    padding: 56px 16px;
}
.auth-title { font-size: 2rem; margin-bottom: 24px; }
.auth-foot { margin-top: 18px; font-size: .95rem; color: var(--muted); }
.auth-lang { display: flex; margin-top: 20px; }

/* ---- Dashboard ---------------------------------------------------------- */
.hero {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 26px;
}
.hero h2 { font-size: 1.75rem; line-height: 1.2; }
.cat-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
}
.cat-card { display: flex; flex-direction: column; gap: 10px; }
.cat-card h3 { font-size: 1.15rem; line-height: 1.3; }
.cat-card p { color: var(--muted); font-size: .95rem; flex: 1; }
.cat-card .btn { margin-top: 4px; }
/* One black button per screen (the final test); category buttons stay quiet. */
.cat-card .btn-primary { background: var(--tile); color: var(--ink); }
.cat-card .btn-primary:hover { background: var(--tile-hover); }
.cat-stats { display: flex; gap: 16px; font-size: .85rem; color: var(--muted); }
.cat-stats strong { color: var(--ink); font-weight: 500; }
.meter { height: 4px; border-radius: 2px; background: var(--line); overflow: hidden; }
.meter > div { height: 100%; background: var(--ink); border-radius: 2px; }
.meter > div.ok { background: var(--ok); }
.meter > div.bad { background: var(--bad); }

/* ---- Quiz --------------------------------------------------------------- */
.quiz-page { padding-bottom: calc(70px + env(safe-area-inset-bottom)); }

.quizbar {
    position: sticky;
    top: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    background: var(--bg);
}
.quizbar .btn-danger-ghost, .quizbar .flag-btn { background: var(--surface); }
.quizbar-back {
    display: inline-flex;
    padding: 8px;
    color: var(--muted);
    border-radius: 10px;
}
.quizbar-back:hover { background: var(--tile-hover); color: var(--ink); }
.quizbar-title {
    flex: 1;
    min-width: 0;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.timer {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border-radius: 10px;
    background: var(--surface);
    font-weight: 500;
    font-variant-numeric: tabular-nums;
}
.timer.warning { background: var(--warn-soft); color: var(--warn); }
.timer.danger { background: var(--bad); color: #fff; }

.progress { height: 3px; background: var(--line); position: sticky; top: 48px; z-index: 5; }
.progress-fill { height: 100%; width: 0; background: var(--ink); transition: width .2s; }

/* Phones: everything is kept tight so the picture, the question and all the
   answers are on screen together; the roomier sizes start at 640px below. */
.quiz-main {
    max-width: 760px;
    margin: 0 auto;
    padding: 8px;
}
.qcard {
    background: var(--surface);
    border-radius: var(--radius-lg);
    overflow: hidden;
}
.q-media {
    margin: 8px 8px 0;
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--tile);
    display: flex;
    justify-content: center;
}
.q-media img, .q-media video {
    display: block;
    width: 100%;
    max-height: min(30vh, 240px);
    object-fit: contain;
}
.q-media img { cursor: zoom-in; }
.q-body { padding: 12px 14px 14px; }
/* The bottom bar already shows "3 / 65" on a phone. */
.q-count { display: none; font-size: .85rem; color: var(--muted); margin-bottom: 10px; }
.flag-btn {
    flex: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 36px;
    padding: 6px 9px;
    border: 0;
    border-radius: 10px;
    background: var(--tile);
    color: var(--muted);
    font-size: .85rem;
    cursor: pointer;
}
.flag-btn:hover { background: var(--tile-hover); }
.flag-btn span { display: none; }
.quizbar .flag-btn.flagged, .flag-btn.flagged { background: var(--warn-soft); color: var(--warn); }
.flag-btn.flagged .icon { fill: currentColor; }

.q-text-row { display: flex; align-items: flex-start; gap: 6px; margin-bottom: 10px; }
.q-text { flex: 1; font-size: 1.2rem; line-height: 1.3; }

.speak {
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border: 0;
    border-radius: 10px;
    background: none;
    color: var(--muted);
    cursor: pointer;
}
.speak:hover { background: var(--tile); color: var(--ink); }
.speak.speaking { background: var(--ink); color: #fff; animation: speak-pulse 1.1s ease-in-out infinite; }
@keyframes speak-pulse { 50% { opacity: .55; } }

.answers { display: flex; flex-direction: column; gap: 6px; }
.answer {
    display: flex;
    align-items: center;
    border: 2px solid transparent;
    border-radius: var(--radius);
    background: var(--tile);
    transition: border-color .12s, background-color .12s;
}
.answer:hover:not(.locked) { background: var(--tile-hover); }
.answer-main {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 44px;
    padding: 8px 12px;
    border: 0;
    background: none;
    text-align: start;
    font-size: 1rem;
    line-height: 1.35;
    cursor: pointer;
    border-radius: var(--radius);
}
.answer-main:disabled { cursor: default; color: inherit; }
.answer-mark {
    flex: none;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #c9c4b9;
}
.answer .speak { margin-inline-end: 4px; }
.answer.selected, .answer.selected:hover { border-color: var(--ink); background: var(--surface); }
.answer.selected .answer-mark { background: var(--ink); }
.answer.correct, .answer.correct:hover { border-color: transparent; background: var(--ok-soft); }
.answer.correct .answer-mark { background: var(--ok); }
.answer.incorrect, .answer.incorrect:hover { border-color: transparent; background: var(--bad-soft); }
.answer.incorrect .answer-mark { background: var(--bad); }

.feedback {
    scroll-margin-bottom: 80px;
    margin-top: 8px;
    padding: 14px;
    border-radius: var(--radius-lg);
    background: var(--surface);
}
.feedback p { margin-top: 6px; color: var(--muted); }
.feedback p strong { font-weight: 500; color: var(--ink); }
.feedback-title { font-weight: 500; display: flex; align-items: center; gap: 8px; }
.feedback-title::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.feedback-ok .feedback-title::before { background: var(--ok); }
.feedback-bad .feedback-title::before { background: var(--bad); }
.feedback-explanation { display: flex; align-items: flex-start; gap: 8px; }
.feedback-explanation p { flex: 1; }

.quiz-actions {
    position: fixed;
    inset-inline: 0;
    bottom: 0;
    z-index: 5;
    background: var(--bg);
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
}
.quiz-actions .btn-ghost { background: var(--surface); }
.quiz-actions .btn-ghost:hover:not(:disabled) { background: var(--tile-hover); }
.quiz-actions-inner {
    max-width: 736px;
    margin: 0 auto;
    display: flex;
    gap: 8px;
}
#nav-count { font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Question navigator: bottom sheet on phones, centred panel on wide screens */
dialog { border: 0; padding: 0; color: inherit; }
dialog::backdrop { background: rgba(23, 23, 23, .35); }
.sheet {
    position: fixed;
    inset: auto 0 0 0;
    margin: 0;
    width: 100%;
    max-width: none;
    max-height: 80vh;
    border-radius: 20px 20px 0 0;
    background: var(--surface);
    padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
}
.sheet[open] { display: flex; flex-direction: column; }
.sheet-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.sheet-head strong { font-weight: 400; font-size: 1.4rem; letter-spacing: -.01em; }
.sheet-summary { flex: 1; color: var(--muted); font-variant-numeric: tabular-nums; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: .85rem; color: var(--muted); margin-bottom: 14px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #c9c4b9; display: inline-block; }
.dot-answered { background: var(--ink); }
.dot-flagged { background: var(--warn); }
.nav-grid {
    overflow-y: auto;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(44px, 1fr));
    gap: 6px;
    padding: 4px;
}
.nav-cell {
    position: relative;
    height: 44px;
    border: 0;
    border-radius: 10px;
    background: var(--tile);
    color: var(--muted);
    font-variant-numeric: tabular-nums;
    cursor: pointer;
}
.nav-cell:hover { background: var(--tile-hover); }
.nav-cell.answered { background: var(--ink); color: #fff; }
.nav-cell.flagged::after {
    content: "";
    position: absolute;
    top: -3px;
    inset-inline-end: -3px;
    width: 11px;
    height: 11px;
    border-radius: 50%;
    background: var(--warn);
    border: 2px solid var(--surface);
}
.nav-cell.current { outline: 2px solid var(--ink); outline-offset: 2px; }

.lightbox {
    max-width: 100vw;
    max-height: 100vh;
    width: 100vw;
    height: 100vh;
    background: rgba(23, 23, 23, .92);
    cursor: zoom-out;
}
.lightbox[open] { display: flex; align-items: center; justify-content: center; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lightbox-close {
    position: fixed;
    top: calc(12px + env(safe-area-inset-top));
    inset-inline-end: 12px;
    width: 46px;
    height: 46px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: 12px;
    background: var(--surface);
    color: var(--ink);
    font-size: 1.1rem;
    cursor: pointer;
    outline: none; /* it is focused automatically when the view opens */
}

.toast {
    position: fixed;
    inset-inline: 12px;
    top: 64px;
    z-index: 20;
    max-width: 420px;
    margin: 0 auto;
    padding: 12px 16px;
    border-radius: var(--radius);
    background: var(--ink);
    color: #fff;
    text-align: center;
    font-size: .95rem;
}
.toast-warn { background: var(--warn); }
.toast-error { background: var(--bad); }

/* ---- Results ------------------------------------------------------------ */
.result-score {
    font-size: 4rem;
    font-weight: 300;
    letter-spacing: -.03em;
    line-height: 1.05;
    font-variant-numeric: tabular-nums;
}
.result-verdict { display: flex; align-items: center; gap: 8px; margin-top: 6px; color: var(--muted); }
.result-verdict::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.result-summary.passed .result-verdict::before { background: var(--ok); }
.result-summary.failed .result-verdict::before { background: var(--bad); }
.stat-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    margin: 22px 0;
}
.stat { background: var(--tile); border-radius: var(--radius); padding: 14px 16px; }
.stat-value { font-size: 1.6rem; font-weight: 300; letter-spacing: -.02em; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat-label { font-size: .82rem; color: var(--muted); }

.cat-perf { display: flex; flex-direction: column; gap: 14px; }
.cat-perf-head { display: flex; justify-content: space-between; gap: 12px; font-size: .95rem; margin-bottom: 6px; }

.review-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin: 32px 0 12px;
}
.review-head h2 { font-size: 1.4rem; display: flex; align-items: center; gap: 8px; }
.review-head .select { width: auto; min-height: 40px; background: var(--surface); }
.review-card { margin-bottom: 12px; padding: 18px; }
.review-meta { display: flex; align-items: center; gap: 12px; font-size: .85rem; color: var(--muted); margin-bottom: 8px; }
.badge { display: inline-flex; align-items: center; gap: 6px; color: var(--ink); }
.badge::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: #c9c4b9; }
.badge-ok::before { background: var(--ok); }
.badge-bad::before { background: var(--bad); }
.badge-none::before { background: var(--warn); }
.review-flag { color: var(--warn); display: none; }
.review-flag .icon { fill: currentColor; }
.review-card[data-flagged="true"] .review-flag { display: inline-flex; }
.speak-line { display: flex; align-items: flex-start; gap: 6px; }
.speak-line > span, .speak-line > p { flex: 1; }
.review-question { font-size: 1.15rem; line-height: 1.35; margin-bottom: 12px; }
.review-media { margin-bottom: 12px; text-align: center; background: var(--tile); border-radius: var(--radius); overflow: hidden; }
.review-media img, .review-media video { display: block; margin: 0 auto; max-height: 300px; object-fit: contain; }
.review-media img { cursor: zoom-in; }
.review-answer {
    align-items: center;
    padding-block: 4px;
    padding-inline: 14px 6px;
    border-radius: 10px;
    background: var(--tile);
    margin-bottom: 6px;
}
.review-answer.correct { background: var(--ok-soft); }
.review-answer.incorrect { background: var(--bad-soft); }
.review-answer .tag { font-size: .8rem; font-weight: 500; flex: none !important; }
.review-answer.correct .tag { color: var(--ok); }
.review-answer.incorrect .tag { color: var(--bad); }
.review-explanation { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); font-size: .95rem; color: var(--muted); }
.review-explanation strong { font-weight: 500; color: var(--ink); }

/* ---- Wider screens ------------------------------------------------------ */
@media (min-width: 640px) {
    .page { padding: 28px 24px 64px; }
    .topbar-inner { padding-inline: 24px; }
    .page-title { font-size: 2.4rem; }
    .cat-grid { grid-template-columns: repeat(2, 1fr); }
    .stat-row { grid-template-columns: repeat(4, 1fr); }
    .quizbar { padding: 10px 20px; gap: 10px; }
    .progress { top: 56px; }
    .quiz-page { padding-bottom: calc(84px + env(safe-area-inset-bottom)); }
    .quiz-main { padding: 12px 12px 20px; }
    .quiz-actions { padding: 10px 12px calc(12px + env(safe-area-inset-bottom)); }
    .q-media { margin: 10px 10px 0; }
    .q-media img, .q-media video { max-height: min(34vh, 300px); }
    .q-body { padding: 26px; }
    .q-count { display: block; }
    .flag-btn { padding: 6px 12px; }
    .flag-btn span { display: inline; }
    .feedback { padding: 22px 26px; scroll-margin-bottom: 100px; }
    .q-text-row { gap: 8px; margin-bottom: 18px; }
    .q-text { font-size: 1.7rem; }
    .speak { width: 38px; height: 38px; }
    .answers { gap: 8px; }
    .answer-main { min-height: 54px; padding: 10px 14px; font-size: 1.02rem; }
    .answer .speak { margin-inline-end: 6px; }
    .btn-grow { flex: 0 1 240px; margin-inline-start: auto; }
    .sheet {
        inset: 0;
        margin: auto;
        width: min(640px, 92vw);
        max-height: 80vh;
        border-radius: 20px;
    }
}
@media (min-width: 960px) {
    .cat-grid { grid-template-columns: repeat(3, 1fr); }
}
/* Desktop quiz: picture beside the question, like the real exam screen */
@media (min-width: 1024px) {
    .quiz-main { max-width: 1120px; }
    .quiz-actions-inner { max-width: 1096px; }
    .qcard.has-media { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
    .qcard.has-media .q-media { margin: 10px; margin-inline-end: 0; align-items: center; }
    .qcard.has-media .q-media img,
    .qcard.has-media .q-media video { max-height: calc(100vh - 220px); }
    .qcard:not(.has-media) .q-body { max-width: 760px; margin: 0 auto; padding-block: 40px; }
}

@media print {
    .topbar, .no-print, .speak { display: none !important; }
    body { background: #fff; }
    .card { break-inside: avoid; }
    .review-card { display: block !important; }
}
