@font-face { font-family: 'Naskh'; src: url('fonts/notonaskharabic.ttf') format('truetype'); font-weight: 400 700; font-display: swap; }

:root { --brand: #325a66; --brand-soft: #e0eef0; --ink: #1c2126; --muted: #626d78; --line: #dde3e8; --bg: #f5f7fa; --paper: #fff; }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; overscroll-behavior: none; background: #dfe5ea; color: var(--ink); font-family: 'Naskh', serif; -webkit-text-size-adjust: 100%; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: #fff; border-radius: 14px; padding: 6px 14px; min-height: 46px; touch-action: manipulation; -webkit-user-select: none; user-select: none; }
button:active { filter: brightness(.94); }
button.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
button.small { min-height: 38px; padding: 2px 12px; font-size: 14px; border-radius: 12px; color: var(--brand); }
button.small.primary { color: #fff; }
button.danger { color: #a33127; }
button.icon { border: 0; background: none; width: 44px; padding: 0; display: grid; place-items: center; flex: 0 0 auto; }
svg.i { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* قالب الهاتف: ملء الشاشة على الهاتف، وإطار بعرض هاتف في وسط الشاشات الواسعة. الارتفاع يتبع لوحة المفاتيح. */
#app { position: fixed; top: var(--vvt, 0px); left: 0; right: 0; margin: 0 auto; width: 100%; max-width: 430px; height: 100vh; height: var(--vvh, 100dvh); display: flex; flex-direction: column; background: var(--bg); overflow: hidden; }
@media screen and (min-width: 520px) and (min-height: 600px) {
  #app { top: 20px; height: min(900px, calc(100dvh - 40px)); border-radius: 36px; border: 8px solid #1c2126; box-shadow: 0 20px 50px #0003; }
}
.top { display: flex; gap: 4px; align-items: center; background: #fff; border-bottom: 1px solid var(--line); padding: calc(6px + env(safe-area-inset-top)) 6px 6px; }
.top .grow { flex: 1; text-align: center; font-size: 12px; color: var(--muted); min-width: 0; white-space: nowrap; overflow: hidden; }
.pill { border-radius: 22px; min-height: 40px; padding: 2px 12px; font-size: 15px; white-space: nowrap; }
.pill[aria-pressed=true] { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); }
.title { background: #fff; border-bottom: 1px solid var(--line); padding: calc(16px + env(safe-area-inset-top)) 20px 12px; }
.title h1 { margin: 0; font-size: 28px; }
.title p { margin: 0; color: var(--muted); font-size: 14px; }
.page { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; padding: 14px 12px 28px; }
.bar { display: flex; gap: 8px; background: #fff; border-top: 1px solid var(--line); padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }
.bar button { flex: 1 1 0; min-width: 0; padding: 4px 6px; white-space: nowrap; }
.tabs { display: flex; background: #fff; border-top: 1px solid var(--line); padding: 6px 6px calc(6px + env(safe-area-inset-bottom)); }
.tabs button { flex: 1; border: 0; background: none; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 13px; color: var(--muted); padding: 2px; }
.tabs button span { display: grid; place-items: center; width: 62px; height: 32px; border-radius: 18px; }
.tabs button[aria-current=page] { color: var(--brand); font-weight: 700; }
.tabs button[aria-current=page] span { background: var(--brand-soft); }
.keyboard .tabs { display: none; }
.keyboard .bar { padding-bottom: 8px; }

.card { display: flex; align-items: center; gap: 10px; background: #fff; border: 1px solid var(--line); border-radius: 16px; padding: 12px; margin-bottom: 10px; }
.card .name { flex: 1; text-align: start; border: 0; background: none; padding: 0; }
.card .name small { display: block; color: var(--muted); font-size: 13px; }
.empty { text-align: center; color: var(--muted); padding: 48px 10px 16px; margin: 0; }
.center { text-align: center; }
.note { color: var(--muted); font-size: 13px; text-align: center; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid button { min-height: 64px; }
.rows { background: #fff; border: 1px solid var(--line); border-radius: 16px; margin-bottom: 12px; overflow: hidden; }
.rows button { display: flex; width: 100%; justify-content: space-between; align-items: center; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; background: none; padding: 12px 16px; min-height: 56px; }
.rows > :last-child { border-bottom: 0; }

/* الورقة: على الشاشة مريحة للإصبع (لا يقل خط الكتابة عن 16 كي لا يكبّر الآيفون الشاشة)، وعند الطباعة بمقاساتها الحقيقية. */
.paper { background: var(--paper); box-shadow: 0 2px 10px #0000001a; border-radius: 4px; padding: 16px 10px 40px; min-height: 100%; font-size: max(16px, calc(var(--body) * 1.6px)); line-height: var(--leading); }
.paper[dir=ltr] { text-align: left; }
.header { display: grid; grid-template-columns: 1fr 1.5fr 1fr; column-gap: 8px; font-size: max(13px, calc(var(--head) * 1.6px)); line-height: 1.6; padding-bottom: 6px; margin-bottom: 10px; border-bottom: 2px solid var(--ink); }
.cell { min-height: 1.5em; min-width: 0; outline: none; overflow-wrap: anywhere; -webkit-user-select: text; user-select: text; }
.cell:nth-child(3n+2) { text-align: center; }
.cell:nth-child(3n+3) { text-align: end; }
.cell:nth-child(2) { font-weight: 700; font-size: 1.15em; }

.field { outline: none; white-space: pre-wrap; overflow-wrap: anywhere; min-height: 1.6em; flex: 1; min-width: 0; text-align: start; -webkit-user-select: text; user-select: text; }
.field:empty::before, .cell:empty::before { content: attr(data-hint); color: #a6adb5; font-weight: 400; pointer-events: none; }

.block { position: relative; margin-bottom: 8px; break-inside: avoid; }
.row { display: flex; align-items: flex-start; gap: 6px; }
.label { font-weight: 700; white-space: nowrap; }
.label sup { font-size: .72em; }
.branches { padding-inline-start: 1.2em; }
.items { padding-inline-start: 1.8em; }
.items.two { display: grid; grid-template-columns: 1fr 1fr; column-gap: 12px; }
.marks { border: 0; background: none; padding: 0 2px; min-height: 0; font-weight: 700; white-space: nowrap; font-size: .9em; }
.marks.unset { color: #9aa3ad; font-weight: 400; font-size: .8em; display: none; }
/* أزرار السؤال تظهر للسؤال المحدد وحده */
.tools { display: none; flex-wrap: wrap; gap: 6px; padding: 2px 0 6px; font-size: 16px; line-height: 1.4; }
.block.active { background: #325a660a; border-radius: 8px; }
.block.active .tools { display: flex; }
.block.active .marks.unset { display: inline-block; }
.divider { border-bottom: 1px solid var(--ink); padding-bottom: 6px; }
.footer { margin-top: 26px; }
.footer .field { text-align: center; }
.footer.teacher { width: 55%; margin-inline-start: auto; margin-top: 8px; text-align: center; }
.kept { border: 1px dashed var(--line); border-radius: 8px; color: var(--muted); font-size: 14px; padding: 8px; text-align: center; }

/* المعادلات */
.eq { display: inline-block; vertical-align: middle; padding: 0 3px; border-radius: 4px; cursor: pointer; white-space: nowrap; text-indent: 0; }
.math { display: inline-flex; align-items: center; direction: ltr; unicode-bidi: isolate; }
.m-var { font-style: italic; font-family: 'Times New Roman', serif; }
.eq.chem .m-var, .preview.chem .m-var { font-style: normal; }
.m-op { padding: 0 .2em; }
.m-frac, .m-arrow { display: inline-flex; flex-direction: column; align-items: center; margin: 0 .12em; line-height: 1.25; font-size: .92em; }
.m-num { padding: 0 .2em; }
.m-den { padding: 0 .2em; border-top: .07em solid currentColor; min-width: 100%; display: flex; justify-content: center; }
.m-sup { font-size: .7em; position: relative; top: -.55em; line-height: 1.1; }
.m-sub { font-size: .7em; position: relative; top: .4em; line-height: 1.1; }
.m-root { display: inline-flex; align-items: stretch; }
.m-index { font-size: .6em; align-self: flex-start; margin-right: -.4em; }
.m-radical { font-size: 1.15em; display: flex; align-items: center; }
.m-radicand { border-top: .07em solid currentColor; padding: 0 .15em; display: flex; align-items: center; }
.m-paren { display: inline-flex; align-items: center; }
.m-bracket { font-size: 1.15em; }
.m-big { display: inline-flex; align-items: center; }
.m-ldiv { display: inline-grid; grid-template-columns: auto auto; line-height: 1.3; }
.m-quot { grid-column: 2; justify-self: center; }
.m-divisor { grid-column: 1; border-bottom: .08em solid currentColor; padding: 0 .3em; }
.m-dividend { grid-column: 2; border-top: .08em solid currentColor; border-left: .08em solid currentColor; padding: 0 .4em; }

/* النوافذ: القوائم تصعد من الأسفل، ونوافذ الكتابة من الأعلى كي لا تغطيها لوحة المفاتيح */
dialog { border: 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); width: 100%; max-width: 414px; max-height: 82vh; max-height: 82dvh; overflow-y: auto; font-family: 'Naskh', serif; color: var(--ink); }
dialog.sheet { margin: auto auto 0; border-radius: 24px 24px 0 0; }
dialog.top-sheet { margin: calc(8px + env(safe-area-inset-top)) auto auto; border-radius: 20px; width: calc(100% - 16px); }
@media screen and (min-width: 520px) and (min-height: 600px) {
  dialog.sheet { margin-bottom: 28px; border-radius: 24px; }
  dialog.top-sheet { margin-top: 48px; }
}
dialog::backdrop { background: #0008; }
dialog h2 { margin: 0 0 8px; font-size: 18px; }
dialog input[type=text] { width: 100%; font-size: 18px; border: 1px solid var(--line); border-radius: 12px; padding: 10px; }
dialog label { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 9px 0; }
dialog input[type=checkbox] { width: 26px; height: 26px; accent-color: var(--brand); }
dialog .stepper { display: flex; align-items: center; gap: 12px; }
.preview { min-height: 70px; display: flex; align-items: center; justify-content: center; font-size: 24px; background: var(--bg); border-radius: 12px; margin: 10px 0; overflow-x: auto; }
.chips { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin: 8px 0; }
.chips button { min-width: 0; padding: 0; direction: ltr; }
.actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 10px; }
.actions .primary { min-width: 96px; }

@media print {
  @page { size: A4; margin: 15mm; }
  html, body { background: #fff; height: auto; overflow: visible; }
  #app { position: static; display: block; width: auto; max-width: none; height: auto; overflow: visible; background: #fff; }
  .top, .bar, .tabs, .tools, .note, .kept, .marks.unset, .block.active .marks.unset, dialog { display: none !important; }
  .page { overflow: visible; padding: 0; }
  .block.active { background: none; }
  .paper { box-shadow: none; padding: 0; min-height: 0; font-size: calc(var(--body) * 1pt); }
  .header { font-size: calc(var(--head) * 1pt); }
  .field:empty::before, .cell:empty::before { content: ''; }
  .field { text-align: justify; }
  .footer .field { text-align: center; }
  .branches { padding-inline-start: 1.6em; }
  .items { padding-inline-start: 2.4em; }
  .footer.teacher { width: 40%; }
  .marks { color: var(--ink); font-size: 1em; }
  .eq { cursor: auto; }
}

/* ---------- المرحلة الثالثة: التنسيق والتراجع والجداول والأشكال والقوالب والتقسيم ---------- */
.bar button.icon { flex: 0 0 38px; }
.bar button { padding: 2px; font-size: 15px; }
.bar button[aria-pressed=true] { background: var(--brand-soft); border-color: var(--brand); }
button:disabled { opacity: .35; }
.format { display: flex; gap: 6px; overflow-x: auto; background: #fff; border-top: 1px solid var(--line); padding: 6px 10px; flex: 0 0 auto; }
.format[hidden] { display: none; }
.format button { flex: 0 0 auto; min-width: 44px; font-size: 16px; }
.caption { font-size: 14px; color: var(--muted); margin: 8px 0 2px; }
.chips-row { display: flex; gap: 8px; margin-bottom: 12px; }
button.template { display: flex; flex-direction: column; justify-content: center; gap: 4px; min-height: 120px; }
button.template small { color: var(--muted); font-size: 12px; line-height: 1.5; }
.shapes { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.shapes button { padding: 6px; min-height: 58px; display: grid; place-items: center; }
.shapes svg { width: 100%; max-height: 40px; }
.tbl { width: 100%; border-collapse: collapse; table-layout: fixed; }
.tbl td { border: 1px solid var(--ink); padding: 2px 4px; vertical-align: top; }
.tbl.t-header tr:first-child td { background: #e8ebee; font-weight: 700; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.tbl.t-lines td { border-width: 0 0 1px; }
.tbl.t-plain td { border: 0; }
.tbl .field { display: block; }
.block.shape { text-align: center; }
.figure { position: relative; display: inline-block; max-width: 100%; }
.figure svg, .figure img { display: block; width: 100%; height: 100%; object-fit: contain; }
.block.free { text-align: center; }
.split-title .field { text-align: center; font-weight: 700; font-size: 1.15em; }
.half.bottom { border-top: 2px dashed #8a949e; margin-top: 18px; padding-top: 14px; }
.break-before { border-top: 2px dashed #b9c2ca; padding-top: 12px; margin-top: 16px; }
.answer.lines div { height: 8mm; border-bottom: 1.5px dotted #555; }
.answer { margin: 4px 0; }
.answer.a-box { border: 1px solid var(--ink); }
.answer.a-grid { border: 1px solid var(--ink); background-image: linear-gradient(#999 .5px, transparent .5px), linear-gradient(90deg, #999 .5px, transparent .5px); background-size: 5mm 5mm; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.m-above, .m-below { display: flex; justify-content: center; font-size: .65em; line-height: 1.1; padding: 0 .3em; }
.m-above { margin-bottom: -.25em; }
.m-below { margin-top: -.25em; }

@media print {
  .format { display: none !important; }
  .half:not(.bottom) { min-height: 128mm; }
  .half.bottom { margin-top: 0; padding-top: 5mm; }
  .break-before { break-before: page; border-top: 0; margin-top: 0; padding-top: 0; }
}
