:root {
  --bg: #eef1f4;
  --card: #ffffff;
  --ink: #16212b;
  --muted: #5c6873;
  --line: #e3e8ec;
  --soft: #f5f7f9;
  --pri: #0b6e8a;
  --pri-2: #085a71;
  --pri-soft: #e2f0f4;
  --ok: #15803d;
  --ok-soft: #e4f4ea;
  --warn: #a9570a;
  --warn-soft: #fdf2df;
  --bad: #b42318;
  --bad-soft: #fdeceb;
  --r: 14px;
  color-scheme: light;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  font-family: 'IBM Plex Sans Arabic', Tahoma, 'Segoe UI', sans-serif;
  font-size: 14.5px;
  line-height: 1.6;
  color: var(--ink);
  background-color: var(--bg);
  background-image:
    linear-gradient(rgba(12, 52, 66, .07) 1px, transparent 1px),
    linear-gradient(90deg, rgba(12, 52, 66, .07) 1px, transparent 1px),
    linear-gradient(rgba(12, 52, 66, .035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(12, 52, 66, .035) 1px, transparent 1px);
  background-size: 80px 80px, 80px 80px, 16px 16px, 16px 16px;
  background-attachment: fixed;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--pri); }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace; direction: ltr; unicode-bidi: isolate; letter-spacing: .03em; }

/* ---------- البطاقة الرئيسية في منتصف الشاشة ---------- */
/* البطاقة تملأ ارتفاع الشاشة دائماً، فلا يظهر فراغ كبير تحتها في الصفحات القصيرة */
:root { --shell-m: 40px; }
.shell {
  max-width: 920px;
  margin: var(--shell-m) auto;
  min-height: calc(100vh - 2 * var(--shell-m));
  min-height: calc(100dvh - 2 * var(--shell-m));
  display: flex;
  flex-direction: column;
  background: var(--card);
  border: 1px solid #d5dbe1;
  border-radius: 28px;
  box-shadow: 0 12px 40px rgba(12, 52, 66, .10);
  overflow: hidden;
}
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 28px;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; }
.brand strong { display: block; font-size: 16px; line-height: 1.25; }
.brand small { display: block; font-size: 12px; color: var(--muted); }
.brand-mark { display: block; flex: none; }
.privacy { font-size: 12px; color: #DFE7F1; background: #7E2A2A; border: 1px solid #7E2A2A; padding: 3px 12px; border-radius: 99px; }
main { padding: 28px 32px 36px; flex: 1; }

/* ---------- عناصر عامة ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 7px 15px; border-radius: 10px; cursor: pointer; font-weight: 500; text-decoration: none;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--soft); border-color: #cfd6dd; }
.btn.primary { background: var(--pri); border-color: var(--pri); color: #fff; }
.btn.primary:hover { background: var(--pri-2); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); }
.btn.danger:hover { background: var(--bad-soft); border-color: #f3c2bd; }
.btn.sm { padding: 4px 11px; font-size: 13.5px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.icon-btn { border: 0; background: transparent; width: 32px; height: 32px; border-radius: 8px; cursor: pointer; font-size: 20px; line-height: 1; color: var(--muted); }
.icon-btn:hover { background: #eef1f4; color: var(--ink); }

.panel { border: 1px solid var(--line); border-radius: var(--r); padding: 18px; background: #fff; }
.panel-title { font-size: 15px; margin: 0 0 12px; }
.panel .panel-title:not(:first-child) { margin-top: 20px; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 11px; }
.field label { font-size: 13px; color: var(--muted); font-weight: 500; }
.field small { color: var(--muted); font-size: 12.5px; }
input:not([type]), input[type=text], input[type=number], select, textarea {
  border: 1px solid #d3dae1; border-radius: 9px; padding: 7px 10px; background: #fff; width: 100%;
}
input::placeholder, textarea::placeholder { color: #a3adb6; }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(11, 110, 138, .22); border-color: var(--pri); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.seg { display: inline-flex; flex-wrap: wrap; background: #eef1f4; padding: 3px; border-radius: 10px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 13px; border-radius: 8px; cursor: pointer; font-weight: 500; color: var(--muted); }
.seg button.on { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .1); }
.seg button small { font-weight: 400; color: var(--muted); margin-inline-start: 4px; }

.alert { padding: 9px 14px; border-radius: 10px; margin-bottom: 14px; font-size: 13.5px; }
.alert.warn { background: var(--warn-soft); color: #6f3a06; border: 1px solid #f5d9a8; }

.pill { display: inline-block; font-size: 12px; padding: 0 8px; border-radius: 99px; background: #eef1f4; color: var(--muted); font-weight: 500; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 12.5px; padding: 1px 10px; border-radius: 99px; font-weight: 500; white-space: nowrap; }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }

/* ---------- الرئيسية ---------- */
.hero { text-align: center; margin-bottom: 28px; }
.hero h1 { font-size: 24px; line-height: 1.35; margin: 0 0 8px; }
.hero p { margin: 0 auto; color: var(--muted); max-width: 54ch; }
.flow { list-style: none; margin: 20px auto 0; padding: 0; display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; counter-reset: s; }
.flow li { counter-increment: s; display: flex; align-items: center; gap: 8px; font-size: 13.5px; padding: 5px 14px 5px 6px; border: 1px solid var(--line); border-radius: 99px; background: var(--soft); }
.flow li::before { content: counter(s, arabic-indic); display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; background: var(--pri); color: #fff; font-size: 12px; font-weight: 700; }
.list-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; padding-top: 22px; border-top: 1px solid var(--line); }
.list-head h2 { margin: 0; font-size: 16px; }
.exam-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
.exam-card { border: 1px solid var(--line); border-radius: var(--r); padding: 14px 16px; cursor: pointer; transition: border-color .15s, background .15s; }
.exam-card:hover { border-color: #b9d6df; background: #fbfdfd; }
.exam-card h3 { margin: 0; font-size: 15.5px; }
.ec-meta { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 12px; }
.ec-actions { display: flex; gap: 6px; }
.empty { text-align: center; padding: 32px 20px; color: var(--muted); border: 1px dashed #cdd5dc; border-radius: var(--r); }

/* ---------- صفحة الاختبار ---------- */
.exam-head { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px; margin-bottom: 14px; }
.exam-head h1 { margin: 0; font-size: 20px; text-align: center; }
.back { text-decoration: none; color: var(--muted); font-size: 13.5px; justify-self: start; }
.back:hover { color: var(--pri); }
.tabs { display: flex; width: fit-content; max-width: 100%; margin: 0 auto 22px; padding: 4px; gap: 2px; background: #eef1f4; border-radius: 12px; overflow-x: auto; }
.tab { display: flex; align-items: center; gap: 7px; padding: 6px 14px; border-radius: 9px; text-decoration: none; color: var(--muted); font-weight: 500; white-space: nowrap; font-size: 14px; }
.tab b { display: grid; place-items: center; width: 20px; height: 20px; border-radius: 50%; background: #dfe5ea; font-size: 11.5px; }
.tab.on { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .1); }
.tab.on b { background: var(--pri); color: #fff; }

/* الإعداد */
.setup-grid { display: grid; grid-template-columns: 1fr 300px; gap: 18px; align-items: start; }
.sec-row { display: flex; flex-wrap: wrap; align-items: center; gap: 7px; padding: 9px 10px; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 8px; background: var(--soft); }
.sec-row .range { flex-basis: 100%; font-size: 12px; color: var(--muted); margin-bottom: -4px; }
.sec-row span { font-size: 13px; color: var(--muted); }
.sec-row input[type=number] { width: 66px; }
.sec-row select { flex: 1 1 170px; width: auto; }
.sec-row .icon-btn { margin-inline-start: auto; }
.totals { margin-top: 12px; padding: 8px 12px; border-radius: 10px; background: var(--pri-soft); color: #0a4a5c; font-weight: 500; font-size: 13.5px; }
.totals.over { background: var(--bad-soft); color: var(--bad); }
.preview-panel { position: sticky; top: 16px; }
.preview-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.preview-head .panel-title { margin: 0; }
.preview-actions { display: flex; gap: 6px; }
.paper { background: #fff; border: 1px solid var(--line); box-shadow: 0 4px 16px rgba(16, 33, 43, .1); aspect-ratio: 210 / 297; }
.paper svg { width: 100%; height: 100%; display: block; }
.hints { margin: 12px 0 0; padding-inline-start: 16px; color: var(--muted); font-size: 12.5px; }
.hints li { margin-bottom: 3px; }

/* المفتاح */
.key-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.key-head .panel-title { margin: 0; }
.key-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.progress { height: 5px; background: #eef1f4; border-radius: 99px; overflow: hidden; }
.progress .bar { height: 100%; background: var(--pri); transition: width .2s; }
.key-count { font-size: 13px; color: var(--muted); margin: 6px 0 14px; }
/* شبكة المفتاح بنفس تخطيط الورقة المطبوعة */
.key-sheet-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--r); padding: 12px 10px; }
.key-sheet { display: grid; justify-content: center; column-gap: 28px; min-width: max-content; margin: 0 auto; }
.key-col .kq { display: flex; align-items: center; gap: 4px; height: 42px; padding: 0 2px; }
.key-col .kq.sep { border-bottom: 1px solid #d3d9de; }
.key-col .kn { width: 30px; text-align: start; font-weight: 700; font-size: 15px; color: var(--ink); font-variant-numeric: tabular-nums; }
.key-col .kq.empty .kn { color: #d9822b; }
.key-col .opt { width: 32px; height: 32px; }
/* اختبارات كثيرة الأعمدة والخيارات: أصغر قليلاً لتتسع كل الأعمدة */
.key-sheet.dense { column-gap: 14px; }
.key-sheet.dense .kq { gap: 3px; height: 38px; }
.key-sheet.dense .kn { width: 24px; font-size: 14px; }
.key-sheet.dense .opt { width: 28px; height: 28px; font-size: 12.5px; }
.kq { display: flex; align-items: center; gap: 4px; }
.kn { width: 28px; font-weight: 600; font-size: 13.5px; color: var(--muted); text-align: center; }
.opt { width: 31px; height: 31px; border-radius: 50%; border: 1.5px solid #c5cdd5; background: #fff; cursor: pointer; font-weight: 600; color: #53606b; font-size: 13.5px; }
.opt:hover { border-color: var(--pri); color: var(--pri); }
.opt.on { background: var(--pri); border-color: var(--pri); color: #fff; }
.quick { margin-top: 16px; border-top: 1px solid var(--line); padding-top: 12px; }
.quick summary { cursor: pointer; font-weight: 600; }
.quick textarea { margin: 10px 0 8px; min-height: 64px; }

/* التصحيح */
.upload-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; margin-bottom: 10px; }
.drop { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; text-align: center; padding: 22px 16px; border: 2px dashed #b8cbd3; border-radius: var(--r); background: var(--soft); cursor: pointer; transition: background .15s, border-color .15s; }
.drop:hover, .drop.over { background: var(--pri-soft); border-color: var(--pri); }
.drop strong { font-size: 15px; }
.drop span { font-size: 13px; color: var(--muted); }
.drop-icon { font-size: 24px; color: var(--pri); line-height: 1; }
.camera { flex-direction: column; min-width: 118px; border-radius: var(--r); border-style: dashed; background: var(--soft); }
.camera .ic { font-size: 22px; }
.busy { border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; margin-bottom: 12px; }
.busy p { margin: 0 0 6px; font-size: 13.5px; }
.tips { font-size: 12.5px; color: var(--muted); margin: 0 0 16px; text-align: center; }
.stats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin-bottom: 14px; }
.stat { border: 1px solid var(--line); border-radius: 12px; padding: 9px 12px; text-align: center; }
.stat b { display: block; font-size: 19px; line-height: 1.3; }
.stat span { font-size: 12px; color: var(--muted); }
.stat.warn b { color: var(--warn); }
.table-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.table-head h2 { margin: 0; font-size: 15.5px; margin-inline-end: auto; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--r); }
table.results { width: 100%; border-collapse: collapse; font-size: 14px; }
.results th { text-align: start; font-weight: 600; font-size: 12.5px; color: var(--muted); background: var(--soft); padding: 8px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.results td { padding: 7px 12px; border-bottom: 1px solid #eef1f4; white-space: nowrap; }
.results tbody tr:last-child td { border-bottom: 0; }
.results tbody tr { cursor: pointer; }
.results tbody tr:hover { background: #f7fafb; }
.results tr.err td { background: #fff7f6; }
.results .num { font-variant-numeric: tabular-nums; }
.results .fname { color: var(--muted); font-size: 12px; max-width: 170px; overflow: hidden; text-overflow: ellipsis; }

/* نافذة المراجعة */
.modal { position: fixed; inset: 0; background: rgba(10, 24, 32, .55); z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-card { background: #fff; border-radius: 22px; width: min(1120px, 100%); height: min(92vh, 1000px); display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 20px 60px rgba(0, 0, 0, .3); }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.modal-head .nav { display: flex; gap: 6px; align-items: center; }
.review-body { flex: 1; display: grid; grid-template-columns: 1fr 330px; min-height: 0; }
.sheet-view { overflow: auto; background: #dfe4e8; padding: 16px; }
.sheet-wrap { position: relative; margin: 0 auto; width: 100%; max-width: 640px; box-shadow: 0 4px 18px rgba(0, 0, 0, .18); background: #fff; }
.sheet-wrap img { display: block; width: 100%; }
.sheet-wrap svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.sheet-view img.raw { max-width: 100%; display: block; margin: 0 auto; }
.zoom { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; margin-inline-end: 6px; }
.zoom button { width: 34px; height: 30px; border: 0; background: #fff; cursor: pointer; font-size: 18px; line-height: 1; }
.zoom button + button { border-inline-start: 1px solid var(--line); }
.zoom button:hover { background: var(--soft); }
.review-side { border-inline-start: 1px solid var(--line); overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.score-box { background: var(--pri-soft); border-radius: 12px; padding: 12px 14px; }
.score-box b { font-size: 26px; line-height: 1.2; }
.score-box span { color: var(--muted); }
.side-sec h4 { margin: 0 0 6px; font-size: 13.5px; }
.side-sec .note { font-size: 12.5px; color: var(--warn); margin-top: 4px; }
.rq { display: flex; align-items: center; gap: 4px; margin-bottom: 6px; }
.rq .kn { width: 36px; }
.rq .opt { width: 29px; height: 29px; font-size: 13px; }
.rq .opt.blank { width: auto; border-radius: 99px; padding: 0 10px; font-weight: 500; font-size: 12px; }
.legend { font-size: 12px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 12px; }
.legend i { display: inline-block; width: 11px; height: 11px; border-radius: 50%; border: 2px solid; vertical-align: -1px; margin-inline-end: 4px; }
.side-actions { margin-top: auto; display: flex; flex-direction: column; gap: 8px; }

.ov-hit { fill: transparent; stroke: transparent; stroke-width: .55; cursor: pointer; pointer-events: all; }
.ov-hit:hover { stroke: rgba(11, 110, 138, .55); fill: rgba(11, 110, 138, .08); }
.ov-ok { stroke: #16a34a; }
.ov-bad { stroke: #dc2626; }
.ov-sel { stroke: #2563eb; }
.ov-key { stroke: #16a34a; stroke-dasharray: 1 .7; }
.ov-flag { fill: rgba(245, 158, 11, .2); stroke: #f59e0b; stroke-width: .3; pointer-events: none; }

/* الإشعارات */
#toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: #16212b; color: #fff; padding: 9px 18px; border-radius: 10px; font-size: 13.5px; box-shadow: 0 6px 20px rgba(0, 0, 0, .2); transition: opacity .4s; }
.toast.bad { background: var(--bad); }
.toast.out { opacity: 0; }

@media (max-width: 960px) {
  :root { --shell-m: 16px; }
  .shell { margin: var(--shell-m); }
  .review-body { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .review-side { border-inline-start: 0; border-top: 1px solid var(--line); max-height: 42vh; }
}
@media (max-width: 760px) {
  .setup-grid { grid-template-columns: 1fr; }
  .preview-panel { position: static; }
  .stats { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 600px) {
  :root { --shell-m: 0px; }
  .shell { margin: 0; border: 0; border-radius: 0; box-shadow: none; }
  .topbar { padding: 12px 16px; }
  main { padding: 18px 16px 28px; }
  /* على الجوال: التنبيه كاملاً على سطرين بجانب الشعار */
  .privacy { font-size: 11px; line-height: 1.45; text-align: center; border-radius: 10px; padding: 4px 10px; }
  .privacy .pv-dash { display: none; }
  .privacy .pv-2 { display: block; }
  .row2 { grid-template-columns: 1fr; }
  .upload-row { grid-template-columns: 1fr; }
  .exam-head { grid-template-columns: 1fr; text-align: center; }
  .back { justify-self: center; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .key-sheet-wrap { padding: 10px 6px; }
  .key-sheet, .key-sheet.dense { column-gap: 14px; }
  .key-col .kq, .key-sheet.dense .kq { gap: 3px; height: 36px; }
  .key-col .kn, .key-sheet.dense .kn { width: 24px; font-size: 13.5px; }
  .key-col .opt, .key-sheet.dense .opt { width: 26px; height: 26px; font-size: 12px; border-width: 1.2px; }
  .modal { padding: 0; }
  .modal-card { height: 100vh; border-radius: 0; }
}

/* ---------- الطباعة ---------- */
#print-area { display: none; }
@media print {
  @page { size: A4 portrait; margin: 0; }
  body { background: #fff; }
  body > :not(#print-area) { display: none !important; }
  #print-area { display: block; }
  #print-area svg { display: block; width: 210mm; height: 296mm; }
}
