/* ============================================================
   ESP32 Level 3 – shared stylesheet
   ============================================================ */
* { box-sizing: border-box; }

:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --green: #16a34a;
  --red: #dc2626;
  --amber: #d97706;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(15, 23, 42, .08);
}

html, body { margin: 0; padding: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  min-height: 100vh;
  font-size: 15px;
}

h1 { font-size: 1.6rem; margin: 0 0 6px; }
h2 { font-size: 1.15rem; margin: 0 0 12px; }
h3 { font-size: 1rem; margin: 18px 0 8px; }

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }

.link { color: var(--primary); text-decoration: none; font-weight: 600; }
.link:hover { text-decoration: underline; }

/* ---------- layout ---------- */
.center-page {
  display: grid;
  place-items: center;
  padding: 24px;
}

.container {
  max-width: 980px;
  margin: 0 auto;
  padding: 24px 16px 90px;
}

/* ---------- topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 20px;
  background: #0f172a;
  color: #fff;
  box-shadow: 0 4px 16px rgba(15, 23, 42, .18);
}
.brand { font-weight: 700; letter-spacing: .3px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.who { font-size: .9rem; opacity: .9; }

/* ---------- card ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
  margin-bottom: 18px;
}
.card.narrow { max-width: 420px; margin: 40px auto; }
.card-head {
  display: flex; align-items: baseline;
  justify-content: space-between; gap: 12px;
  margin-bottom: 10px;
}
.login-card { width: 100%; max-width: 400px; }

/* ---------- forms ---------- */
label {
  display: block;
  margin-bottom: 12px;
  font-size: .9rem;
  font-weight: 600;
  color: #334155;
}

input[type="text"],
input[type="password"],
input[type="number"],
textarea {
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font: inherit;
  font-weight: 400;
  color: var(--text);
  background: #fff;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .15);
}

.row-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}
.row-form input { flex: 1 1 180px; margin-top: 0; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border: 1px solid transparent;
  border-radius: 10px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, transform .05s, border-color .15s;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.ghost {
  background: rgba(255,255,255,.08);
  color: #fff;
  border-color: rgba(255,255,255,.25);
}
.btn.ghost:hover { background: rgba(255,255,255,.18); }
.btn.small { padding: 6px 10px; font-size: .82rem; }
.btn.big { padding: 14px 28px; font-size: 1.05rem; }
.btn.block { width: 100%; }
.btn.danger { background: #fee2e2; color: var(--red); border-color: #fecaca; }
.btn.danger:hover { background: #fecaca; }
.btn.amber { background: #fef3c7; color: var(--amber); border-color: #fde68a; }
.btn.amber:hover { background: #fde68a; }
.btn.green { background: #dcfce7; color: var(--green); border-color: #bbf7d0; }
.btn.green:hover { background: #bbf7d0; }
.file-btn { cursor: pointer; }

.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,.3);
  background: rgba(255,255,255,.1);
  font-size: 1.15rem;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background .15s;
}
.icon-btn:hover { background: rgba(255,255,255,.22); }

/* ---------- messages ---------- */
.error { color: var(--red); font-size: .88rem; margin: 8px 0 0; }
.msg { font-size: .88rem; margin: 10px 0 0; min-height: 1.2em; }
.msg.ok { color: var(--green); }
.msg.bad { color: var(--red); }

/* ---------- profile panel ---------- */
.profile-panel {
  position: fixed;
  top: 66px;
  right: 16px;
  width: 300px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 18px 40px rgba(15, 23, 42, .18);
  padding: 16px;
  z-index: 30;
}
.profile-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}
.avatar {
  width: 42px; height: 42px;
  border-radius: 50%;
  background: #e0edff;
  display: grid; place-items: center;
  font-size: 1.2rem;
}
.attempt-list {
  list-style: none;
  margin: 0 0 10px;
  padding: 0;
}
.attempt-list li {
  display: flex;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: 8px;
  background: #f8fafc;
  margin-bottom: 6px;
  font-size: .9rem;
}

/* ---------- exam ---------- */
.q-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  margin-bottom: 16px;
}
.q-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.q-num { font-weight: 700; color: var(--primary); font-size: .9rem; }
.q-pts {
  font-size: .8rem;
  background: #eff6ff;
  color: var(--primary-dark);
  padding: 3px 10px;
  border-radius: 999px;
  font-weight: 700;
}
.q-text { margin: 0 0 12px; font-weight: 500; }

.q-code {
  background: #0f172a;
  color: #e2e8f0;
  padding: 14px 16px;
  border-radius: 10px;
  overflow-x: auto;
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  font-size: .84rem;
  line-height: 1.5;
  margin: 0 0 12px;
  white-space: pre;
}

.options { display: grid; gap: 8px; }
.option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  font-weight: 400;
  margin: 0;
  transition: background .12s, border-color .12s;
}
.option:hover { background: #f8fafc; border-color: #cbd5e1; }
.option input { accent-color: var(--primary); width: 17px; height: 17px; }

.short-input { margin-top: 0; }

.code-input {
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  font-size: .88rem;
  line-height: 1.5;
  background: #f8fafc;
  resize: vertical;
  margin-top: 0;
}

.exam-footer { text-align: center; padding: 10px 0 30px; }

/* ---------- results ---------- */
.result-card { text-align: left; }
.score-big {
  font-size: 3rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1;
  margin: 14px 0 6px;
}
.score-big span { font-size: 1.3rem; color: var(--muted); font-weight: 600; }

.progress {
  height: 12px;
  background: #e2e8f0;
  border-radius: 999px;
  overflow: hidden;
  margin: 10px 0 8px;
}
.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #2563eb, #22c55e);
  border-radius: 999px;
  transition: width .6s ease;
}

.review-item {
  border: 1px solid var(--border);
  border-left: 4px solid #cbd5e1;
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 10px;
  background: #fff;
}
.review-item.ok { border-left-color: var(--green); }
.review-item.partial { border-left-color: var(--amber); }
.review-item.bad { border-left-color: var(--red); }
.review-item summary {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  cursor: pointer;
  font-weight: 600;
  list-style: none;
}
.review-item summary::-webkit-details-marker { display: none; }
.review-item .pts { color: var(--muted); font-weight: 700; white-space: nowrap; }
.review-body { padding-top: 10px; }
.review-body pre {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  overflow-x: auto;
  font-size: .82rem;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 4px 0 10px;
}

/* ---------- table ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
th {
  font-size: .76rem;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--muted);
  background: #f8fafc;
}
tbody tr:hover { background: #f8fafc; }
td.actions { display: flex; gap: 6px; flex-wrap: wrap; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: .76rem;
  font-weight: 700;
  background: #eff6ff;
  color: var(--primary-dark);
  margin: 2px 3px 2px 0;
}
.badge.green { background: #dcfce7; color: var(--green); }
.badge.amber { background: #fef3c7; color: var(--amber); }
.badge.gray  { background: #f1f5f9; color: var(--muted); }

/* ---------- modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .55);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 50;
}
.modal-card {
  position: relative;
  background: #fff;
  border-radius: var(--radius);
  max-width: 760px;
  width: 100%;
  max-height: 82vh;
  overflow-y: auto;
  padding: 26px;
  box-shadow: 0 24px 60px rgba(15, 23, 42, .35);
}
.modal-close {
  position: absolute;
  top: 12px; right: 12px;
  border: none;
  background: #f1f5f9;
  width: 32px; height: 32px;
  border-radius: 50%;
  cursor: pointer;
  font-size: .9rem;
}
.modal-close:hover { background: #e2e8f0; }

.detail-block { margin-bottom: 22px; }
.detail-list { padding-left: 18px; }
.detail-list li { margin-bottom: 10px; }
.detail-list li.ok .d-q { color: var(--green); }
.detail-list li.bad .d-q { color: var(--red); }
.detail-list li.partial .d-q { color: var(--amber); }
.d-q { font-weight: 600; font-size: .9rem; }
.d-q .pts { color: var(--muted); font-weight: 700; }
.d-a {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: .8rem;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 6px 0 0;
}

/* ---------- responsive ---------- */
@media (max-width: 620px) {
  .profile-panel { left: 12px; right: 12px; width: auto; }
  .q-card { padding: 14px; }
  .card { padding: 16px; }
  .score-big { font-size: 2.3rem; }
}

/* ============================================================
   دعم اللغة العربية (RTL)
   ============================================================ */
body { direction: rtl; }

th, td { text-align: right; }
.exercise-title { text-align: right; }

/* الأكواد تبقى LTR حتى داخل واجهة عربية */
.q-code,
.code-input,
textarea.code-input {
  direction: ltr;
  text-align: left;
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
}

/* لوحة الملف الشخصي والمودال على الجهة المقابلة في RTL */
.profile-panel  { left: 16px; right: auto; }
.modal-close    { left: 12px; right: auto; }

/* قائمة النقاط في المودال */
.detail-list { padding-left: 0; padding-right: 18px; }

/* إجابات الطالب في المراجعة: موجّه تلقائيًا حسب المحتوى */
.review-body pre { unicode-bidi: plaintext; }

/* ============================================================
   إضافات التمرين الثاني (HTML) + لوحة المسؤول
   ============================================================ */

/* معاينة HTML داخل السؤال */
.q-preview {
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  margin: 0 0 12px;
  direction: rtl;
  color: var(--text);
  font-size: 0.95rem;
  line-height: 1.6;
}
.q-preview ul,
.q-preview ol { padding-right: 24px; margin: 6px 0; }
.q-preview table {
  border-collapse: collapse;
  margin: 4px 0;
}
.q-preview table th,
.q-preview table td {
  border: 1px solid #cbd5e1;
  padding: 4px 10px;
}
.q-preview table th { background: #f1f5f9; }
.q-preview h1 { font-size: 1.4rem; margin: 0 0 6px; }
.q-preview h2 { font-size: 1.2rem; margin: 0 0 6px; }
.q-preview h3 { font-size: 1.05rem; margin: 0 0 6px; }
.q-preview p { margin: 4px 0; }
.q-preview input { margin: 2px 4px; padding: 2px 6px; }
.q-preview form { display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.q-preview form label { font-weight: 400; margin: 0; display: inline-block; }
.q-preview button { padding: 4px 12px; border: 1px solid #cbd5e1; border-radius: 6px; background: #f1f5f9; cursor: pointer; }

/* الخيارات المعروضة ككود */
.options-code { gap: 10px; }
.option-with-code {
  padding: 8px 10px;
  align-items: flex-start;
}
.option-with-code input { margin-top: 6px; flex-shrink: 0; }
.option-code {
  margin: 0;
  flex: 1;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  font-size: .82rem;
  direction: ltr;
  text-align: left;
  white-space: pre-wrap;
  word-break: break-word;
  color: #0f172a;
  min-width: 0;
}

/* شارات حالة السؤال في نافذة تفاصيل الطالب */
.badge-status {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: .72rem;
  font-weight: 700;
  margin-left: 8px;
}
.badge-status.ok   { background: #dcfce7; color: var(--green); }
.badge-status.bad  { background: #fee2e2; color: var(--red); }
.badge-status.miss { background: #fef3c7; color: var(--amber); }

/* كتل إجابات الطالب / الإجابة الصحيحة في المودال */
.d-a-block { margin-top: 8px; }
.d-label {
  font-size: .78rem;
  color: var(--muted);
  font-weight: 700;
  margin-bottom: 4px;
}
.d-a-correct {
  background: #f0fdf4;
  border-color: #bbf7d0;
}
.d-a-empty {
  color: var(--muted);
  font-style: italic;
}

/* ============================================================
   تمييز الكود المعطوب في التمرين الثالث
   ============================================================ */
.q-code-broken {
  border-inline-start: 4px solid #f87171;
  position: relative;
}
.q-code-broken::before {
  content: '⚠ الكود التالي يحتوي على خطأ';
  display: block;
  color: #fca5a5;
  font-size: .78rem;
  font-weight: 700;
  margin-bottom: 8px;
  font-family: system-ui, sans-serif;
  direction: rtl;
}