/* ==========================================================
   Math Tutor - one stylesheet, works on phone, tablet, PC
   and prints clean exam papers.
   ========================================================== */
:root {
    --navy: #1e3a8a;
    --navy-2: #172554;
    --blue: #2563eb;
    --blue-soft: #eff4ff;
    --gold: #f59e0b;
    --ink: #111827;
    --muted: #6b7280;
    --line: #e5e7eb;
    --bg: #f6f7fb;
    --card: #ffffff;
    --ok: #15803d;
    --ok-soft: #ecfdf3;
    --err: #b91c1c;
    --err-soft: #fef2f2;
    --warn: #b45309;
    --radius: 12px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
    --sidebar: 260px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 16px;
    line-height: 1.55;
    color: var(--ink);
    background: var(--bg);
    -webkit-text-size-adjust: 100%;
}
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; line-height: 1.25; margin: 0 0 .25rem; }
h2 { font-size: 1.2rem; margin: 0 0 .75rem; }
h3 { font-size: 1.05rem; margin: 0 0 .2rem; }
p { margin: 0 0 .6rem; }
code { background: #f1f5f9; padding: .1rem .35rem; border-radius: 4px; font-size: .9em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.text-error { color: var(--err); }
.text-ok { color: var(--ok); }
.text-warn { color: var(--warn); }
h1:focus { outline: none; }

/* ---------- App shell ---------- */
.shell { min-height: 100vh; }

.sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: var(--sidebar);
    background: linear-gradient(180deg, var(--navy) 0%, var(--navy-2) 100%);
    color: #e0e7ff;
    overflow-y: auto;
    z-index: 30;
    transition: transform .25s ease;
}
.brand {
    display: flex; align-items: center; gap: .6rem;
    padding: 1.1rem 1.25rem;
    color: #fff; font-weight: 700; font-size: 1.15rem;
    border-bottom: 1px solid rgba(255, 255, 255, .1);
}
.brand:hover { text-decoration: none; }
.brand-logo {
    display: inline-grid; place-items: center;
    width: 34px; height: 34px; border-radius: 9px;
    background: var(--gold); color: var(--navy-2);
    font-family: Georgia, serif; font-size: 1.3rem;
}

.nav { padding: .75rem .6rem 2rem; display: flex; flex-direction: column; gap: 2px; }
.nav-group {
    margin: 1rem .65rem .3rem;
    font-size: .72rem; letter-spacing: .08em; text-transform: uppercase;
    color: #a5b4fc;
}
.nav-link {
    display: flex; align-items: center; gap: .6rem;
    padding: .55rem .75rem; border-radius: 8px;
    color: #e0e7ff; font-size: .95rem;
}
.nav-link:hover { background: rgba(255, 255, 255, .08); text-decoration: none; }
.nav-link.active { background: rgba(255, 255, 255, .16); color: #fff; font-weight: 600; }
.nav-icon { width: 1.2rem; text-align: center; opacity: .9; }
.nav-sub { flex-direction: column; align-items: flex-start; gap: 0; padding-left: 2.55rem; }
.nav-sub small { color: #a5b4fc; font-size: .75rem; }
.nav-book { line-height: 1.3; }
.version-box { margin: 0 1.25rem 1.5rem; padding-top: .75rem; border-top: 1px solid rgba(255,255,255,.1); font-size: .75rem; color: #a5b4fc; line-height: 1.6; }
.version-warn { margin-top: .4rem; padding: .4rem .5rem; border-radius: 6px; background: rgba(251, 191, 36, .15); color: #fde68a; }
.nav-note { padding: .3rem .75rem .3rem 2.55rem; font-size: .8rem; color: #a5b4fc; }

.main { margin-left: var(--sidebar); min-height: 100vh; display: flex; flex-direction: column; }
.topbar { display: none; }
.content { width: 100%; max-width: 1100px; margin: 0 auto; padding: 1.75rem 2rem 4rem; }
.backdrop { display: none; }

/* ---------- Buttons, chips, alerts ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
    padding: .55rem 1rem; min-height: 40px;
    border: 1px solid var(--line); border-radius: 9px;
    background: #fff; color: var(--ink);
    font: inherit; font-size: .95rem; font-weight: 500;
    cursor: pointer; white-space: nowrap;
}
.btn:hover { background: #f9fafb; text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn-primary:hover { background: #1d4ed8; }
.btn-danger { color: var(--err); border-color: #fecaca; }
.btn-danger:hover { background: var(--err-soft); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--blue); padding-left: 0; }
.btn-ghost:hover { background: transparent; text-decoration: underline; }
.btn-sm { padding: .35rem .75rem; min-height: 34px; font-size: .88rem; }
.btn-lg { padding: .75rem 1.3rem; font-size: 1rem; }
.icon-btn {
    width: 42px; height: 42px; border: 0; border-radius: 10px;
    background: transparent; font-size: 1.35rem; cursor: pointer; color: var(--navy);
}
.icon-btn:hover { background: var(--blue-soft); }

.chips { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .4rem; }
.chip {
    display: inline-block; padding: .12rem .55rem; border-radius: 999px;
    background: var(--blue-soft); color: var(--navy); font-size: .78rem; font-weight: 500;
}
.chip-muted { background: #f3f4f6; color: var(--muted); }

.badge { padding: .1rem .5rem; border-radius: 6px; font-size: .74rem; font-weight: 700; letter-spacing: .02em; }
.badge-mcq { background: #fef3c7; color: #92400e; }
.badge-short { background: #dbeafe; color: #1e40af; }
.badge-long { background: #ede9fe; color: #5b21b6; }

.alert { padding: .85rem 1rem; border-radius: 10px; margin: 0 0 1rem; }
.alert-error { background: var(--err-soft); color: var(--err); border: 1px solid #fecaca; }

.loading { display: flex; align-items: center; gap: .6rem; color: var(--muted); padding: 2rem 0; }
.spinner {
    width: 18px; height: 18px; border-radius: 50%;
    border: 3px solid #dbeafe; border-top-color: var(--blue);
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Page pieces ---------- */
.page-head {
    display: flex; align-items: flex-start; justify-content: space-between;
    gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem;
}
.crumbs { font-size: .88rem; color: var(--muted); margin-bottom: .6rem; display: flex; flex-wrap: wrap; gap: .35rem; }
.section-title { margin-top: 2rem; }

.hero {
    background: linear-gradient(135deg, var(--navy) 0%, #2b4fb8 100%);
    color: #fff; border-radius: 16px; padding: 2rem;
    margin-bottom: 1.5rem;
}
.hero h1 { font-size: 2rem; }
.hero p { color: #dbe4ff; max-width: 640px; }
.hero .actions { display: flex; flex-wrap: wrap; gap: .6rem; margin-top: 1rem; }
.hero .btn:not(.btn-primary) { background: rgba(255, 255, 255, .12); border-color: rgba(255, 255, 255, .3); color: #fff; }
.hero .btn-primary { background: var(--gold); border-color: var(--gold); color: var(--navy-2); }

.steps { display: grid; grid-template-columns: repeat(3, 1fr); gap: .75rem; margin-top: 2rem; }
.step { display: flex; gap: .7rem; align-items: flex-start; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; }
.step b {
    flex: none; display: grid; place-items: center; width: 28px; height: 28px;
    border-radius: 50%; background: var(--blue-soft); color: var(--navy);
}

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1rem; }
.book-card { display: flex; gap: 1rem; padding: 1rem; color: inherit; transition: transform .12s, box-shadow .12s; }
.book-card:hover { text-decoration: none; transform: translateY(-2px); box-shadow: 0 6px 18px rgba(30, 58, 138, .12); }
.book-cover {
    flex: none; width: 62px; height: 82px; border-radius: 8px;
    display: grid; place-items: center;
    background: linear-gradient(160deg, var(--navy), var(--blue));
    color: var(--gold); font-family: Georgia, serif; font-size: 2rem; font-weight: 700;
}
.book-info { min-width: 0; }
.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.panel {
    background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
    padding: 1.25rem; margin: 1.25rem 0; box-shadow: var(--shadow);
}
details.panel > summary { cursor: pointer; font-weight: 600; color: var(--navy); list-style: none; }
details.panel > summary::-webkit-details-marker { display: none; }
details.panel[open] > summary { margin-bottom: 1rem; }

/* ---------- Forms ---------- */
label { display: flex; flex-direction: column; gap: .3rem; font-size: .9rem; font-weight: 500; }
input[type=text], input[type=number], input:not([type]), select {
    width: 100%; min-height: 42px; padding: .5rem .7rem;
    border: 1px solid #d1d5db; border-radius: 9px;
    font: inherit; font-size: 1rem; background: #fff; color: var(--ink);
}
input:focus, select:focus { outline: 2px solid #bfdbfe; border-color: var(--blue); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 1rem; }
.form-grid .span-2 { grid-column: span 2; }
.form-actions { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; margin: 1rem 0 .5rem; }
.validation-message { color: var(--err); font-size: .8rem; font-weight: 400; }
.invalid { border-color: var(--err) !important; }
.block-label { margin-top: 1rem; }

.check { flex-direction: row; align-items: center; gap: .6rem; font-weight: 400; cursor: pointer; padding: .45rem .2rem; }
.check input { width: 20px; height: 20px; accent-color: var(--blue); flex: none; }
.check small { margin-left: auto; }
.check-strong { font-weight: 600; }
.check.is-disabled { opacity: .5; cursor: default; }
.switch { flex-direction: row; align-items: center; gap: .5rem; font-weight: 400; cursor: pointer; }
.switch input { width: 20px; height: 20px; accent-color: var(--blue); }

/* ---------- Book page ---------- */
.unit-list { display: flex; flex-direction: column; gap: .6rem; }
.unit-card { display: flex; align-items: center; gap: 1rem; padding: .85rem 1rem; }
.unit-no {
    flex: none; width: 42px; height: 42px; border-radius: 10px;
    display: grid; place-items: center;
    background: var(--blue-soft); color: var(--navy); font-weight: 700; font-size: 1.1rem;
}
.unit-info { flex: 1; min-width: 0; }
.unit-title { font-weight: 600; color: var(--ink); font-size: 1.02rem; }
.unit-actions { display: flex; gap: .4rem; flex: none; }

/* ---------- Unit page ---------- */
.tabs {
    display: flex; gap: .35rem; overflow-x: auto; padding-bottom: .4rem; margin-bottom: .75rem;
    scrollbar-width: thin;
}
.tab {
    flex: none; border: 1px solid var(--line); background: #fff; border-radius: 999px;
    padding: .4rem .9rem; font: inherit; font-size: .9rem; cursor: pointer; color: var(--ink);
}
.tab small { color: var(--muted); margin-left: .2rem; }
.tab.active { background: var(--navy); border-color: var(--navy); color: #fff; }
.tab.active small { color: #c7d2fe; }
.toolbar { display: flex; flex-wrap: wrap; gap: .75rem 1.25rem; align-items: center; margin-bottom: 1rem; }
.toolbar select { width: auto; min-width: 180px; }
.ex-title { font-weight: 600; color: var(--navy); }

.q-list { display: flex; flex-direction: column; gap: .8rem; }
.q-card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.15rem; box-shadow: var(--shadow); }
.q-head { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; margin-bottom: .5rem; }
.q-label { font-weight: 600; color: var(--navy); font-size: .9rem; }
.q-marks { margin-left: auto; color: var(--muted); font-size: .8rem; }
.q-body { margin-bottom: .4rem; }

.opt-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .4rem .75rem; margin: .5rem 0; }
.opt { display: flex; gap: .45rem; align-items: baseline; padding: .35rem .55rem; border-radius: 8px; border: 1px solid transparent; }
.opt.correct { background: var(--ok-soft); border-color: #bbf7d0; color: var(--ok); font-weight: 600; }

.solution { margin-top: .6rem; padding: .85rem 1rem; background: #f8fafc; border-left: 4px solid var(--ok); border-radius: 8px; }
.sol-source { font-size: .78rem; color: var(--muted); margin-bottom: .3rem; }
.answer { margin-bottom: .4rem; }
.sol-title { font-size: .78rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--ok); margin: .35rem 0 .25rem; }
.sol-body { white-space: pre-line; }

/* Math text: keep long formulas inside the screen */
.math { overflow-wrap: anywhere; }
div.math { overflow-x: auto; overflow-y: hidden; }
.katex { font-size: 1.08em; }
.katex-display { margin: .4rem 0; overflow-x: auto; overflow-y: hidden; }
.q-body, .item-body > .math { white-space: pre-line; }

/* ---------- Paper builder ---------- */
.builder .panel h2 { display: flex; align-items: center; gap: .6rem; }
.step-no {
    display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 50%;
    background: var(--navy); color: #fff; font-size: .9rem;
}
.unit-checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 0 1rem; margin-top: .5rem; }
.count-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1rem; }
.summary-row { display: flex; flex-wrap: wrap; gap: .5rem 1.25rem; margin: 1rem 0; }

/* ---------- Saved papers ---------- */
.paper-list { display: flex; flex-direction: column; gap: .6rem; }
.paper-row { display: flex; align-items: center; gap: 1rem; padding: .9rem 1rem; }
.paper-row-main { flex: 1; min-width: 0; }
.paper-row-title { font-weight: 600; color: var(--ink); }
.paper-row-actions { display: flex; gap: .4rem; flex: none; }

/* ---------- Paper view ---------- */
.paper-toolbar {
    position: sticky; top: 0; z-index: 10;
    display: flex; align-items: center; gap: .6rem; flex-wrap: wrap;
    padding: .6rem 0; margin-bottom: 1rem; background: var(--bg);
}
.toolbar-right { display: flex; gap: .4rem; margin-left: auto; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: #fff; }
.seg button { border: 0; background: transparent; padding: .45rem .9rem; font: inherit; font-size: .9rem; cursor: pointer; color: var(--ink); }
.seg button.active { background: var(--navy); color: #fff; }

.paper {
    background: #fff; border: 1px solid var(--line); border-radius: 6px;
    padding: 2.25rem 2.5rem; box-shadow: var(--shadow);
    font-family: "Times New Roman", Georgia, serif; font-size: 1.05rem;
}
.paper-head { text-align: center; border-bottom: 2px solid var(--ink); padding-bottom: 1rem; margin-bottom: 1.25rem; }
.paper-book { font-size: .95rem; letter-spacing: .03em; }
.paper-head h1 { font-size: 1.45rem; margin: .3rem 0 .5rem; }
.key-stamp { display: inline-block; border: 2px solid var(--ok); color: var(--ok); padding: .1rem .7rem; font-weight: 700; letter-spacing: .08em; font-size: .85rem; margin-bottom: .5rem; font-family: system-ui, sans-serif; }
.paper-meta { display: flex; justify-content: center; flex-wrap: wrap; gap: .25rem 1.75rem; font-size: .95rem; }
.student-lines { display: flex; flex-wrap: wrap; justify-content: space-between; gap: .75rem 1.5rem; margin-top: 1rem; text-align: left; }
.student-lines span { display: flex; align-items: flex-end; gap: .4rem; flex: 1 1 180px; }
.student-lines i { flex: 1; border-bottom: 1px solid var(--ink); height: 1.1em; }

.paper-section { margin-bottom: 1.75rem; }
.paper-section h2 {
    font-size: 1.12rem; display: flex; justify-content: space-between; gap: 1rem;
    border-bottom: 1px solid #d1d5db; padding-bottom: .3rem;
}
.sec-marks { font-weight: 400; font-size: .95rem; white-space: nowrap; }
.instr { font-style: italic; color: #374151; }
.paper-list-items { list-style: none; padding: 0; margin: 0; }
.paper-list-items > li { display: flex; gap: .6rem; padding: .55rem 0; border-bottom: 1px dashed #e5e7eb; break-inside: avoid; }
.paper-list-items > li:last-child { border-bottom: 0; }
.item-no { flex: none; min-width: 2.4rem; font-weight: 700; }
.item-body { flex: 1; min-width: 0; }
.paper .solution { font-family: system-ui, "Segoe UI", sans-serif; font-size: .95rem; }
.key-grid {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); gap: .3rem;
    background: var(--ok-soft); border: 1px solid #bbf7d0; border-radius: 8px; padding: .6rem .8rem; margin-bottom: .75rem;
    font-family: system-ui, sans-serif; font-size: .9rem;
}

.head-actions { display: flex; flex-wrap: wrap; gap: .5rem; }
.chip-warn { background: #fef3c7; color: #92400e; }

/* ---------- Quiz setup ---------- */
.field-label { font-size: .9rem; font-weight: 500; margin: .9rem 0 .4rem; }
.pill-row { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; }
.pill {
    min-height: 40px; padding: .4rem .95rem; border-radius: 999px;
    border: 1px solid var(--line); background: #fff; color: var(--ink);
    font: inherit; font-size: .92rem; cursor: pointer;
}
.pill.active { background: var(--navy); border-color: var(--navy); color: #fff; }
.pill:disabled { opacity: .4; cursor: not-allowed; }
.pill-input { width: 90px !important; }

/* ---------- Quiz taking ---------- */
.quiz-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: .6rem; }
.quiz-progress-text { font-size: 1.1rem; }
.timer {
    flex: none; font-variant-numeric: tabular-nums; font-weight: 700; font-size: 1.15rem;
    padding: .4rem .85rem; border-radius: 10px; background: var(--blue-soft); color: var(--navy);
}
.timer.is-low { background: var(--err-soft); color: var(--err); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .6; } }
.progress { height: 8px; border-radius: 99px; background: #e5e7eb; overflow: hidden; margin-bottom: 1rem; }
.progress span { display: block; height: 100%; background: linear-gradient(90deg, var(--blue), #60a5fa); transition: width .3s; }

.quiz-card { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 1.25rem 1.4rem; box-shadow: var(--shadow); }
.quiz-question { font-size: 1.15rem; margin: .25rem 0 1rem; white-space: pre-line; }
.choices { display: flex; flex-direction: column; gap: .55rem; margin-bottom: .5rem; }
.choice {
    display: flex; align-items: center; gap: .8rem; width: 100%; text-align: left;
    min-height: 54px; padding: .65rem .9rem;
    border: 2px solid var(--line); border-radius: 12px; background: #fff;
    font: inherit; font-size: 1.05rem; color: var(--ink); cursor: pointer;
    transition: border-color .12s, background .12s;
}
.choice:hover { border-color: #93c5fd; background: #f8fbff; }
.choice.is-chosen { border-color: var(--blue); background: var(--blue-soft); }
.choice-letter {
    flex: none; display: grid; place-items: center; width: 32px; height: 32px; border-radius: 50%;
    background: #f3f4f6; font-weight: 700; text-transform: lowercase;
}
.choice.is-chosen .choice-letter { background: var(--blue); color: #fff; }

.quiz-nav { display: flex; justify-content: space-between; gap: .6rem; margin: 1rem 0; }
.quiz-nav .btn { min-width: 130px; }
.palette-head { display: flex; justify-content: space-between; flex-wrap: wrap; gap: .5rem; font-weight: 600; margin-bottom: .6rem; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: #e5e7eb; margin: 0 .2rem 0 .5rem; vertical-align: middle; }
.dot-done { background: var(--blue); }
.palette { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: .4rem; margin-bottom: 1rem; }
.pal {
    height: 44px; border-radius: 10px; border: 1px solid var(--line); background: #f9fafb;
    font: inherit; font-weight: 600; cursor: pointer; color: var(--ink);
}
.pal.is-done { background: var(--blue); border-color: var(--blue); color: #fff; }
.pal.is-current { outline: 3px solid #fbbf24; outline-offset: 1px; }
.submit-wide { width: 100%; }

/* ---------- Quiz result ---------- */
.result-card {
    display: flex; gap: 1.75rem; align-items: center; flex-wrap: wrap;
    background: #fff; border: 1px solid var(--line); border-radius: 16px; padding: 1.5rem; box-shadow: var(--shadow);
}
.score-ring {
    --pct: 0;
    flex: none; width: 150px; height: 150px; border-radius: 50%;
    background: conic-gradient(var(--ok) calc(var(--pct) * 1%), #e5e7eb 0);
    display: grid; place-items: center;
}
.score-inner {
    width: 118px; height: 118px; border-radius: 50%; background: #fff;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.score-inner b { font-size: 1.7rem; line-height: 1.1; }
.score-inner span { color: var(--muted); font-size: .9rem; }
.result-info { flex: 1; min-width: 240px; }
.result-msg { font-weight: 500; }
.stat-row { display: flex; flex-wrap: wrap; gap: .6rem; margin: .75rem 0; }
.stat { min-width: 86px; padding: .55rem .8rem; border-radius: 10px; background: #f3f4f6; text-align: center; }
.stat b { display: block; font-size: 1.2rem; }
.stat span { font-size: .78rem; color: var(--muted); }
.stat-ok { background: var(--ok-soft); color: var(--ok); }
.stat-bad { background: var(--err-soft); color: var(--err); }
.result-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .5rem; }
.summary-stats { margin-bottom: 1rem; }

.review.is-right { border-left: 5px solid var(--ok); }
.review.is-wrong { border-left: 5px solid var(--err); }
.review.is-skipped { border-left: 5px solid #9ca3af; }
.verdict { font-size: .8rem; font-weight: 700; padding: .1rem .5rem; border-radius: 6px; }
.verdict.ok { background: var(--ok-soft); color: var(--ok); }
.verdict.bad { background: var(--err-soft); color: var(--err); }
.verdict.skip { background: #f3f4f6; color: var(--muted); }
.opt.wrong { background: var(--err-soft); border-color: #fecaca; color: var(--err); text-decoration: line-through; }
.mine { margin-left: auto; font-size: .72rem; font-weight: 600; text-decoration: none; display: inline-block; padding: 0 .4rem; border-radius: 4px; background: rgba(0,0,0,.06); }

.grade-badge {
    flex: none; width: 46px; height: 46px; border-radius: 12px; display: grid; place-items: center;
    font-weight: 800; font-size: 1.05rem;
}
.grade-a { background: var(--ok-soft); color: var(--ok); }
.grade-b { background: #dbeafe; color: #1e40af; }
.grade-d { background: #fef3c7; color: #92400e; }
.grade-f { background: var(--err-soft); color: var(--err); }
.grade-na { background: #f3f4f6; color: var(--muted); }

/* Blazor error bar */
#blazor-error-ui {
    display: none; position: fixed; bottom: 0; left: 0; right: 0; z-index: 1000;
    background: #fffbeb; border-top: 1px solid #fcd34d; padding: .7rem 1.25rem; box-shadow: 0 -1px 4px rgba(0,0,0,.1);
}
#blazor-error-ui .reload { margin-left: .5rem; }
#blazor-error-ui .dismiss { float: right; cursor: pointer; }

/* ==========================================================
   Tablet & phone: sidebar becomes a slide-in menu
   ========================================================== */
@media (max-width: 991.98px) {
    .sidebar { transform: translateX(-100%); box-shadow: 4px 0 24px rgba(0,0,0,.2); }
    .menu-open .sidebar { transform: translateX(0); }
    .menu-open .backdrop {
        display: block; position: fixed; inset: 0; z-index: 25; background: rgba(15, 23, 42, .45);
    }
    .main { margin-left: 0; }
    .topbar {
        display: flex; align-items: center; gap: .5rem;
        position: sticky; top: 0; z-index: 20;
        height: 56px; padding: 0 .6rem;
        background: #fff; border-bottom: 1px solid var(--line);
    }
    .topbar-title { font-weight: 700; color: var(--navy); font-size: 1.05rem; }
    .content { padding: 1.25rem 1.25rem 3rem; }
    .paper-toolbar { top: 56px; }
    .count-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .steps { grid-template-columns: 1fr; }
}

@media (max-width: 599.98px) {
    body { font-size: 15px; }
    h1 { font-size: 1.35rem; }
    .hero { padding: 1.4rem; border-radius: 12px; }
    .hero h1 { font-size: 1.55rem; }
    .content { padding: 1rem .8rem 3rem; }
    .card-grid { grid-template-columns: 1fr; }
    .page-head .btn { width: 100%; }
    .unit-card, .paper-row { flex-wrap: wrap; }
    .unit-actions, .paper-row-actions { width: 100%; }
    .unit-actions .btn, .paper-row-actions .btn { flex: 1; }
    .opt-grid { grid-template-columns: 1fr; }
    .form-grid .span-2 { grid-column: auto; }
    .toolbar select { width: 100%; }
    .builder .btn-lg { width: 100%; }
    .paper { padding: 1.1rem .9rem; border-radius: 0; margin: 0 -.8rem; border-left: 0; border-right: 0; }
    .paper-toolbar { gap: .4rem; }
    .seg { width: 100%; }
    .seg button { flex: 1; }
    .toolbar-right { margin-left: 0; width: 100%; }
    .toolbar-right .btn { flex: 1; }
    .item-no { min-width: 2rem; }
    .q-card { padding: .85rem; }
    .head-actions { width: 100%; }
    .head-actions .btn { width: auto; flex: 1; }
    .quiz-card { padding: 1rem; }
    .quiz-question { font-size: 1.05rem; }
    .quiz-nav .btn { flex: 1; min-width: 0; }
    .result-card { justify-content: center; text-align: center; padding: 1.2rem; }
    .result-info .stat-row, .result-actions { justify-content: center; }
    .result-actions .btn { flex: 1 1 140px; }
    .score-ring { width: 130px; height: 130px; }
    .score-inner { width: 102px; height: 102px; }
}

/* ==========================================================
   Print: only the paper, black on white, A4
   ========================================================== */
/* ---------- Math lines (one block per line, so PDF page breaks fall between lines) ---------- */
.math-line { min-height: 1em; }
.math-gap { height: .6em; }

/* ---------- "Paper ready" bar: print / PDF right after a paper is generated ---------- */
.ready-bar { position: relative; max-width: 900px; margin: 0 auto 1rem; padding: 1rem 2.6rem 1rem 1.1rem;
             background: var(--ok-soft); border: 1px solid #bbf7d0; border-radius: var(--radius); }
.ready-text { display: flex; flex-wrap: wrap; gap: .25rem .6rem; align-items: baseline; margin-bottom: .7rem; }
.ready-text strong { color: var(--ok); font-size: 1.05rem; }
.ready-actions { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .5rem; }
.ready-actions .btn { width: 100%; justify-content: center; }
.ready-close { position: absolute; top: .45rem; right: .55rem; border: 0; background: none; font-size: 1.4rem;
               line-height: 1; color: var(--muted); cursor: pointer; padding: .2rem .4rem; }
@media (max-width: 800px) { .ready-actions { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 420px) { .ready-actions { grid-template-columns: 1fr; } }

/* ---------- PDF export (Download PDF on the paper page) ---------- */
.pdf-note { max-width: 900px; margin: 0 auto .75rem; padding: .6rem .9rem; border-radius: 8px;
            background: #fffbeb; border: 1px solid #fde68a; color: var(--warn); font-size: .92rem; }
.paper.pdf-export { border: 0; box-shadow: none; border-radius: 0; margin: 0; padding: 0;
                    background: #fff; color: #000; font-size: 15px; max-width: none; }
.paper.pdf-export .opt.correct, .paper.pdf-export .key-grid, .paper.pdf-export .solution { background: #fff !important; }
.paper.pdf-export .paper-list-items > li { border-bottom-color: #d1d5db; }
.paper.pdf-export .opt-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.paper.pdf-export a { color: inherit; }

@media print {
    @page { size: A4; margin: 14mm 12mm; }
    body { background: #fff; font-size: 12pt; }
    .no-print, .sidebar, .topbar, .backdrop, #blazor-error-ui { display: none !important; }
    .main { margin: 0; }
    .content { max-width: none; padding: 0; }
    .paper { border: 0; box-shadow: none; padding: 0; margin: 0; }
    .paper-section h2 { break-after: avoid; }
    .opt.correct, .key-grid, .solution { background: #fff !important; }
    a { color: inherit; }
}
