/* Base: fonts, design tokens (light and dark), reset, type, buttons, cards, forms, badges, chips, alerts, toasts,
   progress bars, tables and skeletons. Every other file uses only these tokens. Guide: ui/README.md. */

/* Fonts: self-hosted woff2 from @fontsource 5.0.8 (SIL Open Font License, see fonts/LICENSE-*). The Latin files are cut down
   to Basic Latin, Latin-1 and the punctuation the site uses (fontTools subset), so each is about 12 KB. */
@font-face {
    font-family: "Readex Pro";
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url("../fonts/readex-pro-arabic-400-normal.woff2") format("woff2");
    unicode-range: U+0600-06FF, U+0750-077F, U+0870-088E, U+0890-0891, U+0898-08E1, U+08E3-08FF, U+200C-200E, U+2010-2011, U+204F, U+2E41, U+FB50-FDFF, U+FE70-FE74, U+FE76-FEFC;
}
@font-face {
    font-family: "Readex Pro";
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url("../fonts/readex-pro-latin-400-normal.woff2") format("woff2");
    unicode-range: U+0020-007E, U+00A0-00FF, U+2010-2027, U+2066-2069, U+2212;
}
@font-face {
    font-family: "Readex Pro";
    font-style: normal;
    font-weight: 500;
    font-display: swap;
    src: url("../fonts/readex-pro-arabic-500-normal.woff2") format("woff2");
    unicode-range: U+0600-06FF, U+0750-077F, U+0870-088E, U+0890-0891, U+0898-08E1, U+08E3-08FF, U+200C-200E, U+2010-2011, U+204F, U+2E41, U+FB50-FDFF, U+FE70-FE74, U+FE76-FEFC;
}
@font-face {
    font-family: "Readex Pro";
    font-style: normal;
    font-weight: 500;
    font-display: swap;
    src: url("../fonts/readex-pro-latin-500-normal.woff2") format("woff2");
    unicode-range: U+0020-007E, U+00A0-00FF, U+2010-2027, U+2066-2069, U+2212;
}
@font-face {
    font-family: "Readex Pro";
    font-style: normal;
    font-weight: 700;
    font-display: swap;
    src: url("../fonts/readex-pro-arabic-700-normal.woff2") format("woff2");
    unicode-range: U+0600-06FF, U+0750-077F, U+0870-088E, U+0890-0891, U+0898-08E1, U+08E3-08FF, U+200C-200E, U+2010-2011, U+204F, U+2E41, U+FB50-FDFF, U+FE70-FE74, U+FE76-FEFC;
}
@font-face {
    font-family: "Readex Pro";
    font-style: normal;
    font-weight: 700;
    font-display: swap;
    src: url("../fonts/readex-pro-latin-700-normal.woff2") format("woff2");
    unicode-range: U+0020-007E, U+00A0-00FF, U+2010-2027, U+2066-2069, U+2212;
}
@font-face {
    font-family: "Baloo Bhaijaan 2";
    font-style: normal;
    font-weight: 700;
    font-display: swap;
    src: url("../fonts/baloo-bhaijaan-2-arabic-700-normal.woff2") format("woff2");
    unicode-range: U+0600-06FF, U+0750-077F, U+0870-088E, U+0890-0891, U+0898-08E1, U+08E3-08FF, U+200C-200E, U+2010-2011, U+204F, U+2E41, U+FB50-FDFF, U+FE70-FE74, U+FE76-FEFC;
}
@font-face {
    font-family: "Baloo Bhaijaan 2";
    font-style: normal;
    font-weight: 700;
    font-display: swap;
    src: url("../fonts/baloo-bhaijaan-2-latin-700-normal.woff2") format("woff2");
    unicode-range: U+0020-007E, U+00A0-00FF, U+2010-2027, U+2066-2069, U+2212;
}

/* ---------- Tokens: light theme (default) ---------- */
:root {
    color-scheme: light;

    /* Brand: fixed in both themes. */
    --navy: #17243A;
    --turquoise: #20B8A6;
    --amber: #FFBF47;

    /* Surfaces */
    --bg: #EEF3F9;
    --bg-dots: rgba(23, 36, 58, .07);
    --bg-glow-1: rgba(32, 184, 166, .16);
    --bg-glow-2: rgba(255, 191, 71, .18);
    --surface: #FFFFFF;
    --surface-2: #F5F8FC;
    --surface-3: #E4EBF4;
    --console: #17243A;          /* header, tab bar, video slot: dark in both themes */
    --console-2: #22334F;
    --on-console: #FFFFFF;
    --on-console-muted: #B9C6D8;
    --console-edge: #050A14;

    /* Text */
    --text: #17243A;
    --text-muted: #4E5D73;
    --text-soft: #6A788C;        /* large text and icons only */
    --link: #0E7A6E;
    --accent-text: #8A5A00;      /* amber meaning as text */

    /* Lines and the 3D edge under cards and buttons */
    --border: #D2DCE8;
    --border-strong: #AEBCCD;
    --edge: #C5D1DF;

    /* Actions */
    --primary: #20B8A6;
    --primary-edge: #12877A;
    --primary-soft: #DDF5F1;
    --on-primary: #0F1A2B;
    --accent: #FFBF47;
    --accent-edge: #D3922A;
    --accent-soft: #FFF2D6;
    --on-accent: #17243A;

    /* States */
    --success: #137A3A;
    --success-soft: #E2F5E8;
    --success-edge: #9CD6B0;
    --danger: #C62828;
    --danger-soft: #FDEBEB;
    --danger-edge: #F0B4B4;
    --info: #0E7A6E;
    --info-soft: #DDF5F1;

    /* Game */
    --xp-track: #DCE4EE;
    --xp-fill: linear-gradient(90deg, #4FE3CF, #20B8A6 60%, #17A08F);
    --gold: #FFBF47;
    --gold-light: #FFE3A3;
    --gold-deep: #F4A62A;
    --flame: #FF8A1F;
    /* Pair colours of the match game and the confetti: light fills with navy text, same in both themes. */
    --candy-blue: #8FB4FF;
    --candy-pink: #F59ABF;
    --candy-green: #9BE07A;
    --silver: #C9D3DF;
    --bronze: #E3A06A;

    /* Depth */
    --shadow-1: 0 1px 2px rgba(23, 36, 58, .06), 0 4px 0 var(--edge);
    --shadow-2: 0 2px 4px rgba(23, 36, 58, .06), 0 14px 30px -14px rgba(23, 36, 58, .35);
    --glow: 0 0 0 1px rgba(32, 184, 166, .35), 0 12px 32px -12px rgba(18, 135, 122, .55);
    --glow-accent: 0 0 0 1px rgba(255, 191, 71, .55), 0 12px 32px -12px rgba(211, 146, 42, .6);
    --focus-ring: 0 0 0 3px var(--surface), 0 0 0 6px #0E7A6E;

    /* Shape and space */
    --radius-sm: 10px;
    --radius: 16px;
    --radius-lg: 24px;
    --radius-pill: 999px;
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;
    --space-7: 48px;

    /* Type */
    --font-text: "Readex Pro", system-ui, sans-serif;
    --font-title: "Baloo Bhaijaan 2", "Readex Pro", system-ui, sans-serif;
    --font-code: ui-monospace, "Cascadia Mono", Consolas, monospace;
    --fs-xs: .8rem;
    --fs-sm: .9rem;
    --fs-base: 1rem;
    --fs-md: 1.125rem;
    --fs-lg: 1.35rem;
    --fs-xl: clamp(1.6rem, 4.5vw, 2rem);
    --fs-2xl: clamp(2rem, 6.5vw, 3rem);
    --fs-display: clamp(2.2rem, 8vw, 3.75rem);

    /* Motion */
    --ease-out: cubic-bezier(.2, .8, .2, 1);
    --ease-spring: cubic-bezier(.34, 1.56, .64, 1);
    --tabbar-h: 64px;
}

/* ---------- Tokens: dark theme (follows the phone) ---------- */
@media (prefers-color-scheme: dark) {
    :root {
        color-scheme: dark;

        --bg: #0A1220;
        --bg-dots: rgba(255, 255, 255, .05);
        --bg-glow-1: rgba(32, 184, 166, .2);
        --bg-glow-2: rgba(255, 191, 71, .1);
        --surface: #142037;
        --surface-2: #1A2943;
        --surface-3: #243755;
        --console: #0F1A2E;
        --console-2: #1A2943;

        --text: #EEF3FA;
        --text-muted: #A7B4C8;
        --text-soft: #8797AE;
        --link: #5FDCCB;
        --accent-text: #FFBF47;

        --border: #273A5A;
        --border-strong: #3A5079;
        --edge: #060C17;

        --primary-edge: #0C6B60;
        --primary-soft: rgba(32, 184, 166, .16);
        --on-primary: #071A17;
        --accent-edge: #A86F12;
        --accent-soft: #3A2D10;

        --success: #4ADE80;
        --success-soft: rgba(74, 222, 128, .13);
        --success-edge: rgba(74, 222, 128, .4);
        --danger: #FF9E9E;
        --danger-soft: rgba(255, 128, 128, .13);
        --danger-edge: rgba(255, 128, 128, .4);
        --info: #5FDCCB;
        --info-soft: rgba(32, 184, 166, .16);

        --xp-track: #0B1424;
        --silver: #AFC0D6;
        --bronze: #D98E55;

        --shadow-1: 0 1px 0 rgba(255, 255, 255, .04) inset, 0 4px 0 var(--edge);
        --shadow-2: 0 18px 40px -18px rgba(0, 0, 0, .8);
        --glow: 0 0 0 1px rgba(32, 184, 166, .5), 0 0 28px -4px rgba(32, 184, 166, .45);
        --glow-accent: 0 0 0 1px rgba(255, 191, 71, .55), 0 0 28px -4px rgba(255, 191, 71, .4);
        --focus-ring: 0 0 0 3px var(--bg), 0 0 0 6px #5FDCCB;
    }
}

/* ---------- Reset and page ---------- */
*, *::before, *::after { box-sizing: border-box; }
/* "hidden" always wins over a class that sets display (parts that only JavaScript shows). */
[hidden] { display: none !important; }

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

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    background-color: var(--bg);
    background-image:
        radial-gradient(60rem 30rem at 85% -10%, var(--bg-glow-1), transparent 60%),
        radial-gradient(40rem 26rem at 0% 20%, var(--bg-glow-2), transparent 60%),
        radial-gradient(var(--bg-dots) 1.2px, transparent 1.4px);
    background-size: 100% 100%, 100% 100%, 22px 22px;
    background-repeat: no-repeat, no-repeat, repeat;
    color: var(--text);
    font-family: var(--font-text);
    font-size: var(--fs-base);
    line-height: 1.7;
    overflow-x: hidden;
    -webkit-tap-highlight-color: transparent;
}

h1, h2, h3, h4 {
    font-family: var(--font-title);
    font-weight: 700;
    line-height: 1.3;
    margin: 0 0 .5em;
    text-wrap: balance;
}
h1 { font-size: var(--fs-xl); }
h2 { font-size: var(--fs-lg); }
h3 { font-size: var(--fs-md); }
p { margin: 0 0 1em; }

a { color: var(--link); text-underline-offset: 3px; }
a:hover { text-decoration-thickness: 2px; }

:focus-visible { outline: none; box-shadow: var(--focus-ring); border-radius: 6px; }

img, svg { max-width: 100%; }

code, pre, .ltr-code {
    font-family: var(--font-code);
    direction: ltr;
    text-align: left;
    unicode-bidi: isolate;
}

::selection { background: var(--amber); color: var(--navy); }

/* Numbers in game labels: the title font, Western digits as the rest of the site. */
.num { font-family: var(--font-title); font-variant-numeric: tabular-nums; }

.muted { color: var(--text-muted); }
.hint { font-size: var(--fs-sm); color: var(--text-muted); }
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
}

.prose { max-width: 720px; margin: 0 auto; }
.prose ul { padding-inline-start: 1.2em; }
.actions { display: flex; flex-wrap: wrap; gap: var(--space-3); margin-top: var(--space-4); }
.stack { display: grid; gap: var(--space-4); }

/* ---------- Buttons: chunky, with a 3D edge that goes down when pressed ---------- */
.btn {
    --btn-bg: var(--surface);
    --btn-fg: var(--text);
    --btn-border: var(--border-strong);
    --btn-edge: var(--edge);
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: 48px;
    padding: 8px 22px;
    border: 2px solid var(--btn-border);
    border-radius: var(--radius);
    background: var(--btn-bg);
    color: var(--btn-fg);
    box-shadow: 0 4px 0 var(--btn-edge);
    font-family: var(--font-title);
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1.2;
    text-align: center;
    text-decoration: none;
    cursor: pointer;
    user-select: none;
    transition: transform .12s var(--ease-out);
}
.btn:hover { transform: translateY(-2px); text-decoration: none; }
.btn:active, .btn.is-pressed { transform: translateY(4px); box-shadow: 0 0 0 var(--btn-edge); transition-duration: .04s; }
.btn:focus-visible { box-shadow: 0 4px 0 var(--btn-edge), var(--focus-ring); border-radius: var(--radius); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; transform: none; }

.btn-primary { --btn-bg: var(--primary); --btn-fg: var(--on-primary); --btn-border: var(--primary-edge); --btn-edge: var(--primary-edge); }
.btn-accent { --btn-bg: var(--accent); --btn-fg: var(--on-accent); --btn-border: var(--accent-edge); --btn-edge: var(--accent-edge); }
.btn-secondary { --btn-bg: var(--surface); --btn-fg: var(--text); }
.btn-danger { --btn-bg: var(--surface); --btn-fg: var(--danger); --btn-border: var(--danger-edge); --btn-edge: var(--danger-edge); }
.btn-ghost { --btn-bg: transparent; --btn-border: transparent; --btn-edge: transparent; box-shadow: none; }
.btn-ghost:hover { background: var(--surface-3); }
.btn-on-dark { --btn-fg: var(--on-console); }
.btn-on-dark:hover { background: rgba(255, 255, 255, .1); }
.btn-small { min-height: 44px; padding: 6px 16px; font-size: .95rem; border-radius: var(--radius-sm); }
.btn-lg { min-height: 56px; padding: 10px 28px; font-size: 1.2rem; border-radius: 18px; }
.btn-block { display: flex; width: 100%; }
.btn-icon { padding-inline: 12px; }
.btn .icon { flex: none; }
/* Kept from before: a form's own submit button fills the form. */
form .btn[type="submit"] { width: 100%; }
form .btn.btn-icon[type="submit"], .inline-form .btn[type="submit"] { width: auto; }

.inline-form { display: inline; margin: 0; }
.link-button { background: none; border: 0; padding: 0; font: inherit; color: var(--link); cursor: pointer; }

/* ---------- Cards ---------- */
.card {
    position: relative;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 20px 18px;
    box-shadow: var(--shadow-1);
}
.card > :last-child { margin-bottom: 0; }
.card-glow { border-color: color-mix(in srgb, var(--primary) 70%, var(--border)); box-shadow: var(--glow), 0 4px 0 var(--primary-edge); }
.card-accent { border-color: var(--accent); box-shadow: var(--glow-accent), 0 4px 0 var(--accent-edge); }
.card-link { display: block; color: inherit; text-decoration: none; transition: transform .15s var(--ease-out); }
.card-link:hover { transform: translateY(-3px); border-color: var(--primary); }
.card-link:active { transform: translateY(2px); }
.form-card { max-width: 480px; margin: 0 auto; }
.wide-card, .form-card.wide { max-width: 720px; margin: 0 auto var(--space-4); }

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 18px; }
.field label { font-weight: 500; }
/* :where keeps this rule light, so the error and focus rules below win over it. */
.field :where(input:not([type="checkbox"], [type="radio"]), select, textarea) {
    width: 100%;
    min-height: 52px;
    padding: 10px 14px;
    border: 2px solid var(--border);
    border-radius: 14px;
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
}
.field input::placeholder { color: var(--text-soft); }
.field input:hover, .field select:hover, .field textarea:hover { border-color: var(--border-strong); }
.field input:focus, .field select:focus, .field textarea:focus {
    outline: none;
    border-color: var(--primary);
    background: var(--surface);
    box-shadow: 0 0 0 4px color-mix(in srgb, var(--primary) 25%, transparent);
}
.field input[dir="ltr"] { text-align: left; }
.field input[aria-invalid="true"], .field:has(.field-error) :is(input, select, textarea) { border-color: var(--danger); background: var(--danger-soft); }
.field.check { flex-direction: row; flex-wrap: wrap; align-items: center; gap: 10px; min-height: 44px; }
.field.check input { width: 24px; height: 24px; accent-color: var(--primary); flex: none; }
.field.check label { flex: 1; min-width: 0; padding-block: 10px; cursor: pointer; }
.field.check .field-error { flex-basis: 100%; }
.field-error { font-size: var(--fs-sm); color: var(--danger); font-weight: 500; }
.field-error::before { content: "! "; font-family: var(--font-title); }

/* ---------- Badges (small labels) and chips (bigger, game labels) ---------- */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 10px;
    border-radius: var(--radius-pill);
    background: var(--surface-3);
    color: var(--text-muted);
    font-size: var(--fs-xs);
    font-weight: 500;
    line-height: 1.6;
    white-space: nowrap;
}
.badge-ok { background: var(--success-soft); color: var(--success); }
.badge-accent { background: var(--accent-soft); color: var(--accent-text); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-primary { background: var(--primary-soft); color: var(--link); }

.chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 32px;
    padding: 2px 14px;
    border: 2px solid var(--border);
    border-radius: var(--radius-pill);
    background: var(--surface);
    color: var(--text);
    font-family: var(--font-title);
    font-size: .95rem;
    line-height: 1.4;
    white-space: nowrap;
}
.chip-accent { background: var(--accent); border-color: var(--accent-edge); color: var(--on-accent); }
.chip-primary { background: var(--primary); border-color: var(--primary-edge); color: var(--on-primary); }
.chip-small { min-height: 24px; padding: 0 10px; font-size: .8rem; border-width: 1.5px; }
.chip-tilt { transform: rotate(-3deg); box-shadow: 0 3px 0 var(--edge); }

/* Kept from before (admin and quiz pages). */
.tag { display: inline-block; max-width: 100%; padding: 0 8px; border-radius: var(--radius-pill); background: var(--accent); color: var(--on-accent); font-size: var(--fs-xs); }
.tag-ok { background: var(--primary); color: var(--on-primary); }

/* ---------- Alerts, notices and toasts ---------- */
.alert, .notice {
    padding: 12px 16px;
    border: 2px solid var(--danger-edge);
    border-radius: 14px;
    background: var(--danger-soft);
    color: var(--danger);
    font-weight: 500;
}
.notice { border-color: color-mix(in srgb, var(--primary) 45%, transparent); background: var(--info-soft); color: var(--info); }
.alert-success { border-color: var(--success-edge); background: var(--success-soft); color: var(--success); }

/* A toast is a short message from the server ("لفل جديد!"). Put it in .toast-stack; it comes in and goes after 5 s. */
.toast-stack {
    position: fixed;
    inset-inline: 16px;
    bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
    z-index: 60;
    display: grid;
    justify-items: center;
    gap: var(--space-2);
    pointer-events: none;
}
.toast {
    display: flex;
    align-items: center;
    gap: 10px;
    max-width: 420px;
    padding: 12px 18px;
    border: 2px solid var(--border-strong);
    border-radius: var(--radius);
    background: var(--console);
    color: var(--on-console);
    box-shadow: var(--shadow-2);
    font-family: var(--font-title);
    font-size: 1.05rem;
    animation: toast-in .45s var(--ease-spring) both, toast-out .4s ease-in 5s forwards;
}
.toast-success { border-color: var(--primary); }
.toast-accent { border-color: var(--accent); }
.toast-danger { border-color: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(24px) scale(.9); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(12px); visibility: hidden; } }

/* ---------- Progress bars (plain) ---------- */
.progress-bar {
    position: relative;
    display: block;
    height: 12px;
    overflow: hidden;
    border-radius: var(--radius-pill);
    background: var(--xp-track);
}
.progress-fill {
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background: var(--primary);
    transform-origin: 100% 50%;   /* RTL: fills from the right */
    transform: scaleX(var(--p, 0));
    animation: bar-fill 1s var(--ease-out) both;
}
.progress-label { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; font-size: var(--fs-sm); color: var(--text-muted); }
@keyframes bar-fill { from { transform: scaleX(0); } }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; background: var(--surface); border: 2px solid var(--border); border-radius: var(--radius); }
.table, .data-table { width: 100%; border-collapse: collapse; font-size: .95rem; }
.table th, .table td, .data-table th, .data-table td { padding: 10px; border-bottom: 1px solid var(--border); text-align: start; vertical-align: top; }
.table th, .data-table th { background: var(--surface-2); color: var(--text-muted); font-weight: 500; white-space: nowrap; }
.data-table th, .data-table td { white-space: nowrap; font-size: .9rem; }
.table tr:last-child td, .data-table tr:last-child td { border-bottom: 0; }
.ltr-cell { text-align: left; font-family: var(--font-code); }

/* Tabs as pills (leaderboard: all students / my school) */
.tabs { display: flex; flex-wrap: wrap; gap: var(--space-2); margin: 0 0 var(--space-4); }
.tabs a {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    padding: 6px 18px;
    border: 2px solid var(--border);
    border-radius: var(--radius-pill);
    background: var(--surface);
    color: var(--text);
    font-family: var(--font-title);
    text-decoration: none;
}
.tabs a.active { background: var(--console); border-color: var(--console); color: var(--on-console); box-shadow: 0 3px 0 var(--primary-edge); }

/* ---------- Skeleton (content that is still loading) ---------- */
.skeleton {
    position: relative;
    display: block;
    overflow: hidden;
    min-height: 1em;
    border-radius: var(--radius-sm);
    background: var(--surface-3);
}
.skeleton::after {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .35), transparent);
    transform: translateX(100%);
    animation: shimmer 1.4s infinite;
}
.skeleton-text { height: 1em; margin: .35em 0; }
.skeleton-text.short { width: 60%; }
.skeleton-circle { width: 48px; height: 48px; border-radius: 50%; }
.skeleton-card { height: 120px; border-radius: var(--radius-lg); }
@keyframes shimmer { to { transform: translateX(-100%); } }

/* ---------- Reduced motion: the same information, no movement ---------- */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0s !important;
        animation-delay: 0s !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0s !important;
        scroll-behavior: auto !important;
    }
    /* A toast cannot leave without motion, so it stays in the page flow instead of covering it. */
    .toast-stack { position: static; margin: var(--space-4) auto 0; }
    .toast { animation: none !important; }
}
