/* ==========================================================================
   نظام تنسيق العمليات — مستشفى سعاد كفافي
   تصميم Mobile-first بالعربية RTL. القاعدة للموبايل، والتوسعة بـ min-width.
   ========================================================================== */

/* الخط: Cairo مستضاف محليًا (خط متغير، ملف لكل نطاق محارف).
   لا اتصال بأي خدمة خطوط خارجية — سياسة CSP تسمح بـ font-src 'self' فقط.
   المصدر: Google Fonts، رخصة SIL Open Font License 1.1 (assets/brand/FONTS.md). */
@font-face {
  font-family: 'Cairo';
  src: url('/static/fonts/Cairo-arabic.woff2') format('woff2');
  font-weight: 400 700;
  font-style: normal;
  font-display: swap;
  unicode-range: U+0600-06FF, U+0750-077F, U+0870-088E, U+0890-0891, U+0897-08E1, U+08E3-08FF, U+200C-200E, U+2010-2011, U+204F, U+2E41, U+FB50-FDFF, U+FE70-FE74, U+FE76-FEFC, U+102E0-102FB, U+10E60-10E7E, U+10EC2-10EC4, U+10EFC-10EFF, U+1EE00-1EE03, U+1EE05-1EE1F, U+1EE21-1EE22, U+1EE24, U+1EE27, U+1EE29-1EE32, U+1EE34-1EE37, U+1EE39, U+1EE3B, U+1EE42, U+1EE47, U+1EE49, U+1EE4B, U+1EE4D-1EE4F, U+1EE51-1EE52, U+1EE54, U+1EE57, U+1EE59, U+1EE5B, U+1EE5D, U+1EE5F, U+1EE61-1EE62, U+1EE64, U+1EE67-1EE6A, U+1EE6C-1EE72, U+1EE74-1EE77, U+1EE79-1EE7C, U+1EE7E, U+1EE80-1EE89, U+1EE8B-1EE9B, U+1EEA1-1EEA3, U+1EEA5-1EEA9, U+1EEAB-1EEBB, U+1EEF0-1EEF1;
}
@font-face {
  font-family: 'Cairo';
  src: url('/static/fonts/Cairo-latin.woff2') format('woff2');
  font-weight: 400 700;
  font-style: normal;
  font-display: swap;
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

:root {
  --primary: #743b83;
  --primary-dark: #5b2e68;
  --primary-soft: #f1eaf4;
  --accent: #c83e96;
  --bg: #f7f5fa;
  --surface: #ffffff;
  --text: #303238;
  --muted: #6b6b76;
  --border: #e7e1ec;

  --ok: #1b7a4b;
  --ok-bg: #e7f5ee;
  --warn: #8a5b00;
  --warn-bg: #fdf3e0;
  --danger: #a32020;
  --danger-bg: #fbeaea;
  --info: #245a8d;
  --info-bg: #e9f1f9;

  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 3px rgba(48, 50, 56, 0.09);
  --shadow-lg: 0 8px 28px rgba(48, 50, 56, 0.18);

  --tap: 44px;           /* الحد الأدنى لهدف اللمس */
  --header-h: 56px;
  --sidebar-w: 264px;

  --font: 'Cairo', 'Segoe UI', Tahoma, 'Noto Sans Arabic', 'Dubai', Arial, sans-serif;
}

*,
*::before,
*::after { box-sizing: border-box; }

html {
  /* منع تكبير iOS التلقائي عند التركيز، ودعم تكبير المستخدم */
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;              /* 16px يمنع تكبير الحقول على iOS */
  line-height: 1.65;
  color: var(--text);
  background: var(--bg);
  /* لا تمرير أفقي للصفحة إطلاقًا */
  overflow-x: hidden;
}

/* كل عنصر يلتزم بعرض الحاوية؛ يمنع الجداول والنصوص الطويلة من دفع الصفحة */
img, svg, video, canvas { max-width: 100%; height: auto; }

a { color: var(--primary); }

/* مؤشر تركيز واضح للوحة المفاتيح */
:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.skip-link {
  position: absolute;
  right: -9999px;
  top: 0;
  background: var(--primary);
  color: #fff;
  padding: 12px 18px;
  z-index: 200;
}
.skip-link:focus { right: 8px; top: 8px; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

/* ===================== الترويسة ===================== */
.app-header {
  position: sticky;
  top: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--header-h);
  padding: 6px 12px;
  background: var(--primary);
  color: #fff;
}

.app-header .brand-line {
  display: flex;
  flex-direction: row;   /* اللوجو ثم الاسم في سطر واحد */
  align-items: center;
  gap: 10px;
  min-width: 0;          /* يسمح باقتطاع النص بدل دفع التخطيط */
  min-height: var(--tap); /* رابط قابل للنقر: هدف لمس كامل */
  line-height: 1.25;
}
.app-header .brand-line strong {
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.app-header .brand-line span {
  font-size: 11px;
  opacity: 0.9;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.app-header .spacer { flex: 1 1 auto; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--tap);
  min-height: var(--tap);
  padding: 0 10px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.22); }

.header-user {
  display: none;
  font-size: 13px;
  align-items: center;
  gap: 8px;
}

/* ===================== التخطيط ===================== */
.layout { display: block; }

/* الموبايل: قائمة جانبية تنزلق من اليمين */
.sidebar {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(86vw, var(--sidebar-w));
  z-index: 90;
  background: var(--surface);
  border-inline-start: 1px solid var(--border);
  padding: 12px;
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform 0.22s ease;
  -webkit-overflow-scrolling: touch;
}
.sidebar.open { transform: translateX(0); }

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(30, 20, 35, 0.45);
  border: 0;
  padding: 0;
  display: none;
}
.sidebar-backdrop.show { display: block; }

.sidebar h2 {
  font-size: 12px;
  letter-spacing: 0.02em;
  color: var(--muted);
  margin: 14px 6px 6px;
  font-weight: 600;
}
.sidebar nav a {
  display: flex;
  align-items: center;
  min-height: var(--tap);
  padding: 8px 12px;
  margin-bottom: 3px;
  border-radius: var(--radius-sm);
  color: var(--text);
  text-decoration: none;
  font-size: 15px;
}
.sidebar nav a:hover { background: var(--primary-soft); }
.sidebar nav a[aria-current='page'] {
  background: var(--primary-soft);
  color: var(--primary-dark);
  font-weight: 600;
  box-shadow: inset -3px 0 0 var(--primary);
}

.main {
  padding: 12px;
  min-width: 0;    /* حاسم: يمنع محتوى grid من توسيع الصفحة */
}

.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}
.page-head h1 {
  font-size: 20px;
  margin: 0;
  font-weight: 700;
  min-width: 0;
}
.page-head .actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-inline-start: auto;
}

/* ===================== البطاقات ===================== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  overflow: hidden;    /* يحبس المحتوى العريض داخل البطاقة */
}
.card > h2,
.card > .card-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 0;
  padding: 12px 14px;
  font-size: 15px;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
  background: #fcfafd;
}
.card-body { padding: 14px; }

/* ===================== الأزرار ===================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn:hover { background: #f3eef6; }
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-accent {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-danger { color: var(--danger); border-color: #e3bcbc; }
.btn-danger:hover { background: var(--danger-bg); }
/* حتى الأزرار الصغيرة تحقق 44×44 على اللمس؛ تصغر فقط مع مؤشر دقيق على شاشة واسعة */
.btn-sm { min-height: var(--tap); padding: 4px 12px; font-size: 14px; }
@media (pointer: fine) and (min-width: 1024px) {
  .btn-sm { min-height: 36px; }
}
.btn[disabled] { opacity: 0.55; cursor: not-allowed; }

/* ===================== النماذج ===================== */
/* عمود واحد افتراضًا (موبايل)، وأعمدة أكثر عند اتساع المساحة */
.form-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field.span-2 { grid-column: 1 / -1; }

label { font-size: 14px; font-weight: 600; }
.hint { font-size: 12px; color: var(--muted); font-weight: 400; }

input[type='text'],
input[type='search'],
input[type='password'],
input[type='date'],
input[type='number'],
select,
textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px;    /* لا تقللها: تمنع تكبير iOS عند التركيز */
}
textarea { min-height: 88px; resize: vertical; line-height: 1.6; }
select { appearance: none; background-image: none; padding-inline-end: 12px; }

input:focus, select:focus, textarea:focus { border-color: var(--primary); }

/* خانات الاختيار: هدف لمس كافٍ */
.check {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 2px 4px;
  font-size: 15px;
  cursor: pointer;
}
.check input[type='checkbox'],
.check input[type='radio'] {
  width: 20px;
  height: 20px;
  flex: 0 0 auto;
  accent-color: var(--primary);
  cursor: pointer;
}

fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px 12px;
  margin: 0;
  min-width: 0;
}
legend { font-size: 13px; font-weight: 700; padding: 0 6px; color: var(--muted); }

.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding-top: 12px;
  margin-top: 4px;
  border-top: 1px solid var(--border);
}

/* ===================== الرسائل ===================== */
.alert {
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid;
  margin-bottom: 12px;
  font-size: 15px;
}
.alert-error   { background: var(--danger-bg); border-color: #edc6c6; color: var(--danger); }
.alert-success { background: var(--ok-bg);     border-color: #c3e3d1; color: var(--ok); }
.alert-info    { background: var(--info-bg);   border-color: #c6d9ea; color: var(--info); }
.alert ul { margin: 6px 0 0; padding-inline-start: 20px; }

/* ===================== الشارات ===================== */
/* اللون مصحوب دائمًا بنص صريح — لا يُعتمد على اللون وحده */
.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  border: 1px solid;
  white-space: nowrap;
}
.badge-neutral { background: #f0eef2; border-color: var(--border); color: var(--muted); }
.badge-ok      { background: var(--ok-bg);     border-color: #c3e3d1; color: var(--ok); }
.badge-warn    { background: var(--warn-bg);   border-color: #ecd9b0; color: var(--warn); }
.badge-danger  { background: var(--danger-bg); border-color: #edc6c6; color: var(--danger); }
.badge-info    { background: var(--info-bg);   border-color: #c6d9ea; color: var(--info); }

/* ===================== العدادات ===================== */
.counters {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}
.counter {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  text-decoration: none;
  color: inherit;
  display: block;
  min-height: var(--tap);
}
.counter:hover { border-color: var(--primary); background: #fcfafd; }
.counter .n {
  display: block;
  font-size: 22px;
  font-weight: 700;
  color: var(--primary-dark);
  line-height: 1.2;
}
.counter .l { font-size: 12.5px; color: var(--muted); }


/* أهداف لمس: عناصر يصعب أن تبلغ 44 بكسل بحكم نوعها */
input[type='file'] {
  min-height: var(--tap);
  padding: 10px 8px;
  width: 100%;
}
/* روابط داخل جداول البيانات: صف الجدول قصير، فيُوسَّع الرابط نفسه */
@media (max-width: 767px) {
  table.data a,
  .case-card a:not(.cc-main) {
    display: inline-flex;
    align-items: center;
    min-height: var(--tap);
  }
}

/* ===================== شريط الترخيص ===================== */
.lic-banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  margin-bottom: 14px;
  border-radius: var(--radius-sm);
  border: 1px solid;
  font-size: 14.5px;
  line-height: 1.6;
}
.lic-banner a {
  font-weight: 700;
  text-decoration: underline;
  /* رابط داخل نص: يبقى هدف لمس كاملًا على الموبايل */
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  padding: 0 4px;
}
.lic-banner-bad {
  background: #fdf2f4;
  border-color: #e6adb8;
  color: #7d2438;
}
.lic-banner-warn {
  background: #fdf8ec;
  border-color: #e3cd9a;
  color: #7a5714;
}
.lic-state { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 0; }

/* ===================== اللوجو ===================== */
/* النسبة محفوظة دائمًا: الارتفاع مقيَّد والعرض auto، بلا أي تمدد.
   لا يُعاد رسم اللوجو ولا تُغيَّر الكتابة داخله. */
.brand-logo {
  /* القيمة من assets/brand/facility.json يبثّها القالب متغيّرَ CSS، فلا
     يتكرر الرقم في مكانين. النسبة محفوظة دائمًا: العرض تلقائي.
     والقيمة الاحتياطية تعمل حين يُعرض ملف CSS خارج قوالب التطبيق فقط —
     ولا تُنقذ من قيمة معرَّفة غير صالحة، ولذلك يفحصها `brand.ts` عند
     التشغيل ويرفض الإقلاع إن لم تكن عددًا موجبًا. */
  height: var(--brand-logo-h, 30px);
  width: auto;
  max-width: 120px;
  object-fit: contain;
  flex: 0 0 auto;
  /**
   * مساحة فاتحة صغيرة تحت الشعار على الهيدر البنفسجي.
   *
   * الشعار مصمَّم لخلفية فاتحة: كتابته رمادية داكنة، وتباينها على #743B83
   * ضعيف. الحل هنا **عرضي لا تحريري**: الملف نفسه بلا إعادة تلوين ولا مساس
   * بأي تفصيلة، ويُعرض فوق لوحة بيضاء بحواف دائرية وحشو بسيط — كما يُطبع
   * الشعار على ورق أبيض داخل مطبوعة ملونة.
   *
   * `box-sizing: content-box` مقصود: الارتفاع يبقى ارتفاع **الصورة**، فتضيف
   * اللوحة 3 بكسل من كل جهة فوقه. عند 40px يصير الصندوق 46px، ويسعه الهيدر
   * (56px) بلا تغيير ارتفاعه ولا مزاحمة مساحة العمل.
   */
  box-sizing: content-box;
  background: #fff;
  padding: 3px 5px;
  border-radius: 8px;
}
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.login-logo {
  display: block;
  height: 72px;
  width: auto;
  max-width: 100%;
  object-fit: contain;
  margin: 0 auto 10px;
}
.print-logo {
  height: 44px;
  width: auto;
  object-fit: contain;
}
.sheet-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.sheet-brand h1 { margin: 0; }

/* شاشات ضيقة: لوجو أصغر حتى لا يزاحم الاسم ولا يُقص */
@media (max-width: 520px) {
  /* تصغير للموبايل وحده — قرار تجاوب، لا قيمة هوية */
  .brand-logo { height: calc(var(--brand-logo-h, 30px) * 0.8); max-width: 84px; }
  .login-logo { height: 56px; }
}

/* ===================== الفلاتر ===================== */
.filter-panel { margin: 0; }
.filter-panel > summary.filter-summary {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 8px 10px;
  margin-top: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #faf7fb;
  font-weight: 700;
  color: var(--primary-dark);
  cursor: pointer;
  list-style: none;
}
.filter-panel > summary.filter-summary::-webkit-details-marker { display: none; }
.filter-panel > summary.filter-summary::after {
  content: '\25BC';                 /* مثلث لأسفل: مغلقة */
  margin-inline-start: auto;
  font-size: 11px;
  color: var(--muted);
}
.filter-panel[open] > summary.filter-summary::after { content: '\25B2'; }
.filter-panel > summary.filter-summary:hover { border-color: var(--primary); }
.filter-panel > summary.filter-summary:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}
/* مجموعة مطوية داخل اللوحة: الباقي غير الشائع */
/* بطاقة ثانوية قابلة للطي: العنوان نفسه هو الزر */
.collapse-card > summary.card-head {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--tap);
  cursor: pointer;
  list-style: none;
}
.collapse-card > summary.card-head::-webkit-details-marker { display: none; }
.collapse-card > summary.card-head::after {
  content: '\25BC';
  margin-inline-start: auto;
  font-size: 11px;
  color: var(--muted);
}
.collapse-card[open] > summary.card-head::after { content: '\25B2'; }
.collapse-card > summary.card-head:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }

.filter-more { margin-top: 12px; }
.filter-more > summary {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 6px 10px;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  color: var(--primary-dark);
  cursor: pointer;
}
.filter-more > summary:hover { border-color: var(--primary); }
.filter-more > summary:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.filter-more > .filter-groups,
.filter-more > fieldset { margin-top: 10px; }
/* خيارات قصيرة في صف يلتف بدل قائمة رأسية طويلة */
.check-chips { display: flex; flex-wrap: wrap; gap: 6px 8px; }
.check-chips .check {
  flex: 0 1 auto;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 12px 2px 8px;
  background: var(--surface);
  font-size: 14.5px;
}
.check-chips .check:hover { border-color: var(--primary); }
.check-chips .check:has(input:checked) {
  border-color: var(--primary);
  background: #f6f0f8;
  font-weight: 600;
}
.check-chips .check:has(input:focus-visible) { outline: 2px solid var(--primary); outline-offset: 2px; }
.date-pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.applied-line { margin: 8px 2px 0; }
.filter-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.filter-bar .search-wrap { flex: 1 1 220px; min-width: 0; }
.filter-count {
  display: inline-block;
  min-width: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}
.filter-groups {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

/* ===================== الجداول ===================== */
/* الجدول العريض يمرَّر داخل حاويته فقط، ولا يحرك الصفحة */
.table-scroll {
  width: 100%;
  max-width: 100%;
  /**
   * ارتفاع مقيَّد ليصير الجدول منطقة تمرير حقيقية في المحورين.
   *
   * بدونه كان `overflow-x: auto` يجعل overflow-y يُحسب auto أيضًا، فتصير
   * هذه الحاوية هي الحاضنة لعنصر sticky — لكنها لا تُمرَّر رأسيًا أصلًا
   * (ارتفاعها يتبع محتواه)، فالذي يُمرَّر هو الصفحة، ويخرج رأس الجدول من
   * أعلى النافذة مع الصفحة. القياس: عند منتصف جدول من 27 صفًا كان الرأس
   * عند y = -191 أي خارج النافذة تمامًا.
   *
   * بتقييد الارتفاع يحدث التمرير الرأسي **داخل** الحاوية، فيثبت الرأس عند
   * حافتها العليا، ويبقى شريط التمرير الأفقي عند حافتها السفلى داخل
   * النافذة دائمًا بلا نزول إلى آخر الجدول.
   */
  max-height: calc(100vh - var(--header-h) - 190px);
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius);
  /**
   * شريط تمرير **كلاسيكي يشغل مساحة** لا شريط تراكبي يظهر أثناء التمرير
   * فقط. القياس: مع `scrollbar-width: thin` كان الفرق بين offsetHeight
   * وclientHeight صفرًا، أي أن الشريط تراكبي ولا يُرسم إلا وقت التمرير،
   * فلا يعرف المستخدم أن هناك أعمدة مخفية أصلًا.
   *
   * تعريف `::-webkit-scrollbar` وحده يفرض الشريط الكلاسيكي في Chrome
   * وEdge؛ ووجود `scrollbar-width` يُبطل مفعوله، لذلك لا يُستعمل هنا.
   */
}
.table-scroll::-webkit-scrollbar { height: 12px; }
.table-scroll::-webkit-scrollbar-track { background: #ece5f0; border-radius: 6px; }
.table-scroll::-webkit-scrollbar-thumb {
  background: var(--primary);
  border-radius: 6px;
  border: 2px solid #ece5f0;
}

/**
 * عرض الجدول = مجموع أعمدته، لا 100% من الحاوية.
 *
 * كان `width: 100%` مع `min-width: 860px` فقط: عند 17 عمودًا يقسم المتصفح
 * الـ860 بينها فيصير نصيب «المريض» 79 بكسل و«الطبيب» 66، فتنزل كل كلمة في
 * سطر. الحد الأدنى للجدول لا يكفي؛ الحد الأدنى **لكل عمود** هو ما يمنع
 * الضغط. و`max-content` يجعل الجدول يستوعب المجموع ويتجاوز الحاوية عند
 * الحاجة، بينما `min-width: 100%` يبقيه ممتلئًا حين تكون الأعمدة قليلة.
 */
table.data {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  font-size: 14.5px;
}
table.data th,
table.data td {
  padding: 9px 12px;
  text-align: right;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  /* الالتفاف طبيعي عند حدود الكلمات؛ الكلمة الطويلة جدًا وحدها تُكسر */
  overflow-wrap: break-word;
}

/**
 * حدود دنيا لعرض الأعمدة حسب محتواها.
 * تُوضع على th و td معًا: الرأس وحده لا يكفي لأن بعض الرؤوس أقصر من قيمها.
 */
/* الحد الأقصى يجعل القيمة الطويلة جدًا تلتفّ على سطرين بدل أن تمدّ
   العمود بلا حد. الالتفاف طبيعي عند حدود الكلمات، بلا قصّ ولا ضغط. */
table.data th.col-name,   table.data td.col-name   { min-width: 200px; max-width: 300px; }
table.data th.col-person, table.data td.col-person { min-width: 190px; max-width: 300px; }
table.data th.col-dept,   table.data td.col-dept   { min-width: 170px; max-width: 260px; }
table.data th.col-entity, table.data td.col-entity { min-width: 180px; max-width: 300px; }
table.data th.col-class,  table.data td.col-class  { min-width: 140px; max-width: 220px; }
table.data th.col-code,   table.data td.col-code   { min-width: 128px; white-space: nowrap; }
table.data th.col-id,     table.data td.col-id     { min-width: 112px; white-space: nowrap; }
table.data th.col-phone,  table.data td.col-phone  { min-width: 124px; white-space: nowrap; }
table.data th.col-date,   table.data td.col-date   { min-width: 104px; white-space: nowrap; }
table.data th.col-num,    table.data td.col-num    { min-width: 74px;  white-space: nowrap; }
table.data th.col-badge,  table.data td.col-badge  { min-width: 130px; white-space: nowrap; }
table.data th.col-text,   table.data td.col-text   { min-width: 220px; max-width: 380px; }
/**
 * رؤوس ثابتة أثناء التمرير الرأسي داخل الحاوية.
 * `top: 0` نسبةً إلى حاوية التمرير لا إلى النافذة، والحاوية تقع أسفل هيدر
 * التطبيق، فلا تداخل بينهما. وz-index أقل من الهيدر (60) عمدًا: لو مرّت
 * الحاوية تحته أثناء تمرير الصفحة يبقى الهيدر فوقها لا العكس.
 */
table.data thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #f4eff7;
  color: var(--primary-dark);
  font-weight: 700;
  font-size: 13.5px;
  white-space: nowrap;
}
table.data tbody tr:hover { background: #fbf9fc; }
table.data a { font-weight: 600; }

.table-note { padding: 8px 4px; font-size: 12.5px; color: var(--muted); }

/**
 * شريط تمرير أفقي مخصص فوق الجدول.
 *
 * لماذا مخصص لا شريط المتصفح: القياس أظهر أن شريط المتصفح هنا **تراكبي**
 * (الفرق بين offsetHeight وclientHeight صفر)، فلا يُرسم إلا لحظة التمرير
 * ولا يشغل مساحة. المستخدم لا يرى ما يدله على وجود أعمدة مخفية أصلًا.
 * ومحاولة فرض الشريط الكلاسيكي عبر ::-webkit-scrollbar لم تعد مضمونة في
 * الإصدارات الحديثة. الشريط المخصص مرئي دائمًا ولا يعتمد على المتصفح.
 *
 * لا يُخفي شيئًا ولا يستبدل وسائل التمرير الأخرى: عجلة الفأرة والسحب
 * باللمس ومفاتيح الأسهم تعمل كلها كما هي، وشريط المتصفح باقٍ في مكانه.
 */
.table-hscroll {
  position: relative;
  height: 14px;
  margin-bottom: 4px;
  border-radius: 7px;
  background: #ece5f0;
  cursor: pointer;
  touch-action: none;
}
.table-hscroll-thumb {
  position: absolute;
  top: 2px;
  height: 10px;
  min-width: 32px;
  border-radius: 5px;
  background: var(--primary);
  transition: background 0.12s;
}
.table-hscroll:hover .table-hscroll-thumb { background: var(--primary-dark); }
.table-hscroll.dragging .table-hscroll-thumb { background: var(--primary-dark); }

/* بطاقات الحالات على الموبايل — نفس البيانات، لا حذف لأي حقل */
.case-cards { display: grid; gap: 10px; }
.case-card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  text-decoration: none;
  color: inherit;
  box-shadow: var(--shadow);
}
.case-card:hover { border-color: var(--primary); }
.case-card .cc-top {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 6px;
}
.case-card .cc-name { font-weight: 700; font-size: 16px; min-width: 0; overflow-wrap: anywhere; }
.case-card .cc-code { font-size: 12.5px; color: var(--muted); }
.case-card .cc-meta {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px 10px;
  font-size: 13.5px;
  color: var(--muted);
  margin-bottom: 8px;
}
.case-card .cc-meta b { color: var(--text); font-weight: 600; overflow-wrap: anywhere; }
.case-card .cc-badges { display: flex; flex-wrap: wrap; gap: 6px; }

/* قوائم التعريف */
.kv {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px 14px;
  margin: 0;
  font-size: 14.5px;
}
.kv > div { min-width: 0; }
.kv dt { color: var(--muted); font-size: 12.5px; }
.kv dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }

/* الجدول الزمني للمتابعات والتسعير */
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li {
  padding: 10px 0;
  border-bottom: 1px dashed var(--border);
}
.timeline li:last-child { border-bottom: 0; }
.timeline .t-head {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 3px;
}
.timeline .t-body { overflow-wrap: anywhere; }

/* ===================== النوافذ ===================== */
/* لا تتجاوز مساحة العرض، وتبقى أزرار الحفظ والإلغاء قابلة للوصول
   عند ظهور لوحة مفاتيح الهاتف (dvh + تمرير داخلي) */
dialog.modal {
  width: min(96vw, 620px);
  max-width: 96vw;
  max-height: 90vh;
  max-height: 90dvh;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  color: var(--text);
  background: var(--surface);
}
dialog.modal::backdrop { background: rgba(30, 20, 35, 0.5); }
dialog.modal form { display: flex; flex-direction: column; max-height: inherit; }
dialog.modal .m-head {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  background: #fcfafd;
}
dialog.modal .m-body {
  padding: 14px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1 1 auto;
}
dialog.modal .m-foot {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  background: #fcfafd;
  position: sticky;
  bottom: 0;
  flex: 0 0 auto;
}

/* ===================== صفحة الدخول ===================== */
/* هوية المستشفى فقط — لا بيانات مرضى ولا إحصاءات قبل الدخول */
.login-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 14px;
  background: linear-gradient(160deg, #ffffff 0%, var(--bg) 55%, #f0e9f4 100%);
}
.login-box {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  padding: 22px 18px 20px;
}
.login-brand { text-align: center; margin-bottom: 18px; }
.login-brand img {
  /* يحافظ على نسب اللوجو دون تحريف */
  max-width: min(230px, 70%);
  height: auto;
  object-fit: contain;
  margin: 0 auto 10px;
  display: block;
}
.login-brand .wordmark {
  font-size: 21px;
  font-weight: 700;
  color: var(--primary-dark);
  line-height: 1.35;
  overflow-wrap: break-word;
}
.login-brand .en { font-size: 12px; color: var(--muted); direction: ltr; }
.login-brand .sys {
  margin-top: 8px;
  font-size: 14px;
  color: var(--text);
  padding-top: 8px;
  border-top: 1px solid var(--border);
}
.login-foot { margin-top: 14px; font-size: 12px; color: var(--muted); text-align: center; }

/* ===================== أدوات ===================== */
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }
.stack > * + * { margin-top: 10px; }
.row-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.pill-list { display: flex; flex-wrap: wrap; gap: 6px; margin: 0; padding: 0; list-style: none; }

.pagination {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}

.perm-source {
  font-size: 12px;
  color: var(--muted);
  display: block;
  overflow-wrap: anywhere;
}

.only-mobile { display: block; }
.only-desktop { display: none; }

/* ===================== نقاط التحقق ===================== */
/* 768px وما فوق: تابلت */
@media (min-width: 768px) {
  .main { padding: 18px 20px; }
  .page-head h1 { font-size: 23px; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .counters { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .filter-groups { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kv { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .header-user { display: flex; }
  .only-mobile { display: none; }
  .only-desktop { display: block; }
  .app-header .brand-line strong { font-size: 17px; }
  .app-header .brand-line span { font-size: 12px; }
}

/* 1024px وما فوق: القائمة ثابتة بجانب المحتوى */
@media (min-width: 1024px) {
  .layout {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);  /* minmax(0,…) يمنع تجاوز الصفحة */
  }
  .sidebar {
    position: sticky;
    top: var(--header-h);
    height: calc(100vh - var(--header-h));
    transform: none;
    transition: none;
    border-inline-start: 0;
    border-inline-end: 1px solid var(--border);
    width: auto;
  }
  .sidebar-backdrop, .sidebar-toggle { display: none !important; }
  .counters { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  .filter-groups { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .form-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  /* اللوحة تبقى قابلة للطي على الديسكتوب أيضًا حتى لا تحجب الجدول */
  .filter-panel > .filter-groups { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .filter-more > .filter-groups { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (min-width: 1366px) {
  .main { padding: 22px 28px; }
  .kv { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (min-width: 1600px) {
  .main-inner { max-width: 1560px; }
}

/* الموبايل الأفقي: ترويسة أقصر حتى لا تبتلع الشاشة */
@media (max-height: 480px) and (orientation: landscape) {
  :root { --header-h: 48px; }
  .app-header { min-height: 48px; }
  .login-page { align-items: flex-start; padding-top: 12px; }
}

/* تقليل الحركة */
@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ===================== الطباعة ===================== */
@media print {
  .app-header, .sidebar, .sidebar-backdrop, .no-print, .form-actions, .pagination {
    display: none !important;
  }
  .layout { display: block; }
  body { background: #fff; font-size: 12pt; }
  .card { break-inside: avoid; box-shadow: none; border-color: #999; }
  .table-scroll { max-height: none !important; overflow: visible !important; }
  table.data { width: 100%; min-width: 0; font-size: 10.5pt; table-layout: fixed; }
  /* الحدود الدنيا للأعمدة للشاشة لا للورق: الورق عرضه ثابت */
  table.data th, table.data td { min-width: 0 !important; white-space: normal !important; }
  .table-hscroll { display: none !important; }
  table.data thead th { position: static; background: #eee !important; color: #000 !important; }
  .table-scroll { overflow: visible; }
  a[href]::after { content: ''; }
  .print-header { display: block !important; }
}
.print-header { display: none; }

/* ===================== التقارير ===================== */
.report-cards {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}
.report-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-decoration: none;
  color: inherit;
  min-height: var(--tap);
}
.report-card:hover { border-color: var(--primary); background: #fcfafd; }
.report-card strong { font-size: 16px; color: var(--primary-dark); }

.scope-line { margin: -4px 0 10px; }

/* عدادات مدمجة: ارتفاع أقل حتى لا تؤخر الوصول لأول صف */
.counters-compact .counter { padding: 7px 10px; }
.counters-compact .counter .n { font-size: 18px; }
.counters-compact .counter .l { font-size: 11.5px; }

select[multiple] {
  min-height: 108px;
  padding: 4px;
}
select[multiple] option { padding: 5px 6px; }

.col-picker { grid-column: 1 / -1; }

.sort-link {
  color: var(--primary-dark);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  min-height: 32px;
}
.sort-link:hover { text-decoration: underline; }

.saved-pill { display: flex; align-items: center; gap: 4px; }

@media (min-width: 768px) {
  .report-cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (min-width: 1200px) {
  .report-cards { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

/* ===================== لوحة اليوم ===================== */
.today-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 14px;
}
.today-tile {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-inline-start: 4px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  text-decoration: none;
  color: inherit;
  min-height: var(--tap);
  box-shadow: var(--shadow);
}
.today-tile:hover { background: #fcfafd; }
.today-tile .tt-n { font-size: 22px; font-weight: 700; line-height: 1.15; }
.today-tile .tt-t { font-size: 13.5px; font-weight: 600; }
.today-tile .tt-h { font-size: 11.5px; color: var(--muted); }
.today-tile.tone-danger { border-inline-start-color: var(--danger); }
.today-tile.tone-danger .tt-n { color: var(--danger); }
.today-tile.tone-warn { border-inline-start-color: var(--warn); }
.today-tile.tone-warn .tt-n { color: var(--warn); }
.today-tile.tone-info { border-inline-start-color: var(--primary); }
.today-tile.tone-info .tt-n { color: var(--primary-dark); }
.today-tile.tone-ok { border-inline-start-color: var(--ok); }
.today-tile.tone-ok .tt-n { color: var(--ok); }

.today-list { list-style: none; margin: 0; padding: 0; }
.today-list li { border-bottom: 1px solid var(--border); }
.today-list li:last-child { border-bottom: 0; }
.today-list a {
  display: grid;
  grid-template-columns: 1fr;
  gap: 2px;
  padding: 10px 14px;
  text-decoration: none;
  color: inherit;
  min-height: var(--tap);
}
.today-list a:hover { background: #fbf9fc; }
.today-list .tl-name { font-weight: 700; }
.today-list .tl-meta { font-size: 12.5px; color: var(--muted); }
.today-list .tl-dates { font-size: 12.5px; color: var(--primary-dark); font-weight: 600; }

/* المتابعة المصحَّحة تبقى ظاهرة لكن باهتة */
.fu-superseded { opacity: 0.62; }

.catalog-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

@media (min-width: 768px) {
  .today-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .today-list a { grid-template-columns: 1.4fr 1fr 1.2fr auto; align-items: center; gap: 10px; }
}
@media (min-width: 1200px) {
  .today-grid { grid-template-columns: repeat(6, minmax(0, 1fr)); }
}

/* ===================== قائمة الحساب في الترويسة ===================== */
.account-menu { position: relative; }
.account-menu > summary {
  list-style: none;
  gap: 8px;
  cursor: pointer;
  padding-inline: 8px;
}
.account-menu > summary::-webkit-details-marker { display: none; }
.acc-initial {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px; height: 26px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.9);
  color: var(--primary-dark);
  font-weight: 700;
  font-size: 14px;
}
.acc-name { display: none; }

.account-pop {
  position: absolute;
  top: calc(100% + 6px);
  inset-inline-end: 0;
  z-index: 120;
  min-width: 230px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.account-pop .acc-head {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: #fcfafd;
}
.account-pop a,
.account-pop .acc-logout {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: var(--tap);
  padding: 8px 12px;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  text-align: start;
  text-decoration: none;
  cursor: pointer;
}
.account-pop a:hover,
.account-pop .acc-logout:hover { background: var(--primary-soft); }
.account-pop .acc-logout { color: var(--danger); border-top: 1px solid var(--border); }

a.brand-line { text-decoration: none; color: inherit; }

/* ===================== ملخص قابل للطي فوق القوائم ===================== */
.summary-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 10px;
}
.summary-box > summary {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 10px;
  padding: 10px 14px;
  min-height: var(--tap);
  cursor: pointer;
  list-style: none;
  font-size: 15px;
}
.summary-box > summary::-webkit-details-marker { display: none; }
.summary-box .sum-lead b { font-size: 19px; color: var(--primary-dark); }
.summary-box .sum-scope { margin-inline-start: auto; }
.summary-box .sum-toggle {
  color: var(--primary);
  font-weight: 600;
  white-space: nowrap;
}
.summary-box .sum-toggle::after { content: ' ▾'; }
.summary-box[open] .sum-toggle::after { content: ' ▴'; }
.summary-box .counters { margin: 0; padding: 0 12px 12px; }

/* ===================== بطاقة الحالة: ملخص ثم تفاصيل ===================== */
.case-card { padding: 0; overflow: hidden; }
.case-card .cc-main {
  display: block;
  padding: 12px 14px 10px;
  text-decoration: none;
  color: inherit;
}
.case-card .cc-main:hover { background: #fcfafd; }
.case-card .cc-key {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 5px;
  font-size: 13.5px;
  margin-bottom: 3px;
}
.case-card .cc-badges { margin-top: 7px; }

.cc-more { border-top: 1px solid var(--border); }
.cc-more > summary {
  display: flex;
  align-items: center;
  min-height: var(--tap);
  padding: 6px 14px;
  cursor: pointer;
  font-size: 13.5px;
  color: var(--primary);
  font-weight: 600;
  list-style: none;
  background: #fcfafd;
}
.cc-more > summary::-webkit-details-marker { display: none; }
.cc-more > summary::after { content: ' ▾'; }
.cc-more[open] > summary::after { content: ' ▴'; }
.cc-more > dl,
.cc-more > .btn { margin: 0; }
.cc-more > .btn { margin: 0 14px 12px; }
.case-card dl.cc-meta {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px 12px;
  padding: 10px 14px;
  margin: 0;
  font-size: 13.5px;
}
.case-card dl.cc-meta > div { min-width: 0; }
.case-card dl.cc-meta dt { color: var(--muted); font-size: 12px; }
.case-card dl.cc-meta dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }

/* شريط البحث المدمج: بلا فراغ كبير حين تكون الفلاتر مغلقة */
.filter-bar { margin-bottom: 0; }
.filter-panel[open] > .filter-groups { margin-top: 12px; }
/* زر التطبيق واضح ودائم أسفل اللوحة المفتوحة */
.filter-apply {
  position: sticky;
  bottom: 0;
  z-index: 3;
  margin-top: 14px;
  padding: 10px 0 2px;
  background: linear-gradient(to top, var(--surface) 70%, rgba(255, 255, 255, 0));
}
.filter-apply .btn-primary { min-width: 150px; }

.sidebar-scope { margin-top: 18px; padding: 0 6px; }

@media (min-width: 768px) {
  .acc-name {
    display: inline;
    max-width: 150px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* على الشاشات الضيقة: حقل البحث سطرًا كاملًا والأزرار في سطر واحد تحته */
@media (max-width: 520px) {
  .filter-bar .search-wrap { flex: 1 1 100%; }
  .filter-bar > .btn { flex: 1 1 auto; }
  .filter-bar > a.btn-sm { flex: 0 0 auto; }
}
