:root {
  --background: #f3f7f8;
  --foreground: #17343b;
  --card: #ffffff;
  --primary: #087f8c;
  --primary-dark: #06616b;
  --primary-foreground: #ffffff;
  --secondary: #e8f3f4;
  --secondary-foreground: #075e68;
  --muted: #edf3f4;
  --muted-foreground: #60777c;
  --accent: #dff2f2;
  --destructive: #e7000b;
  --border: #d6e3e5;
  --input: #c8dadd;
  --ring: #2aa6b0;
  --radius: 0.9rem;
  --font-sans: "Segoe UI", Tahoma, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--background);
  color: var(--foreground);
  font-family: var(--font-sans);
  min-height: 100vh;
}
button, input, textarea, select { font-family: inherit; font-size: inherit; }
h1, h2, h3, p { margin: 0; }

/* ---------- Login ---------- */
.login-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1fr;
}
@media (min-width: 900px) {
  .login-shell { grid-template-columns: 1fr 1fr; }
}
.login-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  padding: 3rem 2rem;
  max-width: 440px;
  margin: auto;
  width: 100%;
  text-align: center;
}
.login-brand { display: flex; flex-direction: column; align-items: center; gap: .5rem; }
.login-brand-badge {
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--primary); color: #fff; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 1.2rem;
}
.login-brand-badge img { width: 100%; height: 100%; object-fit: cover; }
.login-brand p { font-weight: 700; }
.login-brand span { color: var(--muted-foreground); font-size: .85rem; }
.login-icon { font-size: 1.8rem; }
.login-copy { display: flex; flex-direction: column; align-items: center; gap: .35rem; }
.eyebrow { color: var(--primary); font-size: .82rem; font-weight: 600; }
.login-copy h1 { font-size: 1.6rem; font-weight: 800; }
.login-copy .lead { color: var(--muted-foreground); font-size: .92rem; }
.login-form { display: flex; flex-direction: column; align-items: center; gap: .6rem; width: 100%; }
.login-form label { align-self: center; }
.login-form .pin-wrap, .login-form .btn-primary { width: 100%; }
.login-form label { font-size: .85rem; font-weight: 600; }
.pin-wrap input {
  width: 100%; padding: .85rem 1rem; border-radius: var(--radius);
  border: 1px solid var(--input); background: #fff; letter-spacing: .3em; text-align: center;
}
.pin-wrap input:focus { outline: 2px solid var(--ring); border-color: var(--ring); }
.login-error { color: var(--destructive); font-size: .85rem; }
.btn-primary {
  background: var(--primary); color: #fff; border: none; border-radius: var(--radius);
  padding: .85rem 1.2rem; font-weight: 700; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: .5rem;
}
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.security-note { color: var(--muted-foreground); font-size: .8rem; display: flex; align-items: center; gap: .4rem; }
.login-aside {
  display: none; background: linear-gradient(160deg, var(--primary) 0%, #054850 100%);
  color: #fff; align-items: center; justify-content: center; flex-direction: column; gap: .75rem; text-align: center; padding: 2rem;
}
@media (min-width: 900px) { .login-aside { display: flex; } }
.aside-logo {
  width: 156px; height: 156px; border-radius: 50%; overflow: hidden;
  border: 3px solid rgba(255,255,255,.5); background: #fff;
  display: flex; align-items: center; justify-content: center;
}
.aside-logo img { width: 100%; height: 100%; object-fit: cover; }
.login-aside p { font-size: 1.4rem; font-weight: 800; line-height: 1.2; }
.login-aside span { opacity: .85; font-size: .9rem; }
.aside-notice {
  margin-top: 1.4rem; padding-top: 1.2rem; border-top: 1px solid rgba(255,255,255,.25);
  max-width: 320px;
}
.aside-notice .ar {
  font-family: 'Cairo', var(--font-sans);
  font-size: .82rem; line-height: 1.7; opacity: .92; margin: 0;
}
.aside-notice .credit {
  margin-top: .8rem; font-size: .72rem; opacity: .65; letter-spacing: .03em;
}

/* ---------- Dashboard ---------- */
.dash-shell { max-width: 1180px; margin: 0 auto; padding: 1.5rem 1rem 3rem; }
.dash-header { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1.5rem; }
.dash-title h1 { font-size: 1.4rem; font-weight: 800; }
.dash-title p { color: var(--muted-foreground); font-size: .9rem; margin-top: .2rem; }
.dash-actions { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.btn { border-radius: var(--radius); padding: .55rem 1rem; font-weight: 600; border: 1px solid var(--border); background: #fff; cursor: pointer; display: inline-flex; align-items: center; gap: .4rem; }
.btn:hover { background: var(--muted); }
.btn.outline { background: transparent; }
.btn.ghost { border-color: transparent; background: transparent; }

.stat-grid { display: grid; grid-template-columns: repeat(2,1fr); gap: .8rem; margin-bottom: 1.2rem; }
@media (min-width: 700px) { .stat-grid { grid-template-columns: repeat(4,1fr); } }
.stat-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; display: flex; align-items: center; justify-content: space-between; }
.stat-card span { display: block; color: var(--muted-foreground); font-size: .8rem; }
.stat-card strong { font-size: 1.5rem; }
.stat-icon { font-size: 1.5rem; opacity: .6; }

.toolbar { display: flex; gap: .6rem; flex-wrap: wrap; margin-bottom: 1rem; }
.toolbar input[type=search] { flex: 1; min-width: 220px; padding: .6rem .9rem; border-radius: var(--radius); border: 1px solid var(--input); background: #fff; }

.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
table { width: 100%; border-collapse: collapse; }
thead th { text-align: start; padding: .7rem 1rem; font-size: .8rem; color: var(--muted-foreground); border-bottom: 1px solid var(--border); background: var(--muted); }
tbody td { padding: .7rem 1rem; border-bottom: 1px solid var(--border); font-size: .9rem; }
tbody tr:hover { background: var(--accent); cursor: pointer; }
.patient-name span { display: block; color: var(--muted-foreground); font-size: .78rem; }
.badge { display: inline-block; padding: .2rem .6rem; border-radius: 999px; font-size: .75rem; font-weight: 600; border: 1px solid; }
.badge.solved { color: #047857; background: #ecfdf5; border-color: #a7f3d0; }
.badge.unsolved { color: #b45309; background: #fffbeb; border-color: #fde68a; }

.empty-state { padding: 3rem 1rem; text-align: center; color: var(--muted-foreground); }

.compliance-notice { display: flex; gap: .8rem; background: var(--secondary); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin-top: 1.2rem; }
.compliance-copy strong { display: block; margin-bottom: .3rem; }
.compliance-copy p { font-size: .85rem; color: var(--muted-foreground); margin: 0 0 .5rem; }

/* Modal */
.overlay { position: fixed; inset: 0; background: rgba(15,30,33,.5); display: flex; align-items: center; justify-content: center; padding: 1rem; z-index: 50; }
.overlay[hidden] { display: none; }
.modal { background: #fff; border-radius: var(--radius); width: 100%; max-width: 780px; max-height: 92vh; overflow-y: auto; padding: 1.5rem; }
.modal h2 { font-size: 1.2rem; font-weight: 800; margin-bottom: .3rem; }
.modal .sub { color: var(--muted-foreground); font-size: .85rem; margin-bottom: 1rem; }

.tabs { display: flex; gap: .3rem; overflow-x: auto; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
.tab-btn { padding: .5rem .9rem; border: none; background: none; border-bottom: 2px solid transparent; cursor: pointer; font-weight: 600; color: var(--muted-foreground); white-space: nowrap; }
.tab-btn.active { color: var(--primary); border-color: var(--primary); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.form-grid { display: grid; grid-template-columns: 1fr; gap: .9rem; }
@media (min-width: 640px) { .form-grid { grid-template-columns: 1fr 1fr; } }
.form-field { display: flex; flex-direction: column; gap: .3rem; }
.form-field.full { grid-column: 1 / -1; }
.form-field label { font-size: .82rem; font-weight: 600; }
.required-mark { color: var(--destructive); margin-inline-start: .2rem; }
.form-field input, .form-field textarea, .form-field select {
  padding: .55rem .8rem; border-radius: .6rem; border: 1px solid var(--input); background: #fff; width: 100%;
}
.form-field textarea { resize: vertical; }

.modal-footer { display: flex; justify-content: flex-end; gap: .6rem; margin-top: 1.2rem; }

.detail-meta { display: flex; gap: .8rem; color: var(--muted-foreground); font-size: .85rem; margin-top: .3rem; flex-wrap: wrap; }
.detail-grid { display: grid; grid-template-columns: 1fr; gap: .8rem; }
@media (min-width: 640px) { .detail-grid { grid-template-columns: 1fr 1fr; } }
.detail-item.full { grid-column: 1 / -1; }
.detail-item label { display: block; font-size: .78rem; color: var(--muted-foreground); margin-bottom: .2rem; }
.detail-item p { white-space: pre-wrap; }

.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.bilingual { display: flex; flex-direction: column; line-height: 1.15; }
.bilingual .en { font-size: .7rem; color: var(--muted-foreground); }

/* ---------- أزرار الإجراءات داخل جدول المرضى ---------- */
.row-actions { display: flex; gap: .2rem; flex-wrap: nowrap; justify-content: flex-start; }
.row-actions .btn { padding: .3rem .5rem; font-size: .85rem; white-space: nowrap; flex: 0 0 auto; }
.btn.ghost.danger { color: var(--destructive); }
.btn.ghost.danger:hover { background: #fdeaea; }

/* ---------- الروابط التي تظهر بشكل أزرار ---------- */
a.btn, a.btn-primary {
  text-decoration: none;
  color: var(--foreground);
  font-family: inherit;
  font-size: inherit;
}
a.btn-primary { color: var(--primary-foreground); }
a.btn:visited { color: var(--foreground); }
a.btn-primary:visited { color: var(--primary-foreground); }

/* ---------- نافذة التأكيد الموحّدة ---------- */
.confirm-overlay { display: flex; align-items: center; justify-content: center;
  position: fixed; inset: 0; background: rgba(15,42,48,.45); z-index: 60; padding: 1rem; }
.confirm-box { background: var(--card); border-radius: var(--radius);
  padding: 1.5rem; max-width: 520px; width: 100%; max-height: 90vh; overflow-y: auto;
  box-shadow: 0 20px 50px rgba(0,0,0,.2); border-top: 4px solid var(--primary); }
.confirm-box.danger { border-top-color: var(--destructive); }
.confirm-box h3 { font-size: 1.15rem; margin-bottom: .9rem; }
.confirm-body { font-size: .92rem; line-height: 1.7; color: var(--foreground); }
.confirm-body p { margin-bottom: .7rem; }
.confirm-body code { background: var(--muted); padding: .1rem .35rem; border-radius: .3rem; }
.confirm-target { background: var(--secondary); border-radius: .6rem;
  padding: .7rem .9rem; margin: .6rem 0; display: flex; flex-direction: column; gap: .2rem; }
.confirm-target span { font-size: .84rem; color: var(--muted-foreground); }
.confirm-note { font-size: .84rem; color: var(--muted-foreground); line-height: 1.6; }
.confirm-field { display: flex; flex-direction: column; gap: .35rem; margin-top: .9rem; }
.confirm-field label { font-size: .85rem; font-weight: 600; }
.confirm-field input { padding: .6rem .8rem; border: 1px solid var(--input);
  border-radius: .6rem; font-size: 1rem; }
.confirm-table { width: 100%; border-collapse: collapse; margin: .6rem 0; font-size: .88rem; }
.confirm-table th, .confirm-table td { border: 1px solid var(--border);
  padding: .45rem .6rem; text-align: center; }
.confirm-table th { background: var(--secondary); font-size: .82rem; }
.confirm-table td:first-child { text-align: right; font-weight: 600; }
.confirm-modes { display: flex; flex-direction: column; gap: .6rem; margin-top: .9rem; }
.confirm-modes label { display: flex; gap: .55rem; align-items: flex-start;
  background: var(--muted); border: 1px solid var(--border); border-radius: .6rem;
  padding: .7rem .8rem; cursor: pointer; font-size: .86rem; line-height: 1.6; }
.confirm-modes label:hover { border-color: var(--ring); }
.confirm-modes input { margin-top: .25rem; }
.btn-primary.danger { background: var(--destructive); }
.btn-primary.danger:hover:not(:disabled) { background: #b91c1c; }

/* ---------- زر إغلاق النافذة ---------- */
.confirm-box { position: relative; }
.confirm-close {
  position: absolute; top: .8rem; left: .9rem;
  width: 30px; height: 30px; border-radius: 50%;
  border: 1px solid var(--border); background: #fff;
  color: var(--muted-foreground); cursor: pointer;
  font-size: .9rem; line-height: 1; display: flex;
  align-items: center; justify-content: center;
}
.confirm-close:hover { background: var(--muted); color: var(--foreground); }
.confirm-box h3 { padding-left: 2.2rem; }
.confirm-box .modal-footer .btn.outline {
  border-color: var(--input); background: #fff; font-weight: 600;
}
.confirm-box .modal-footer .btn.outline:hover { background: var(--muted); }

/* ---------- تثبيت تخطيط الشاشة الرئيسية ---------- */
.dash-header { align-items: flex-start; }
.dash-title { flex: 1 1 260px; }
.dash-actions { flex: 0 1 auto; justify-content: flex-start; }
.dash-actions .btn, .dash-actions .btn-primary { white-space: nowrap; }

/* منطقة الجدول تحجز ارتفاعًا أدنى فلا تنهار الصفحة أثناء البحث */
.table-region { min-height: 180px; }
.table-region .empty-state { min-height: 180px; display: flex;
  align-items: center; justify-content: center; }

/* عرض ثابت لأعمدة الجدول حتى لا تتغير مع اختلاف طول الأسماء */
.card table { table-layout: auto; }
.card th:nth-child(2), .card td:nth-child(2) { width: 110px; }
.card th:nth-child(3), .card td:nth-child(3) { width: 130px; }
.card th:nth-child(4), .card td:nth-child(4) { width: 110px; }
.card th:nth-child(5), .card td:nth-child(5) { width: 120px; }
.card th:nth-child(6), .card td:nth-child(6) { width: 265px; }

@media (max-width: 860px) {
  .card { overflow-x: auto; }
  .card table { min-width: 760px; }
  .dash-actions { width: 100%; }
}

/* ---------- تثبيت رأس الصفحة نهائيًا ---------- */
/* الأزرار في سطر مستقل دائمًا، فلا تتنقل بين سطر وسطرين مع تغيّر عرض النافذة */
.dash-header {
  flex-direction: column;
  align-items: stretch;
  gap: .9rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}
.dash-title { flex: none; }
.dash-actions {
  width: 100%;
  justify-content: flex-start;
  gap: .5rem;
}
.dash-actions .btn, .dash-actions .btn-primary {
  padding: .5rem .85rem;
  font-size: .88rem;
  flex: 0 0 auto;
}

/* ---------- زر مسح البحث ---------- */
.toolbar { align-items: center; }
.search-clear { display: none; white-space: nowrap; color: var(--primary);
  border-color: var(--input); font-size: .87rem; }
.search-clear:hover { background: var(--accent); }
.empty-state p { margin: 0; }
.table-region .empty-state { flex-direction: column; }

/* ---------- ضبط نهائي لخلية الأزرار ---------- */
.card td:last-child { padding: .5rem .7rem; }
.card tbody td { vertical-align: middle; }
