:root {
  --navy: #142A4F;
  --blue: #3975C6;
  --green: #16835C;
  --bg: #F3F5F8;
  --card: #fff;
  --ink: #142033;
  --muted: #5b6678;
  --line: #dde3ec;
  --warn: #B26A00;
  --warn-bg: #FFF4E0;
  --err: #B42318;
  --err-bg: #FDECEA;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: 'IBM Plex Sans Arabic', 'Noto Naskh Arabic', 'Noto Sans Arabic', 'Segoe UI', Tahoma, system-ui, sans-serif;
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); min-height: 100%; }
body { padding-top: calc(56px + var(--safe-t)); padding-bottom: var(--safe-b); }
[hidden] { display: none !important; }
button, input, select { font: inherit; }
a { color: var(--blue); }
h1 { font-size: 1.6rem; margin: .4rem 0; color: var(--navy); }
h2 { font-size: 1.15rem; margin: 0; color: var(--navy); }
h3 { font-size: 1rem; margin: 0 0 .6rem; color: var(--navy); }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.center { text-align: center; }

.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 20; height: calc(56px + var(--safe-t)); padding: var(--safe-t) 8px 0;
  display: flex; align-items: center; gap: 6px; background: var(--navy); color: #fff;
}
.topbar-title { flex: 1; display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; min-width: 0; }
.topbar-title > span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-actions { display: flex; align-items: center; gap: 2px; }
.icon-btn { position: relative; width: 44px; height: 44px; border: 0; background: transparent; color: inherit; border-radius: 12px; display: grid; place-items: center; cursor: pointer; }
.icon-btn:active { background: rgba(255,255,255,.12); }
.badge { position: absolute; top: 4px; left: 4px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: #F59E0B; color: #111; font-size: .7rem; font-weight: 700; display: grid; place-items: center; }
.demo-badge { background: #F59E0B; color: #1d1300; font-size: .72rem; padding: 2px 8px; border-radius: 999px; font-weight: 700; white-space: nowrap; }
.net { width: 10px; height: 10px; border-radius: 50%; background: #22c55e; margin-inline: 6px; box-shadow: 0 0 0 2px rgba(255,255,255,.25); }
.net.off { background: #ef4444; }

main { max-width: 640px; margin: 0 auto; padding: 14px 14px 24px; }
.screen { animation: fade .18s ease-out; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 12px 0; }
.hero { text-align: center; padding: 18px 8px 4px; }
.hero-logo { border-radius: 18px; box-shadow: 0 6px 18px rgba(20,42,79,.25); }
.field { display: grid; gap: 6px; margin-bottom: 12px; }
.field > span { font-weight: 600; font-size: .9rem; color: var(--navy); }
.field input, .field select { width: 100%; padding: 12px 14px; border: 1.5px solid var(--line); border-radius: 12px; background: #fff; font-size: 1rem; color: var(--ink); }
.field input:focus, .field select:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(57,117,198,.18); }
.otp { letter-spacing: .5em; text-align: center; font-size: 1.4rem !important; font-variant-numeric: tabular-nums; }
.check { display: flex; align-items: center; gap: 8px; margin: 10px 0; }
.hint { color: var(--muted); font-size: .82rem; margin: 8px 0 0; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); margin: 8px 0; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--line); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 46px; padding: 0 18px; border-radius: 12px; border: 1.5px solid transparent; font-weight: 700; cursor: pointer; text-decoration: none; background: #e8edf5; color: var(--navy); }
.btn.block { width: 100%; }
.btn.primary { background: var(--blue); color: #fff; }
.btn.primary:active { background: #2d62ab; }
.btn.ghost { background: #fff; border-color: var(--line); color: var(--navy); }
.btn.link { background: transparent; color: var(--blue); min-height: 40px; }
.btn.danger { background: var(--err-bg); color: var(--err); }
.btn.small { min-height: 36px; padding: 0 12px; font-size: .85rem; }
.btn.grow { flex: 1; }
.btn:disabled { opacity: .55; cursor: default; }

.hello { font-weight: 700; color: var(--navy); margin: 4px 0 12px; }
.list-head { display: flex; align-items: center; justify-content: space-between; margin: 14px 0 8px; }
.exam-list, .queue-list, .fix-list { list-style: none; margin: 0; padding: 0; }
.exam-item { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 10px; display: flex; align-items: center; gap: 12px; cursor: pointer; }
.exam-item:active { border-color: var(--blue); }
.exam-item .ic { width: 42px; height: 42px; border-radius: 12px; background: #e9f0fb; color: var(--blue); display: grid; place-items: center; font-weight: 800; flex: none; }
.exam-item .tt { font-weight: 700; }
.exam-item .meta { color: var(--muted); font-size: .82rem; margin-top: 2px; }
.empty { text-align: center; color: var(--muted); padding: 28px 0; }

/* scan */
body.scanning { background: #0b1220; }
body.scanning main { max-width: none; padding: 0; }
.scan { display: flex; flex-direction: column; height: calc(100dvh - 56px - var(--safe-t) - var(--safe-b)); color: #fff; }
.scan-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 14px; gap: 10px; }
.scan-exam { font-weight: 700; }
.scan-sub { font-size: .8rem; opacity: .75; }
.scan-count { min-width: 40px; height: 40px; border-radius: 20px; background: rgba(255,255,255,.12); display: grid; place-items: center; font-weight: 800; padding: 0 10px; }
.viewport { position: relative; flex: 1; overflow: hidden; background: #000; margin: 0 10px; border-radius: 16px; }
.viewport video, .viewport canvas#overlay { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.guide { position: absolute; inset: 7% 12%; pointer-events: none; }
.guide i { position: absolute; width: 34px; height: 34px; border: 4px solid rgba(255,255,255,.85); }
.guide i:nth-child(1) { top: 0; right: 0; border-left: 0; border-bottom: 0; border-top-right-radius: 10px; }
.guide i:nth-child(2) { top: 0; left: 0; border-right: 0; border-bottom: 0; border-top-left-radius: 10px; }
.guide i:nth-child(3) { bottom: 0; left: 0; border-right: 0; border-top: 0; border-bottom-left-radius: 10px; }
.guide i:nth-child(4) { bottom: 0; right: 0; border-left: 0; border-top: 0; border-bottom-right-radius: 10px; }
.guide.found i { border-color: #4ade80; }
.scan-status { position: absolute; bottom: 12px; inset-inline: 12px; text-align: center; background: rgba(0,0,0,.6); border-radius: 12px; padding: 8px 12px; font-size: .9rem; }
.cam-off { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; padding: 24px; background: #111827; }
.processing { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 12px; background: rgba(11,18,32,.72); font-weight: 700; }
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 4px solid rgba(255,255,255,.25); border-top-color: #fff; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.flash { animation: flash .35s ease-out; }
@keyframes flash { from { box-shadow: inset 0 0 0 999px rgba(255,255,255,.85); } to { box-shadow: inset 0 0 0 999px rgba(255,255,255,0); } }
.scan-controls { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 12px 14px 14px; gap: 8px; }
.scan-controls .btn.round { background: rgba(255,255,255,.1); color: #fff; border-color: rgba(255,255,255,.2); flex-direction: column; min-height: 58px; font-size: .72rem; gap: 2px; padding: 4px 10px; justify-self: center; }
.scan-controls .btn.round.on { background: #F59E0B; color: #111; }
.shutter { width: 76px; height: 76px; border-radius: 50%; border: 4px solid #fff; background: transparent; padding: 5px; cursor: pointer; }
.shutter span { display: block; width: 100%; height: 100%; border-radius: 50%; background: #fff; transition: transform .1s; }
.shutter:active span { transform: scale(.88); }
.shutter.progress span { background: conic-gradient(#4ade80 var(--p, 0%), #fff 0); }
.auto-toggle { grid-column: 1 / -1; justify-self: center; display: flex; gap: 6px; align-items: center; font-size: .82rem; opacity: .85; }

/* result */
.result-top { display: flex; gap: 14px; align-items: center; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.score-ring { position: relative; width: 104px; height: 104px; flex: none; }
.score-ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.score-ring circle { fill: none; stroke-width: 11; }
.score-ring .track { stroke: #e7ecf3; }
.score-ring .val { stroke: var(--green); stroke-linecap: round; stroke-dasharray: 326.7; stroke-dashoffset: 326.7; transition: stroke-dashoffset .5s; }
.score-text { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.score-text b { font-size: 1.6rem; color: var(--navy); line-height: 1; }
.score-text span { font-size: .7rem; color: var(--muted); display: block; max-width: 80px; line-height: 1.3; margin-top: 2px; }
.result-meta { min-width: 0; }
.student { font-weight: 800; font-size: 1.05rem; color: var(--navy); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip { font-size: .74rem; padding: 3px 9px; border-radius: 999px; background: #eef2f7; color: var(--navy); font-weight: 600; }
.chip.ok { background: #e3f4ec; color: var(--green); }
.chip.warn { background: var(--warn-bg); color: var(--warn); }
.chip.err { background: var(--err-bg); color: var(--err); }
.alert { border-radius: 12px; padding: 10px 14px; margin: 12px 0; font-weight: 600; font-size: .9rem; }
.alert.warn { background: var(--warn-bg); color: var(--warn); }
.alert.err { background: var(--err-bg); color: var(--err); }
.thumb-card summary, details.card summary { cursor: pointer; font-weight: 700; color: var(--navy); }
.thumb-wrap { margin-top: 10px; overflow: auto; max-height: 70vh; border-radius: 10px; background: #f0f2f5; }
.thumb-wrap canvas { width: 100%; height: auto; display: block; }
.legend { display: flex; gap: 10px; align-items: center; font-size: .78rem; color: var(--muted); margin-top: 8px; flex-wrap: wrap; }
.lg { display: inline-block; width: 12px; height: 12px; border-radius: 50%; border: 2.5px solid; }
.lg.ok { border-color: #16a34a; } .lg.mul { border-color: #dc2626; } .lg.unc { border-color: #f59e0b; }
.fix-item { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.fix-item:last-child { border-bottom: 0; }
.fix-item .qn { font-weight: 800; min-width: 52px; color: var(--navy); }
.fix-item .why { font-size: .72rem; color: var(--warn); display: block; font-weight: 600; }
.fix-item.done .why { color: var(--green); }
.opts { display: flex; gap: 6px; margin-inline-start: auto; }
.opt { width: 42px; height: 42px; border-radius: 50%; border: 2px solid #9aa6b8; background: #fff; font-weight: 700; color: var(--navy); cursor: pointer; }
.opt.sug { border-color: #f59e0b; border-style: dashed; }
.opt.sel { background: var(--navy); border-color: var(--navy); color: #fff; }
.opt.none { width: auto; border-radius: 21px; padding: 0 10px; font-size: .75rem; }
.grid-answers { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 6px; margin-top: 10px; }
.ga { border-radius: 8px; padding: 4px 6px; font-size: .78rem; background: #f3f5f8; display: flex; justify-content: space-between; }
.ga.c { background: #e3f4ec; color: var(--green); }
.ga.w { background: var(--err-bg); color: var(--err); }
.ga.u { background: var(--warn-bg); color: var(--warn); }
.sticky-actions { position: sticky; bottom: 0; display: flex; gap: 10px; padding: 12px 0 calc(12px + var(--safe-b)); background: linear-gradient(transparent, var(--bg) 30%); }
.student-fix { border-color: #f5c26b; }

/* queue */
.queue-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 10px; }
.qs { background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 10px; text-align: center; }
.qs b { display: block; font-size: 1.4rem; color: var(--navy); }
.qs span { font-size: .78rem; color: var(--muted); }
.q-item { background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 8px; display: flex; gap: 10px; align-items: center; }
.q-item .st { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.q-item .st.pending { background: #F59E0B; } .q-item .st.synced { background: var(--green); } .q-item .st.failed { background: var(--err); } .q-item .st.syncing { background: var(--blue); }
.q-item .body { flex: 1; min-width: 0; }
.q-item .t1 { font-weight: 700; }
.q-item .t2 { font-size: .78rem; color: var(--muted); }

.toast { position: fixed; bottom: calc(18px + var(--safe-b)); left: 50%; transform: translateX(-50%) translateY(140%); background: var(--navy); color: #fff; padding: 10px 18px; border-radius: 12px; font-weight: 600; z-index: 50; transition: transform .25s; max-width: 92vw; text-align: center; }
.toast.show { transform: translateX(-50%) translateY(0); }

@media (min-width: 900px) {
  body.scanning main { max-width: 900px; margin: 0 auto; }
}
/* Phase 15: PDF import progress (page n of t) inside the processing overlay */
.processing progress { inline-size: min(70vw, 280px); block-size: 8px; accent-color: #22c55e; }
#btn-pdf svg text { font-family: system-ui, sans-serif; }
.side-btns { display: flex; gap: 6px; justify-self: start; }
