:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --text: #1c2630;
  --muted: #66727f;
  --line: #dde3e8;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --primary-soft: #e0f2ef;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --info: #1d4ed8;
  --info-soft: #dbeafe;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --font: "Segoe UI", Tahoma, "Noto Sans Arabic", "Noto Kufi Arabic", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--primary); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- الهيدر والتنقل ---------- */
/* الهيدر كاملاً (الشعار + القائمة) ثابت أعلى الشاشة. الجوال: يختفي عند التمرير للأسفل ويظهر للأعلى (hideOnScroll في common.js) */
body > header.app-hdr { position: sticky; top: 0; z-index: 50; transition: transform .2s ease; box-shadow: 0 2px 6px rgba(0, 0, 0, .12); }
body > header.app-hdr.hdr-hide { transform: translateY(-100%); box-shadow: none; }
/* شريط "نسخة تجريبية" (demoMode في config.json): ثابت فوق الهيدر، لا يُطبع */
.demo-bar { position: fixed; top: 0; left: 0; right: 0; z-index: 60; height: 22px; line-height: 22px; text-align: center;
  font-size: 12px; font-weight: 700; background: #b45309; color: #fff; pointer-events: none; }
html.demo body { padding-top: 22px; }
html.demo body > header.app-hdr { top: 22px; }
@media (min-width: 701px) { html.demo { scroll-padding-top: calc(var(--hdr-h, 0px) + 30px); } }
@media print { .demo-bar { display: none !important; } html.demo body { padding-top: 0; } }
@media (min-width: 701px) { html { scroll-padding-top: calc(var(--hdr-h, 0px) + 8px); } }
.topbar {
  background: var(--primary); color: var(--primary-ink);
}
.topbar-row { display: flex; align-items: center; gap: 10px; padding: 10px 16px; max-width: 1200px; margin: 0 auto; }
.top-logo { height: 34px; width: auto; background: #fff; border-radius: 8px; padding: 2px 6px; flex: none; }
.brand { font-weight: 700; font-size: 17px; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand small { font-weight: 400; opacity: .85; font-size: 13px; margin-inline-start: 6px; }
.user-btn {
  background: rgba(255, 255, 255, .15); border: 0; color: inherit; border-radius: 999px;
  padding: 6px 12px; cursor: pointer; font-size: 14px; white-space: nowrap;
}
.nav { background: #0b5f58; overflow-x: auto; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }
.nav-inner { display: flex; gap: 2px; padding: 0 8px; max-width: 1200px; margin: 0 auto; }
.nav a {
  color: rgba(255, 255, 255, .82); text-decoration: none; padding: 9px 14px; white-space: nowrap;
  font-size: 15px; border-bottom: 3px solid transparent;
}
.nav a.active { color: #fff; border-bottom-color: #5eead4; font-weight: 600; }

.page { max-width: 1200px; margin: 0 auto; padding: 16px; }
.page-narrow { max-width: 720px; }

/* ---------- عناصر عامة ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px; }
.card h2, .card h3 { margin: 0 0 12px; font-size: 18px; }
.card h3 { font-size: 16px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.grow { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.mt { margin-top: 12px; }
.mb0 { margin-bottom: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: 9px 16px; border-radius: 10px; cursor: pointer; font-weight: 600; font-size: 15px;
  min-height: 42px; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { background: var(--bad); color: #fff; border-color: var(--bad); }
.btn.warn { background: var(--warn); color: #fff; border-color: var(--warn); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.sm { min-height: 34px; padding: 5px 10px; font-size: 14px; }
.btn.lg { min-height: 52px; font-size: 17px; padding: 12px 22px; }
.btn.block { width: 100%; }

label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 4px; }
.field { margin-bottom: 12px; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=time], input[type=search], select, textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; background: #fff;
  min-height: 42px; font-size: 16px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: var(--primary); }
.inline-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; align-items: end; }
.inline-fields > * { min-width: 0; }
/* الآيفون (Safari): خانة التاريخ/الوقت لها عرض أدنى ثابت فتخرج من عمودها، والتاريخ الإنجليزي يُقلب في RTL ("Oct 2026 12") */
@supports (-webkit-touch-callout: none) {
  input[type=date], input[type=time] { -webkit-appearance: none; appearance: none; display: block; min-width: 0; max-width: 100%; direction: ltr; }
  input[type=date]::-webkit-date-and-time-value, input[type=time]::-webkit-date-and-time-value { text-align: right; }
}

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 13px; font-weight: 600; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.present { background: var(--ok-soft); color: var(--ok); }
.badge.late { background: var(--warn-soft); color: var(--warn); }
.badge.absent { background: var(--bad-soft); color: var(--bad); }
.badge.excused { background: var(--info-soft); color: var(--info); }
.badge.dismissed { background: #ede9fe; color: #6d28d9; }
.badge.none { background: #eef1f4; color: var(--muted); }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 8px 16px;
  cursor: pointer; font-weight: 600; min-height: 42px; font-size: 15px;
}
.chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.chip .tick { color: var(--ok); margin-inline-start: 4px; }
.chip.active .tick { color: #a7f3d0; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 10px 14px; cursor: pointer; font-weight: 600; color: var(--muted); border-bottom: 3px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; text-align: center; }
.stat b { display: block; font-size: 26px; line-height: 1.2; }
.stat span { color: var(--muted); font-size: 14px; }
.stat.ok b { color: var(--ok); } .stat.warn b { color: var(--warn); } .stat.bad b { color: var(--bad); } .stat.info b { color: var(--info); }

/* ---------- الجداول ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: 15px; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: right; vertical-align: middle; }
th { background: var(--surface-2); font-weight: 700; font-size: 14px; white-space: nowrap; position: sticky; top: 0; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fafcfc; }
td.num, th.num { text-align: center; width: 1%; white-space: nowrap; }
td.zero { color: #b8c0c8; }
td.nmc { min-width: 170px; }
@media print { td.nmc { min-width: 0; } }
.cell-p { text-align: center; width: 44px; cursor: pointer; user-select: none; font-weight: 700; }
.cell-p.abs { background: var(--bad-soft); color: var(--bad); }
.cell-p.exc { background: var(--info-soft); color: var(--info); }
.cell-p.off { background: #f1f3f5; color: #aab3bb; cursor: default; }
.cell-p.dis { background: #f3effe; color: #8b5cf6; cursor: default; }

/* ---------- Toast + Modal ---------- */
#toasts { position: fixed; bottom: 16px; left: 16px; right: 16px; z-index: 200; display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; }
.toast { background: #1f2937; color: #fff; padding: 10px 18px; border-radius: 10px; box-shadow: 0 6px 20px rgba(0, 0, 0, .2); max-width: 520px; pointer-events: auto; animation: tin .2s ease-out; }
.toast.ok { background: var(--ok); } .toast.bad { background: var(--bad); } .toast.warn { background: var(--warn); }
@keyframes tin { from { transform: translateY(10px); opacity: 0; } }

.modal-back { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); z-index: 150; display: flex; align-items: flex-end; justify-content: center; padding: 0; }
.modal {
  background: var(--surface); width: 100%; max-width: 560px; max-height: 92vh; overflow: auto;
  border-radius: 16px 16px 0 0; padding: 18px 16px; box-shadow: 0 -10px 30px rgba(0, 0, 0, .2);
}
.modal h3 { margin: 0 0 12px; }
@media (min-width: 640px) {
  .modal-back { align-items: center; padding: 16px; }
  .modal { border-radius: 16px; }
}
.modal-actions { display: flex; gap: 8px; justify-content: flex-start; margin-top: 16px; flex-wrap: wrap; }
/* قائمة المستخدم: أزرار متساوية في عمودين */
.menu-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }
.menu-grid .btn { width: 100%; min-height: 48px; padding: 10px 8px; }
.menu-grid .wide { grid-column: 1 / -1; }
.menu-grid .menu-label { grid-column: 1 / -1; font-size: 13px; font-weight: 700; color: var(--muted); margin: 4px 2px -4px; }
/* شريط "فعّل الإشعارات" في صفحة المعلم (pushNudge) */
.push-nudge { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: space-between; }
.push-nudge-btns { display: flex; gap: 8px; margin-inline-start: auto; }
.menu-grid .menu-sep { grid-column: 1 / -1; border-top: 1px solid var(--line, #e5e7eb); margin: 4px 0 0; }
.btn.danger-soft { background: var(--bad-soft); color: var(--bad); border-color: #fecaca; }
.btn.danger-soft:hover { background: #fecaca; }
.btn.primary-soft { background: var(--primary-soft); color: var(--primary); border-color: #b2dfd8; font-weight: 700; }
.btn.muted-btn { background: #eef1f4; color: var(--muted); border-color: #eef1f4; }
.btn.muted-btn:hover { background: #e2e7ec; }

.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.notice { padding: 12px 14px; border-radius: 10px; background: var(--info-soft); color: #1e3a8a; margin-bottom: 14px; }
.notice.warn { background: var(--warn-soft); color: #78350f; }
.notice.bad { background: var(--bad-soft); color: #7f1d1d; }
.notice.ok { background: var(--ok-soft); color: #14532d; }

/* ---------- شاشة المعلم: بطاقات الطلاب ---------- */
.period-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.period-num { background: var(--primary); color: #fff; border-radius: 12px; padding: 8px 14px; font-weight: 700; font-size: 18px; }
.countdown { font-variant-numeric: tabular-nums; }
.students-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 8px; }
.st {
  display: flex; align-items: center; gap: 10px; background: var(--surface); border: 2px solid var(--line);
  border-radius: 12px; padding: 10px 12px; cursor: pointer; user-select: none; min-height: 58px; text-align: right; width: 100%;
}
.st .n { width: 28px; height: 28px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; font-size: 13px; color: var(--muted); flex: none; }
.st .nm { flex: 1; font-weight: 600; font-size: 15px; line-height: 1.35; }
.st .tag { font-size: 12px; }
.st.abs { border-color: var(--bad); background: var(--bad-soft); }
.st.abs.exc { border-color: var(--info); background: var(--info-soft); }
.st.abs .n { background: var(--bad); color: #fff; }
.st.faded { opacity: .42; cursor: not-allowed; background: #eef1f4; }
.st.faded .nm { text-decoration: line-through; text-decoration-color: rgba(0, 0, 0, .25); }
.savebar {
  position: sticky; bottom: 0; z-index: 40; background: var(--surface); border-top: 1px solid var(--line);
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); display: flex; align-items: center; gap: 12px; margin: 16px -16px -16px;
  box-shadow: 0 -4px 12px rgba(0, 0, 0, .06);
}

/* ---------- البوابة ---------- */
.scanner { position: relative; background: #000; border-radius: var(--radius); overflow: hidden; aspect-ratio: 4 / 3; max-height: 52vh; width: 100%; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner .frame { position: absolute; inset: 18% 22%; border: 3px solid rgba(94, 234, 212, .9); border-radius: 14px; box-shadow: 0 0 0 999px rgba(0, 0, 0, .25); pointer-events: none; }
.scanner .placeholder { position: absolute; inset: 0; display: grid; place-items: center; color: #cbd5e1; text-align: center; padding: 16px; }
.scan-result { border-radius: var(--radius); padding: 14px 16px; margin: 12px 0; font-size: 18px; font-weight: 700; display: flex; gap: 12px; align-items: center; min-height: 70px; }
.scan-result .big { font-size: 22px; }
.scan-result.present { background: var(--ok-soft); color: var(--ok); border: 2px solid var(--ok); }
.scan-result.late { background: var(--warn-soft); color: var(--warn); border: 2px solid var(--warn); }
.scan-result.already { background: var(--info-soft); color: var(--info); border: 2px solid var(--info); }
.scan-result.error { background: var(--bad-soft); color: var(--bad); border: 2px solid var(--bad); }
.scan-result.idle { background: var(--surface); color: var(--muted); border: 2px dashed var(--line); font-weight: 400; font-size: 16px; }
.gate-layout { display: grid; gap: 16px; }
@media (min-width: 900px) { .gate-layout { grid-template-columns: 1.1fr 1fr; align-items: start; } }

.search-results { border: 1px solid var(--line); border-radius: 10px; margin-top: 6px; max-height: 280px; overflow: auto; background: #fff; }
.search-results button { display: flex; width: 100%; gap: 8px; border: 0; background: none; padding: 10px 12px; text-align: right; cursor: pointer; border-bottom: 1px solid var(--line); align-items: center; }
.search-results button:hover { background: var(--surface-2); }
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: 0; }

/* ---------- بطاقات QR ---------- */
.qr-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
.qr-card { border: 1.5px dashed #94a3b8; border-radius: 10px; padding: 10px; text-align: center; background: #fff; break-inside: avoid; page-break-inside: avoid; }
.qr-card .school { font-size: 11px; color: var(--muted); }
.qr-card .nm { font-weight: 700; font-size: 13px; margin: 4px 0; min-height: 2.6em; line-height: 1.3; }
.qr-card svg { width: 130px; height: 130px; }
.qr-card .id { font-family: Consolas, monospace; font-size: 13px; letter-spacing: 1px; }
.qr-list td svg { width: 64px; height: 64px; display: block; }

/* ---------- الطباعة ---------- */
.print-only { display: none; }
@media print {
  @page { size: A4; margin: 10mm; }
  body { background: #fff; font-size: 12px; }
  body > header.app-hdr, .topbar, .nav, .no-print, #toasts, .savebar { display: none !important; }
  .print-only { display: block; }
  .page { max-width: none; padding: 0; }
  .card { box-shadow: none; border: 0; padding: 0; margin-bottom: 10px; }
  .table-wrap { border: 0; overflow: visible; }
  th, td, tbody tr:last-child td { padding: 4px 6px; border: 1px solid #999; font-size: 11.5px; }
  th { position: static; background: #eee !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .badge { padding: 0; background: none !important; color: #000 !important; }
  .page-break { page-break-after: always; break-after: page; }
  .qr-cards { grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .qr-card svg { width: 110px; height: 110px; }
  tr { break-inside: avoid; }
}
.print-head { text-align: center; margin-bottom: 10px; }
.print-head .school-logo { height: 52px; width: auto; display: block; margin: 0 auto 4px; }
.qr-card .school-logo { height: 22px; width: auto; display: block; margin: 0 auto 2px; }
.print-head h2 { margin: 0; font-size: 18px; }
.print-head div { font-size: 13px; }

/* ---------- تسجيل الدخول ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg, #0f766e, #134e4a); }
.login-card { background: #fff; border-radius: 18px; padding: 28px 22px; width: 100%; max-width: 380px; box-shadow: 0 20px 50px rgba(0, 0, 0, .25); }
.login-card h1 { margin: 0 0 4px; font-size: 22px; text-align: center; }
.login-card .logo { width: 140px; height: auto; margin: 0 auto 12px; display: block; }

/* تسجيل المعلم */
.join-card { max-width: 440px; }
.join-chips { margin-bottom: 8px; }
.join-chips .chip { display: inline-flex; gap: 6px; align-items: center; color: var(--text); cursor: pointer; margin: 0; font-size: 15px; }
.join-chips .chip input { width: auto; }
.join-chips .chip.active { color: #fff; }
.mt-xs { margin-top: 4px; }

/* تثبيت التطبيق */
.install-btn { background: #fff; color: var(--primary); font-weight: 700; }
.install-steps { padding-inline-start: 22px; line-height: 1.9; margin: 8px 0 12px; }
.install-steps li { margin-bottom: 4px; }

@media (max-width: 600px) {
  .install-btn { padding: 6px 10px; font-size: 13px; }
  .page { padding: 12px; }
  .card { padding: 12px; }
  .students-grid { grid-template-columns: 1fr; }
  .savebar { margin: 12px -12px -12px; }
  th, td { padding: 7px 6px; font-size: 14px; }
}

/* شريط "يوجد تحديث" (التحديث التلقائي في common.js) */
.update-bar { position: fixed; bottom: 12px; left: 12px; right: 12px; z-index: 190; margin: 0 auto; max-width: 520px; display: block;
  background: var(--primary); color: #fff; border: 0; border-radius: 12px; padding: 12px 16px; font: inherit; font-weight: 700;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25); cursor: pointer; }
@media print { .update-bar { display: none !important; } }

/* كشف الطلاب المطبوع (students.html): خانات فارغة للكتابة */
.print-sheet { width: 100%; border-collapse: collapse; }
.print-sheet th, .print-sheet td { border: 1px solid #999; padding: 6px; height: 26px; }
.print-sheet td:nth-child(n+3) { min-width: 50px; }
