:root {
  --teal: #1d6f63;
  --teal-dark: #134f46;
  --teal-soft: #e5f2ef;
  --ink: #1d2725;
  --muted: #64716e;
  --line: #d5dedc;
  --paper: #ffffff;
  --bg: #f3f6f5;
  --ok: #1f8a4c;
  --ok-soft: #e3f4ea;
  --bad: #c0392b;
  --bad-soft: #fbe9e7;
  --part: #c47f0e;
  --radius: 12px;
  --font: "Lexend", system-ui, -apple-system, "Segoe UI", sans-serif;
  --ar: "Tajawal", "Segoe UI", Tahoma, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font-family: var(--font); color: var(--ink); background: var(--bg); line-height: 1.5; font-size: 16px; }
[dir="rtl"], .ar { font-family: var(--ar); }
a { color: var(--teal); }
h1 { font-size: 1.6rem; margin: .2em 0 .3em; line-height: 1.25; }
.small { font-size: .875rem; }
.muted { color: var(--muted); }
.lead { font-size: 1.1rem; color: var(--muted); margin: .3em 0; }

.narrow { max-width: 560px; margin: 0 auto; padding: 32px 16px 64px; }
.wide { max-width: 1100px; margin: 0 auto; padding: 16px 16px 64px; }
.center-page { text-align: center; padding-top: 12vh; }
.home-logos { width: 210px; max-width: 70%; display: block; margin: 0 auto 12px; }

.card { background: var(--paper); border: 1px solid var(--line); border-radius: 18px; padding: 28px 24px; box-shadow: 0 2px 10px rgba(20, 50, 45, .05); }
.start-card { text-align: center; }
.start-card .meta, .result-card .meta { color: var(--muted); margin: 0 0 16px; }

.stack { display: grid; gap: 14px; text-align: start; margin: 18px 0 10px; }
.stack label { display: grid; gap: 6px; font-weight: 500; }
input[type=text], input[type=password], input[type=search], select {
  font: inherit; padding: 11px 13px; border: 1.5px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink); width: 100%;
}
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible { outline: 3px solid rgba(29, 111, 99, .35); outline-offset: 2px; border-color: var(--teal); }

.btn { font: inherit; font-weight: 500; display: inline-flex; align-items: center; gap: 6px; justify-content: center; padding: 9px 16px; border-radius: 10px;
  border: 1.5px solid var(--line); background: #fff; color: var(--ink); cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { border-color: var(--teal); color: var(--teal); }
.btn.primary { background: var(--teal); border-color: var(--teal); color: #fff; }
.btn.primary:hover { background: var(--teal-dark); color: #fff; }
.btn.big { padding: 14px 22px; font-size: 1.05rem; }
.btn.small { padding: 5px 11px; font-size: .85rem; border-radius: 8px; }
.btn.ghost { background: transparent; }
.btn.danger:hover { border-color: var(--bad); color: var(--bad); }

.error { background: var(--bad-soft); color: var(--bad); border-radius: 10px; padding: 10px 14px; margin: 12px 0 0; }
.flash { position: fixed; top: 14px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 10px; z-index: 50;
  box-shadow: 0 6px 20px rgba(0,0,0,.2); animation: fadeout 5s forwards; max-width: calc(100% - 32px); }
@keyframes fadeout { 0%, 80% { opacity: 1 } 100% { opacity: 0; visibility: hidden } }

/* ---------- exam paper ---------- */
.paper { background: var(--paper); max-width: 860px; margin: 0 auto; padding: 28px 36px 40px; border: 1px solid var(--line); border-radius: 6px; box-shadow: 0 2px 14px rgba(20, 50, 45, .06); }
.taking-paper { margin: 18px auto 80px; }

.letterhead { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 16px; padding-bottom: 14px; }
.lh-info { font-size: .9rem; line-height: 1.55; justify-self: start; text-align: right; }
.lh-logos { height: 96px; justify-self: center; }
.lh-ksa { height: 72px; justify-self: end; }

.namebar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; border: 1.5px solid var(--teal); border-radius: 12px; padding: 10px 16px; margin-bottom: 18px; }
.nb-label { color: var(--muted); font-size: .95rem; }
.nb-val { flex: 1 1 160px; border-bottom: 1px solid var(--line); min-height: 1.5em; font-weight: 700; font-size: 1.05rem; padding: 0 4px; unicode-bidi: plaintext; }
.nb-val.short { flex: 0 1 90px; }
.nb-score .markbox { width: 64px; }

.sec { margin: 0 0 26px; }
.sec-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; }
.sec h2 { font-size: 1.12rem; margin: 6px 0 4px; text-decoration: underline; text-underline-offset: 4px; }
.part h3 { font-size: 1rem; font-weight: 600; margin: 10px 0 8px; text-decoration: underline; text-underline-offset: 3px; text-decoration-thickness: 1px; }
.note { font-size: .85rem; color: var(--muted); margin: -4px 0 8px; }

.markbox { display: grid; grid-template-rows: 30px 30px; width: 76px; border: 1.5px solid var(--ink); flex: none; text-align: center; font-weight: 700; background: #fff; }
.markbox .got { border-bottom: 1.5px solid var(--ink); line-height: 28px; color: var(--teal-dark); }
.markbox .of { line-height: 28px; }

.items { margin: 0; padding-left: 1.6em; }
.items > li { margin: 0 0 12px; padding: 2px 0; }
.q { margin: 0 0 6px; font-weight: 500; }
.opts { display: flex; flex-wrap: wrap; gap: 6px 10px; }

.opt { display: inline-flex; align-items: center; gap: 8px; padding: 7px 12px; border: 1.5px solid var(--line); border-radius: 10px; cursor: pointer; background: #fff; }
label.opt:hover { border-color: var(--teal); }
.opt input, .pill input { accent-color: var(--teal); width: 18px; height: 18px; margin: 0; }
label.opt:has(input:checked) { border-color: var(--teal); background: var(--teal-soft); }

.pill { display: inline-flex; align-items: center; gap: 6px; padding: 4px 11px; border: 1.5px solid var(--line); border-radius: 999px; cursor: pointer; margin: 2px 1px; background: #fff; font-weight: 600; }
.pill input { position: absolute; opacity: 0; pointer-events: none; }
.pill:has(input:checked) { background: var(--teal); border-color: var(--teal); color: #fff; }
.pill:has(input:focus-visible) { outline: 3px solid rgba(29, 111, 99, .35); }
.choice { white-space: normal; }

.blank { color: var(--muted); letter-spacing: 1px; }
.bank { border: 1.5px solid var(--ink); text-align: center; font-weight: 700; padding: 8px 14px; margin: 6px auto 14px; max-width: 520px; word-spacing: 3px; }
.bank span { font-weight: 400; color: var(--muted); }
.cloze { border: 1.5px solid var(--ink); padding: 12px 18px; }
.cloze h4 { text-align: center; margin: 0 0 8px; text-decoration: underline; }
.cloze p { margin: 0; line-height: 2.6; }
.slot select, .cell select { width: auto; display: inline-block; padding: 5px 8px; font-size: .95rem; border-radius: 8px; }
.cell select { width: 100%; }

.grid { display: grid; grid-template-columns: repeat(4, 1fr); border: 1.5px solid var(--ink); }
.cell { border-right: 1.5px solid var(--ink); display: flex; flex-direction: column; break-inside: avoid; }
.cell:last-child { border-right: 0; }
.cell img { width: 100%; height: 120px; object-fit: contain; padding: 8px; border-bottom: 1.5px solid var(--ink); background: #fff; }
.cell-body { padding: 8px; display: grid; gap: 6px; align-content: start; flex: 1; }
.cell-body.center { justify-items: center; text-align: center; align-content: center; }
.cell .opt { padding: 6px 8px; font-size: .92rem; }
.cell-mark:empty { display: none; }

.word { font-size: 1.15rem; font-weight: 600; letter-spacing: .5px; }
.letter { width: 2.1em !important; text-align: center; padding: 4px 0 !important; margin: 0 2px; font-size: 1.1rem !important; font-weight: 700; border: 2px solid var(--teal) !important; border-radius: 8px !important; display: inline-block; }
.line { margin-top: 2px; }
.passage { background: var(--teal-soft); border-radius: 10px; padding: 12px 16px; margin: 0 0 12px; line-height: 1.7; }
.tf-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px; }
.tf-row > span:first-child { flex: 1 1 260px; }

/* answered indicator while taking */
.taking [data-q] { transition: background .15s; }

/* ---------- marked paper ---------- */
.fill { display: inline-block; min-width: 3em; border-bottom: 1.5px solid var(--ink); padding: 0 6px; font-weight: 700; color: #1b3d8f; text-align: center; }
.letter-fill { display: inline-block; min-width: .9em; border-bottom: 2px solid var(--ink); text-align: center; font-weight: 800; color: #1b3d8f; margin: 0 1px; }
.written { margin: 2px 0 0; padding: 4px 8px; border-bottom: 1.5px solid var(--ink); color: #1b3d8f; font-weight: 600; }
.tf-fill { font-weight: 700; color: #1b3d8f; }
span.opt { cursor: default; }
.opt.chosen { border-color: #1b3d8f; background: #e9eefb; color: #1b3d8f; font-weight: 600; }
.opt.right { border: 2px solid var(--ok); padding: 6px 11px; }
.opt.chosen.right { background: var(--ok-soft); color: var(--ok); }
.pick { padding: 0 2px; }
.pick.chosen { border: 2px solid #1b3d8f; border-radius: 50%; padding: 0 6px; font-weight: 700; color: #1b3d8f; }
.pick.right:not(.chosen) { text-decoration: underline; text-decoration-color: var(--ok); text-decoration-thickness: 2px; }

.mk { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; font-size: .8rem; font-weight: 800; margin-left: 6px; vertical-align: middle; color: #fff; }
.mk.ok { background: var(--ok); }
.mk.bad { background: var(--bad); }
.mk.part { background: var(--part); }
.corr { margin-left: 6px; font-size: .85rem; color: var(--ok); font-weight: 600; }
.corr::before { content: "→ "; }
.ov { display: none; align-items: center; gap: 4px; margin-left: 8px; font-size: .8rem; color: var(--muted); vertical-align: middle; }
.ov input { width: 64px; padding: 3px 6px; font: inherit; border: 1.5px solid var(--part); border-radius: 6px; }
body.editing .ov { display: inline-flex; }
.paper-foot { margin-top: 24px; padding-top: 10px; border-top: 1px solid var(--line); font-size: .8rem; color: var(--muted); text-align: center; }

/* ---------- taking ---------- */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px;
  background: var(--teal); color: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.12); }
.tb-who { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; unicode-bidi: plaintext; }
.tb-progress { font-size: .9rem; opacity: .9; white-space: nowrap; }
.tb-timer { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 1.15rem; background: rgba(255,255,255,.15); padding: 4px 12px; border-radius: 8px; }
.tb-timer.low { background: #fff; color: var(--bad); }
.submit-row { text-align: center; margin-top: 30px; }

/* ---------- result ---------- */
.result-card { text-align: center; }
.result-card h1 { unicode-bidi: plaintext; }
.score-ring { --pct: 0; width: 170px; height: 170px; border-radius: 50%; margin: 18px auto 10px; display: grid; place-items: center;
  background: conic-gradient(var(--teal) calc(var(--pct) * 1%), var(--teal-soft) 0); }
.score-ring > div { width: 136px; height: 136px; border-radius: 50%; background: #fff; display: grid; place-content: center; }
.score-ring strong { font-size: 2.6rem; line-height: 1; }
.score-ring span { color: var(--muted); }
.cheer { font-size: 1.2rem; font-weight: 600; color: var(--teal-dark); margin: 4px 0 18px; }
.breakdown { width: 100%; border-collapse: collapse; margin: 0 0 14px; text-align: start; }
.breakdown th, .breakdown td { padding: 9px 4px; border-bottom: 1px solid var(--line); }
.breakdown th { font-weight: 500; text-align: left; }
.breakdown td { text-align: right; font-weight: 700; white-space: nowrap; }
.review { margin-top: 20px; }
.review summary { cursor: pointer; font-weight: 600; color: var(--teal); padding: 10px 0; }
.review .paper { padding: 18px; max-width: 100%; }

/* ---------- teacher ---------- */
.appbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px; background: #fff; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.brand img { height: 44px; }
.paper-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.tabs { display: flex; gap: 6px; margin: 8px 0 18px; border-bottom: 1px solid var(--line); }
.tabs a { padding: 10px 16px; text-decoration: none; color: var(--muted); font-weight: 500; border-bottom: 3px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--teal); border-color: var(--teal); }
.count { display: inline-block; min-width: 22px; padding: 0 6px; border-radius: 999px; background: var(--teal-soft); color: var(--teal-dark); font-size: .8rem; text-align: center; }
.exam-bar { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
.linkbox { width: 260px; max-width: 100%; padding: 4px 8px !important; font-size: .85rem !important; display: inline-block; border-radius: 6px !important; }
.toggle-form { display: flex; align-items: center; gap: 10px; }
.status { font-size: .85rem; font-weight: 600; padding: 4px 10px; border-radius: 999px; }
.status.on { background: var(--ok-soft); color: var(--ok); }
.status.off { background: var(--bad-soft); color: var(--bad); }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin: 16px 0; }
.stats > div { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; }
.stats span { display: block; font-size: .8rem; color: var(--muted); }
.stats strong { font-size: 1.6rem; }
.stats em { font-style: normal; color: var(--muted); margin-left: 4px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.filters input[type=search] { flex: 1 1 200px; width: auto; padding: 7px 11px; }
.filters select { width: auto; padding: 7px 11px; }
.filters .ghost { margin-left: auto; }
.table-wrap { overflow-x: auto; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); }
.results { width: 100%; border-collapse: collapse; }
.results th, .results td { padding: 10px 14px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.results tr:last-child td { border-bottom: 0; }
.results th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: #fafcfb; }
.results th a { color: inherit; text-decoration: none; }
.results td:first-child a { font-weight: 600; text-decoration: none; unicode-bidi: plaintext; }
.num { text-align: right !important; white-space: nowrap; }
.actions { white-space: nowrap; text-align: right !important; }
.actions form { display: inline; }
.score { font-weight: 700; padding: 3px 9px; border-radius: 8px; }
.band-a { background: var(--ok-soft); color: var(--ok); }
.band-b { background: var(--teal-soft); color: var(--teal-dark); }
.band-c { background: #fdf3df; color: #8a5a00; }
.band-d { background: var(--bad-soft); color: var(--bad); }
.empty { background: #fff; border: 1px dashed var(--line); border-radius: var(--radius); padding: 40px; text-align: center; color: var(--muted); }
.paper-page .paper { margin: 20px auto 60px; }
.savebar { max-width: 860px; margin: 14px auto 0; background: #fdf3df; border: 1px solid #f0d9a8; border-radius: 10px; padding: 8px 14px; font-size: .9rem; }

/* PDF capture: no shadows, fixed width */
.pdf-mode .paper { box-shadow: none; border: 0; border-radius: 0; width: 720px; max-width: none; padding: 4px 6px; }


/* ---------- exam links ---------- */
.brand { text-decoration: none; color: var(--ink); }
.card-flat { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; margin: 18px 0 26px; }
.card-flat h1 { font-size: 1.3rem; margin: 0; }
.card-flat > p { margin: 2px 0 12px; }
.create-form { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.create-form label { display: grid; gap: 4px; font-size: .85rem; font-weight: 500; flex: 1 1 180px; }
.create-form label.grow { flex: 2 1 260px; }
.create-form em { font-style: normal; font-weight: 400; }
.create-form input, .create-form select { padding: 9px 11px; }
.create-form .btn { padding: 10px 18px; }
.list-head { font-size: 1.05rem; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; }
.links { display: grid; gap: 12px; }
.link-card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 18px; }
.link-card.is-new { border: 2px solid var(--teal); box-shadow: 0 0 0 4px var(--teal-soft); animation: pop .5s ease-out; }
@keyframes pop { from { transform: scale(.98); opacity: .4 } to { transform: none; opacity: 1 } }
.new-note { margin: 0 0 8px; color: var(--teal-dark); font-weight: 600; font-size: .9rem; }
.lc-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.lc-top h3 { margin: 0; font-size: 1.05rem; }
.lc-top h3 a { color: var(--ink); text-decoration: none; unicode-bidi: plaintext; }
.lc-top h3 a:hover { color: var(--teal); }
.lc-top p { margin: 2px 0 0; }
.copyrow { display: flex; gap: 8px; margin: 10px 0; max-width: 560px; }
.copyrow .linkbox { flex: 1; width: auto; padding: 8px 10px !important; font-size: .92rem !important; border-radius: 8px !important;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: #f7faf9; }
.copy-btn.copied { background: var(--ok); border-color: var(--ok); color: #fff; }
.lc-bottom { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.lc-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.lc-actions form { display: inline; }
.crumb { margin: 14px 0 4px; font-size: .9rem; }
.crumb a { text-decoration: none; }
.exam-bar .copyrow { margin-top: 6px; }

@media (max-width: 720px) {
  .paper { padding: 16px 14px 28px; border-radius: 0; border-left: 0; border-right: 0; }
  .letterhead { grid-template-columns: 1fr 1fr; }
  .lh-info { display: none; }
  .lh-logos { height: 52px; justify-self: start; }
  .lh-ksa { height: 48px; }
  .grid { grid-template-columns: repeat(2, 1fr); }
  .cell:nth-child(2n) { border-right: 0; }
  .cell:nth-child(-n+2) { border-bottom: 1.5px solid var(--ink); }
  .markbox { width: 60px; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .filters .ghost { margin-left: 0; }
  .results td, .results th { padding: 9px 10px; }
  .tb-progress { display: none; }
}

@media print {
  @page { size: A4; margin: 10mm; }
  body { background: #fff; font-size: 13px; }
  .no-print, .appbar, .topbar, .flash, .savebar { display: none !important; }
  .paper, .paper-page .paper { box-shadow: none; border: 0; margin: 0; padding: 0; max-width: none; }
  .sec-head, .items > li, .grid, .cloze, .passage, .part-picture_choice, .part-picture_label, .part-letters { break-inside: avoid; }
  .sec-head, .part h3, .bank { break-after: avoid; }
  .opt { padding: 3px 8px; }
  .cell img { height: 95px; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
