:root {
  --bg: #f4f1ea; --panel: #fffdf8; --ink: #1f2328; --muted: #6b6f76; --line: #ddd6c8;
  --brand: #1f6f5c; --brand-2: #e8f3ef; --accent: #c2662d; --danger: #b42318; --paper: #fff;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 6px 24px rgba(0,0,0,.06);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-family: Tajawal, system-ui, sans-serif; font-size: 15px; }
button { font: inherit; cursor: pointer; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); padding: 7px 12px; }
button.primary { background: var(--brand); color: #fff; border-color: var(--brand); font-weight: 700; }
button.ghost { background: transparent; }
button.small { padding: 4px 10px; font-size: 13px; }
button.acct { background: var(--brand-2); border-color: #bfe0d5; }
input, select, textarea { font: inherit; border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; background: #fff; color: var(--ink); width: 100%; }
textarea { resize: vertical; }

.top { position: sticky; top: 0; z-index: 20; display: flex; gap: 14px; align-items: center; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { display: flex; gap: 10px; align-items: center; }
.brand small { display: block; color: var(--muted); font-size: 12px; }
.logo { width: 38px; height: 38px; border-radius: 10px; background: var(--brand); color: #fff; display: grid; place-items: center;
  font-family: Amiri, serif; font-size: 24px; font-weight: 700; }
.tabs { display: flex; gap: 4px; background: var(--bg); padding: 3px; border-radius: 10px; }
.tabs button { border: 0; background: transparent; }
.tabs button.on { background: var(--panel); box-shadow: var(--shadow); font-weight: 700; }
.acts { margin-inline-start: auto; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.total { font-weight: 700; padding: 5px 10px; border-radius: 999px; background: #fde7d9; color: var(--accent); }
.total.ok { background: var(--brand-2); color: var(--brand); }

.wrap { display: grid; grid-template-columns: 380px 1fr; gap: 16px; padding: 16px; align-items: start; }
.bank { position: sticky; top: 76px; max-height: calc(100vh - 92px); display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden; }
.filters { padding: 12px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
.row { display: flex; gap: 8px; align-items: center; }
.row > * { flex: 1; }
.chk { display: flex; gap: 6px; align-items: center; font-size: 14px; }
.chk input { width: auto; }
.count { color: var(--muted); font-size: 13px; }
.cards { overflow-y: auto; padding: 10px; display: grid; gap: 8px; }
.card { border: 1px solid var(--line); border-radius: 10px; padding: 10px; background: #fff; cursor: grab; transition: border-color .15s, transform .15s; }
.card:hover { border-color: var(--brand); }
.card.dragging { opacity: .5; }
.card .meta { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px; }
.chip { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--bg); color: var(--muted); }
.chip.k { background: #fde7d9; color: var(--accent); }
.chip.m { background: var(--brand-2); color: var(--brand); }
.card p { margin: 0; font-family: Amiri, serif; font-size: 16px; line-height: 1.6; }
.card .thumb { max-height: 90px; max-width: 100%; display: block; margin-top: 6px; border-radius: 6px; border: 1px solid var(--line); }
.card .cbtns { display: flex; gap: 6px; margin-top: 8px; }
.card .more { display: none; font-family: Amiri, serif; font-size: 15px; margin-top: 6px; color: #333; }
.card.open .more { display: block; }
.more-btn { width: 100%; }

.stage { min-width: 0; }
.hint { color: var(--muted); font-size: 13px; margin: 0 auto 8px; max-width: 210mm; }
.paper { background: var(--paper); width: 210mm; max-width: 100%; min-height: 297mm; margin: 0 auto; padding: 12mm 14mm;
  box-shadow: var(--shadow); border-radius: 4px; font-family: Amiri, "Simplified Arabic", serif; font-size: 17px; line-height: 1.75; }
.paper [contenteditable]:hover { outline: 1px dashed #b9c9c3; }
.paper [contenteditable]:focus { outline: 2px solid var(--brand); outline-offset: 2px; }
.hdr-top { text-align: center; font-weight: 700; line-height: 1.5; }
table.hdr { width: 100%; border-collapse: collapse; margin: 6px 0; }
table.hdr td { border: 1px solid #000; padding: 3px 8px; width: 33%; }
.title { text-align: center; font-weight: 700; font-size: 20px; border: 1.5px solid #000; border-radius: 6px; padding: 4px; margin: 8px 0; }
.student { margin: 4px 0 10px; }
.exlist { min-height: 60px; }
.ex { position: relative; padding: 4px 6px; border-radius: 6px; }
.ex:hover { background: #fbfaf6; }
.ex .tools { position: absolute; top: 2px; left: 2px; display: none; gap: 3px; }
.ex:hover .tools, .ex:focus-within .tools { display: flex; }
.ex .tools button { padding: 1px 7px; font-size: 13px; font-family: Tajawal, sans-serif; }
.ex .handle { cursor: grab; }
.exh { font-weight: 700; text-decoration: underline; margin: 10px 0 2px; }
.exh .pts { text-decoration: none; }
.exb p { margin: 0 0 4px; }
.exb img { max-width: 100%; max-height: 320px; display: block; margin: 6px auto; }
.exb table { border-collapse: collapse; margin: 6px auto; }
.exb td { border: 1px solid #000; padding: 2px 8px; }
.figph { border: 1.5px dashed #999; border-radius: 6px; padding: 10px; text-align: center; color: #666; font-size: 14px; margin: 6px 0; }
.dropend { margin-top: 8px; border: 2px dashed var(--line); border-radius: 10px; padding: 14px; text-align: center; color: var(--muted); font-family: Tajawal, sans-serif; font-size: 14px; }
.dropend.over { border-color: var(--brand); color: var(--brand); background: var(--brand-2); }
.drag-line { position: fixed; height: 3px; background: var(--brand); border-radius: 2px; pointer-events: none; z-index: 50; }
.foot { text-align: center; margin-top: 18px; font-weight: 700; }
.empty { text-align: center; color: var(--muted); padding: 30px; font-family: Tajawal, sans-serif; }

.corr h4 { margin: 12px 0 4px; text-decoration: underline; }
.corr table { width: 100%; border-collapse: collapse; }
.corr td, .corr th { border: 1px solid #000; padding: 3px 7px; vertical-align: top; }
.corr th { background: #f1efe9; }
.corr td.p { width: 70px; text-align: center; }
.corr .add { font-family: Tajawal, sans-serif; margin-top: 4px; }
.corr .sum { text-align: left; font-weight: 700; margin-top: 4px; }

dialog { border: 0; border-radius: 14px; padding: 0; width: min(520px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(20,24,28,.45); }
dialog form { padding: 18px; display: grid; gap: 10px; }
dialog h3 { margin: 0; }
dialog label { display: grid; gap: 4px; font-size: 14px; }
dialog menu { display: flex; justify-content: flex-start; gap: 8px; padding: 0; margin: 6px 0 0; flex-direction: row-reverse; }
.muted { color: var(--muted); font-size: 13px; margin: 0; }
.err { color: var(--danger); font-size: 14px; min-height: 1em; }
.packs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.pack { border: 1.5px solid var(--line); border-radius: 10px; padding: 10px; text-align: center; cursor: pointer; }
.pack.on { border-color: var(--brand); background: var(--brand-2); }
.pack b { display: block; font-size: 20px; }
.steps { margin: 0; padding-inline-start: 20px; display: grid; gap: 6px; font-size: 14px; }
code { background: var(--bg); padding: 2px 6px; border-radius: 6px; direction: ltr; display: inline-block; }
.qimg { max-height: 140px; max-width: 100%; }
.saved { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; max-height: 300px; overflow: auto; }
.saved li { display: flex; gap: 6px; align-items: center; border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; }
.saved li span { flex: 1; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 100; }

@media (max-width: 900px) {
  .wrap { grid-template-columns: 1fr; padding: 10px; }
  .bank { position: static; max-height: 60vh; }
  .paper { padding: 8mm 6mm; min-height: auto; font-size: 16px; }
  .acts { margin-inline-start: 0; }
}

@media print {
  body * { visibility: hidden !important; }
  body.print-paper #paper, body.print-paper #paper *,
  body.print-corr #corrPaper, body.print-corr #corrPaper * { visibility: visible !important; }
  body:not(.print-paper):not(.print-corr)::after { visibility: visible !important; content: "الطباعة متاحة عبر زر «طباعة / PDF»"; position: fixed; top: 40%; width: 100%; text-align: center; font-size: 24px; }
  #paper, #corrPaper { position: absolute; top: 0; right: 0; width: 100%; box-shadow: none; padding: 0; margin: 0; min-height: 0; }
  #corrView[hidden], #paperView[hidden] { display: block !important; }
  .tools, .dropend, .add { display: none !important; }
  @page { size: A4; margin: 12mm 14mm; }
}
