:root {
  --primary: #0f766e;
  --primary-dark: #115e59;
  --primary-light: #ccfbf1;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: 'Cairo', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}
a { color: var(--primary); }
button { font-family: inherit; cursor: pointer; }

/* ===== تسجيل الدخول ===== */
.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  padding: 20px;
}
.login-card {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0,0,0,.2);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}
.login-logo { display: block; width: 110px; height: 110px; object-fit: contain; margin: 0 auto 14px; }
.login-card h1 { font-size: 26px; text-align: center; color: var(--primary-dark); }
.login-card p.sub { text-align: center; color: var(--muted); margin-bottom: 22px; font-size: 14px; }
.login-tabs { display: flex; gap: 6px; background: var(--bg); padding: 5px; border-radius: 12px; margin-bottom: 22px; }
.login-tab { flex: 1; padding: 10px; border: none; background: transparent; border-radius: 9px; font-weight: 700; font-size: 14px; color: var(--muted); }
.login-tab.active { background: var(--surface); color: var(--primary-dark); box-shadow: var(--shadow); }

/* ===== نماذج ===== */
.field { margin-bottom: 16px; }
.field label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 6px; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-family: inherit;
  font-size: 15px;
  background: #fff;
  transition: border-color .15s, box-shadow .15s;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}
.field textarea { resize: vertical; min-height: 80px; }

.btn {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 11px 20px;
  border-radius: 10px;
  font-weight: 700;
  font-size: 15px;
  transition: background .15s, transform .05s;
}
.btn:hover { background: var(--primary-dark); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 12px; font-size: 13px; border-radius: 8px; }
.btn-ghost { background: transparent; color: var(--primary); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #b91c1c; }

.error-msg { color: var(--danger); font-size: 14px; margin-top: 10px; text-align: center; min-height: 20px; }

/* ===== التخطيط ===== */
.layout { display: grid; grid-template-columns: 250px 1fr; height: 100vh; overflow: hidden; }
.sidebar {
  background: var(--primary-dark);
  color: #fff;
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}
.sidebar .brand { display: flex; flex-direction: column; align-items: center; gap: 10px; font-size: 20px; font-weight: 800; text-align: center; padding: 4px 10px 20px; border-bottom: 1px solid rgba(255,255,255,.12); margin-bottom: 16px; }
.sidebar .brand-logo { width: 84px; height: 84px; object-fit: contain; background: #fff; border-radius: 50%; padding: 6px; box-shadow: 0 2px 8px rgba(0,0,0,.18); }
.sidebar nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.sidebar nav a {
  color: #cbd5e1;
  text-decoration: none;
  padding: 11px 14px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 15px;
  transition: background .15s, color .15s;
  display: flex;
  align-items: center;
  gap: 10px;
}
.sidebar nav a .nav-ico { display: inline-flex; flex-shrink: 0; }
.sidebar nav a .nav-ico svg { width: 19px; height: 19px; display: block; }
.sidebar nav a:hover { background: rgba(255,255,255,.08); color: #fff; }
.sidebar nav a.active { background: var(--primary); color: #fff; }
.sidebar .user-box { border-top: 1px solid rgba(255,255,255,.12); padding-top: 14px; margin-top: 14px; }
.sidebar .user-box .name { font-weight: 700; }
.sidebar .user-box .role { font-size: 13px; color: #94a3b8; margin-bottom: 10px; }

.main { padding: 28px 32px; overflow-y: auto; }
.page-title { font-size: 24px; font-weight: 800; margin-bottom: 4px; }
.page-sub { color: var(--muted); margin-bottom: 22px; }

/* ===== بطاقات الإحصائيات ===== */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 20px; margin-bottom: 26px; }
.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 28px 30px;
  box-shadow: var(--shadow);
  border-inline-start: 6px solid var(--primary);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 10px;
}
.stat-card .label { color: var(--muted); font-size: 16px; font-weight: 600; }
.stat-card .value {
  font-size: 42px;
  font-weight: 800;
  color: var(--primary-dark);
  line-height: 1.1;
  white-space: nowrap;
}
/* القيم النصية الطويلة كالتواريخ: حجم منسجم يبقى على سطر واحد */
.stat-card .value.value-date { font-size: 30px; letter-spacing: .5px; }

/* شريط عنوان ملوّن داخل البطاقات (الفلاتر / تقارير تفصيلية) */
.card > .bar-title {
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  color: #fff;
  text-align: center;
  font-size: 18px;
  margin: -22px -22px 18px;
  padding: 14px 20px;
  border-radius: var(--radius) var(--radius) 0 0;
}

/* شبكة فلاتر التقارير */
.filters-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; }
.filters-grid .field { margin: 0; }

/* بطاقات تمييز ترتيب العصب (الأعلى حضورًا / الأعلى غيابًا) */
.rank-card {
  position: relative;
  border-radius: 16px;
  padding: 24px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
  border: 1px solid var(--border);
  overflow: hidden;
  transition: transform .15s ease, box-shadow .15s ease;
}
.rank-card:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(0,0,0,.10); }
/* شريط علوي ملوّن */
.rank-card::before { content: ""; position: absolute; inset-block-start: 0; inset-inline: 0; height: 5px; }
.rank-card.top    { background: linear-gradient(180deg, #f0fdfa, #ffffff); }
.rank-card.top::before    { background: var(--primary); }
.rank-card.bottom { background: linear-gradient(180deg, #fef2f2, #ffffff); }
.rank-card.bottom::before { background: #dc2626; }
.rank-card-head { display: flex; align-items: center; gap: 12px; }
/* أيقونة داخل دائرة */
.rank-badge {
  width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center; font-size: 22px; flex-shrink: 0;
  background: #fff;
}
.rank-card.top .rank-badge    { box-shadow: 0 0 0 3px var(--primary-light); }
.rank-card.bottom .rank-badge { box-shadow: 0 0 0 3px #fecaca; }
.rank-label { color: var(--muted); font-weight: 700; font-size: 15px; }
.rank-name { font-size: 18px; font-weight: 800; color: var(--primary-dark); line-height: 1.35; }
.rank-card.bottom .rank-name { color: #b91c1c; }
.rank-rate { display: flex; align-items: center; gap: 14px; }
.rank-rate b { font-size: 30px; font-weight: 800; color: var(--primary-dark); min-width: 84px; }
.rank-rate .rate-bar { flex: 1; height: 10px; }
.rank-card.bottom .rank-rate b { color: #dc2626; }
.rank-card.bottom .rate-bar > span { background: #dc2626; }

/* ===== أقسام وبطاقات ===== */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
  margin-bottom: 22px;
}
.card h3 { font-size: 18px; margin-bottom: 16px; }
.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.card-header h3 { margin: 0; }

/* شريط رأس ملوّن يمتد على عرض البطاقة */
.card-header-bar {
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  margin: -22px -22px 18px;
  padding: 14px 22px;
  border-radius: var(--radius) var(--radius) 0 0;
  color: #fff;
}
.card-header-bar h3 { color: #fff; }
.card-header-bar .btn { background: #fff; color: var(--primary-dark); }
.card-header-bar .btn:hover { background: var(--primary-light); }
.charts-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 20px; }
.chart-box { position: relative; height: 220px; }

/* ===== جداول ===== */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: right; padding: 12px 14px; border-bottom: 1px solid var(--border); font-size: 15px; }
th { color: var(--muted); font-weight: 700; font-size: 13px; background: #f8fafc; }
tbody tr:hover { background: #f8fafc; }
.table-wrap { overflow-x: visible; }
.actions-cell { display: flex; gap: 8px; flex-wrap: wrap; }
/* عمود الإجراءات عند الطرف الخارجي (الرأس + الأزرار معًا) */
.th-acts-edge { text-align: left; }
.td-acts-edge { justify-content: flex-end; }

/* ===== شارات ===== */
.badge { padding: 3px 12px; border-radius: 999px; font-size: 13px; font-weight: 700; display: inline-block; }
.badge-present { background: #dcfce7; color: #15803d; }
.badge-absent { background: #fee2e2; color: #b91c1c; }
.badge-online { background: #dbeafe; color: #1d4ed8; }
.code-badge { font-family: ui-monospace, "Courier New", monospace; background: var(--primary-light); color: var(--primary-dark); padding: 2px 8px; border-radius: 6px; font-size: 13px; font-weight: 700; letter-spacing: .5px; }
.rate-bar { background: var(--border); border-radius: 999px; height: 8px; overflow: hidden; min-width: 80px; }
.rate-bar > span { display: block; height: 100%; background: var(--primary); }

/* ===== نافذة منبثقة ===== */
.modal-overlay { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal { background: var(--surface); border-radius: 16px; padding: 0; width: 100%; max-width: 480px; max-height: 90vh; overflow-y: auto; box-shadow: 0 20px 50px rgba(0,0,0,.3); }

/* شريط عنوان النافذة */
.modal-header {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  color: #fff; padding: 16px 24px; border-radius: 16px 16px 0 0;
}
.modal-header h3 { margin: 0; color: #fff; font-size: 18px; }
.modal-x {
  background: rgba(255,255,255,.15); border: 0; color: #fff;
  width: 30px; height: 30px; border-radius: 8px; font-size: 20px; line-height: 1;
  cursor: pointer; display: grid; place-items: center; flex-shrink: 0;
}
.modal-x:hover { background: rgba(255,255,255,.3); }
.modal-body { padding: 24px; }
.modal-actions { display: flex; gap: 10px; margin-top: 8px; }

/* نافذة عريضة (مستطيلة) بعمودين — تُستخدم في النماذج كثيرة الحقول مثل إنشاء محاضرة */
.modal-wide { max-width: 860px; }
.modal-wide #modalForm { display: grid; grid-template-columns: 1fr 1fr; gap: 0 18px; }
.modal-wide #modalForm .field-full,
.modal-wide #modalForm .error-msg,
.modal-wide #modalForm .modal-actions { grid-column: 1 / -1; }
.modal-wide #modalForm .field { margin-bottom: 12px; }
.modal-wide #modalForm textarea { min-height: 64px; }
@media (max-width: 640px) {
  .modal-wide #modalForm { grid-template-columns: 1fr; }
}

/* ===== متفرقات ===== */
.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; gap: 12px; flex-wrap: wrap; background: var(--primary-dark); color: #fff; padding: 16px 20px; border-radius: var(--radius); box-shadow: var(--shadow); }
.toolbar .page-title { color: #fff; }
.toolbar .page-sub { color: rgba(255,255,255,.75); margin-bottom: 0; }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; }
.spinner { text-align: center; padding: 40px; color: var(--muted); }
.attendance-toggle { display: flex; gap: 6px; }
.toggle-btn { padding: 6px 16px; border: 1px solid var(--border); background: #fff; border-radius: 8px; font-weight: 600; font-size: 14px; }
.toggle-btn.on-present { background: var(--success); color: #fff; border-color: var(--success); }
.toggle-btn.on-online { background: #1d4ed8; color: #fff; border-color: #1d4ed8; }
.toggle-btn.on-absent { background: var(--danger); color: #fff; border-color: var(--danger); }
.flex { display: flex; gap: 10px; align-items: center; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
select.filter { padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px; font-family: inherit; }

/* ===== الطباعة / PDF ===== */
.report-print { background: #fff; padding: 30px; border-radius: var(--radius); box-shadow: var(--shadow); }
.report-print h2 {
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  color: #fff;
  text-align: center;
  margin: 0 0 14px;
  padding: 14px 20px;
  border-radius: var(--radius);
  font-size: 20px;
}
.report-print .report-date {
  width: fit-content;
  margin: 0 auto 24px;
  background: var(--primary-light);
  color: var(--primary-dark);
  padding: 8px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 700;
  border: 1px solid #99f6e4;
}
.report-print .report-date::before { content: "📅 "; }
.report-print .report-date b { font-size: 16px; font-weight: 800; }
/* رأس جداول التقارير ملوّن */
.report-print thead th { background: var(--primary); color: #fff; font-size: 14px; }
.report-print thead th:first-child { border-start-start-radius: 8px; }
.report-print thead th:last-child { border-start-end-radius: 8px; }
@media print {
  .sidebar, .toolbar, .no-print, .btn { display: none !important; }
  .layout { display: block; }
  .main { padding: 0; }
  .card, .report-print { box-shadow: none; border: 1px solid var(--border); }
  body { background: #fff; }
}

@media (max-width: 768px) {
  .layout { grid-template-columns: 1fr; height: auto; overflow: visible; }
  .sidebar { flex-direction: row; flex-wrap: wrap; height: auto; overflow: visible; }
  .sidebar nav { flex-direction: row; flex-wrap: wrap; }
  .main { height: auto; }
}
