/*
 * نظام التصميم.
 *
 * مصدر واحد للألوان والمسافات والمكوّنات. قبله كانت كل صفحة تعيد تعريف الألوان
 * في إعداد Tailwind الخاص بها وتكرّر سلاسل الأدوات الطويلة في كل جدول — فأي
 * تعديل بصري يعني تحرير عشرة ملفات، وأي سهو يترك صفحة تبدو غريبة عن أخواتها.
 */

:root {
  /* الهوية */
  --brand:        #0D9488;
  --brand-dark:   #0F766E;
  --brand-soft:   #E6F4F1;
  --brand-tint:   #F0FAF8;

  /* محايدات */
  --bg:           #F6F7F9;
  --surface:      #FFFFFF;
  --border:       #E8EAED;
  --border-strong:#D5D9DE;
  --text:         #1A1D21;
  --text-muted:   #6B7280;
  --text-faint:   #9CA3AF;

  /* دلالات */
  --success:      #059669;
  --success-soft: #ECFDF5;
  --warning:      #D97706;
  --warning-soft: #FFFBEB;
  --danger:       #DC2626;
  --danger-soft:  #FEF2F2;
  --info:         #2563EB;
  --info-soft:    #EFF6FF;

  /* مقاييس */
  --radius:       10px;
  --radius-lg:    14px;
  --radius-full:  999px;
  --shadow-sm:    0 1px 2px rgba(16,24,40,.05);
  --shadow:       0 1px 3px rgba(16,24,40,.08), 0 1px 2px rgba(16,24,40,.04);
  --shadow-lg:    0 10px 25px rgba(16,24,40,.10), 0 4px 10px rgba(16,24,40,.05);
  --sidebar-w:    248px;
  --topbar-h:     60px;
}

* { box-sizing: border-box; }

/* الأيقونات ترث لون النص وحجمه المحيط — فتتّسق معه بلا ضبط يدوي في كل موضع. */
.icon {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: 0 0 auto;
  vertical-align: -0.18em;
}
.icon--fill { fill: currentColor; stroke: none; }

body {
  margin: 0;
  font-family: 'Cairo', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

/* ───────────────────────────── التخطيط ───────────────────────────── */

.app { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--surface);
  border-inline-end: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar__brand {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.sidebar__logo {
  width: 32px; height: 32px; border-radius: 9px;
  background: var(--brand); color: #fff;
  display: grid; place-items: center;
}
.sidebar__title { font-weight: 800; font-size: 15px; line-height: 1.2; }
.sidebar__sub   { font-size: 11px; color: var(--text-faint); }

.sidebar__nav { padding: 12px 10px; flex: 1 1 auto; }
.nav-group { margin-bottom: 14px; }
.nav-group__label {
  font-size: 11px; font-weight: 700; color: var(--text-faint);
  padding: 0 10px 6px; letter-spacing: .02em;
}
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; margin-bottom: 2px;
  border-radius: var(--radius);
  color: var(--text-muted); font-weight: 700; font-size: 13.5px;
  text-decoration: none; transition: background .12s, color .12s;
}
.nav-item:hover { background: var(--bg); color: var(--text); }
.nav-item--active { background: var(--brand-soft); color: var(--brand-dark); }
.nav-item__icon { display: inline-flex; width: 18px; flex: 0 0 auto; opacity: .85; }
.nav-item--active .nav-item__icon { opacity: 1; }
.nav-item__badge {
  margin-inline-start: auto; font-size: 11px; font-weight: 800;
  background: var(--warning-soft); color: var(--warning);
  padding: 1px 7px; border-radius: var(--radius-full);
}

.sidebar__foot { border-top: 1px solid var(--border); padding: 12px; flex: 0 0 auto; }
.user-chip { display: flex; align-items: center; gap: 10px; }
.user-chip__avatar {
  width: 34px; height: 34px; border-radius: var(--radius-full);
  background: var(--brand-soft); color: var(--brand-dark);
  display: grid; place-items: center; font-weight: 800; font-size: 13px; flex: 0 0 auto;
}
.user-chip__name { font-weight: 700; font-size: 13px; line-height: 1.2; }
.user-chip__role { font-size: 11px; color: var(--text-faint); }

.main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px;
  padding: 0 20px;
  position: sticky; top: 0; z-index: 20;
}
.topbar__title { font-weight: 800; font-size: 17px; }
.topbar__sub   { font-size: 12px; color: var(--text-faint); }
.topbar__actions { margin-inline-start: auto; display: flex; align-items: center; gap: 8px; }
.topbar__burger { display: none; }

.page { padding: 20px; max-width: 1400px; width: 100%; }
.page > * + * { margin-top: 16px; }

/* ───────────────────────────── المكوّنات ───────────────────────────── */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.card__head {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
/* min-width الافتراضي لعنصر مرن هو auto — أي لا ينكمش تحت محتواه، فيدفع
   البطاقة خارج عرض الصفحة. صفّرها على كل مستويات التداخل لا على الأول فقط. */
.card__head > *, .card__tools > * { min-width: 0; }
.card__tools { max-width: 100%; }
.card__title { font-weight: 800; font-size: 15px; }
.card__sub   { font-size: 12px; color: var(--text-faint); }
.card__tools { margin-inline-start: auto; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.card__body  { padding: 16px; }
.card__body--flush { padding: 0; }

/* عناصر الشبكة كعناصر المرن: min-width الافتراضي auto فلا تنكمش تحت محتواها
   ويتمدّد المسار معها. تصفيرها هنا مرة تكفي كل الشبكات. */
.stat-grid > *, .grid-2 > *, .field-grid > * { min-width: 0; }

.stat-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 14px 16px; box-shadow: var(--shadow-sm);
}
.stat__label { font-size: 12px; color: var(--text-muted); }
.stat__value { font-size: 22px; font-weight: 800; margin-top: 4px; line-height: 1.2; }
/* قيمة البطاقة تُحاذي عنوانها: .num يجعلها LTR لضبط الأرقام، والمحاذاة تبقى
   لليمين وإلا انفصلت عن عنوانها في تخطيط عربي. */
.stat .stat__value.num { text-align: right; }
.stat__hint  { font-size: 11px; color: var(--text-faint); margin-top: 2px; }
.stat--brand   .stat__value { color: var(--brand); }
.stat--success .stat__value { color: var(--success); }
.stat--warning .stat__value { color: var(--warning); }
.stat--danger  .stat__value { color: var(--danger); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border: 1px solid transparent;
  border-radius: var(--radius); font-family: inherit; font-weight: 700; font-size: 13px;
  cursor: pointer; transition: background .12s, border-color .12s, opacity .12s;
  white-space: nowrap; text-decoration: none;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn--primary { background: var(--brand); color: #fff; }
.btn--primary:hover:not(:disabled) { background: var(--brand-dark); }
.btn--ghost { background: var(--bg); color: var(--text-muted); border-color: var(--border); }
.btn--ghost:hover:not(:disabled) { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn--soft { background: var(--brand-soft); color: var(--brand-dark); }
.btn--danger { background: var(--danger-soft); color: var(--danger); }
.btn--danger:hover:not(:disabled) { background: var(--danger); color: #fff; }
.btn--warning { background: var(--warning-soft); color: var(--warning); }
.btn--sm { height: 28px; padding: 0 10px; font-size: 12px; }
.btn--block { width: 100%; }
.btn--lg { height: 46px; font-size: 15px; }

.input, .select {
  height: 36px; padding: 0 10px; width: 100%;
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  font-family: inherit; font-size: 13px; outline: none;
  transition: border-color .12s, box-shadow .12s;
}
.input:focus, .select:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.input--mono { font-family: ui-monospace, monospace; }
.field { display: block; }
.field__label { display: block; font-size: 12px; font-weight: 700; color: var(--text-muted); margin-bottom: 4px; }
.field-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }

/* الجداول: أعمدة الأرقام LTR بأرقام متساوية العرض، وإلا اهتزّت في التخطيط العربي */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th {
  text-align: right; font-weight: 700; font-size: 12px; color: var(--text-faint);
  padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
.table td { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: var(--brand-tint); }
.table tr.is-total { font-weight: 800; background: var(--bg); }
.table tr.is-total td { border-top: 2px solid var(--border-strong); }
.num {
  direction: ltr; text-align: left; unicode-bidi: isolate;
  font-variant-numeric: tabular-nums;
}
.mono { font-family: ui-monospace, monospace; font-size: 12px; }
.cell-actions { display: flex; gap: 6px; justify-content: flex-start; }

.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 9px; border-radius: var(--radius-full);
  font-size: 11.5px; font-weight: 700; white-space: nowrap;
}
.badge--neutral { background: var(--bg); color: var(--text-muted); }
.badge--success { background: var(--success-soft); color: var(--success); }
.badge--warning { background: var(--warning-soft); color: var(--warning); }
.badge--danger  { background: var(--danger-soft); color: var(--danger); }
.badge--info    { background: var(--info-soft); color: var(--info); }
.badge--brand   { background: var(--brand-soft); color: var(--brand-dark); }

.empty { text-align: center; padding: 44px 20px; color: var(--text-faint); }
.empty__icon { display: inline-flex; color: var(--text-faint); opacity: .65; }
.empty__title { font-weight: 700; color: var(--text-muted); margin-top: 8px; }
.empty__hint { font-size: 12px; margin-top: 4px; }
.empty__action { margin-top: 14px; }

.skeleton {
  background: linear-gradient(90deg, var(--bg) 25%, #EEF0F2 37%, var(--bg) 63%);
  background-size: 400% 100%; animation: shimmer 1.3s infinite;
  border-radius: 6px; height: 14px;
}
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: -100% 0; } }

.alert { display: flex; gap: 10px; padding: 12px 14px; border-radius: var(--radius); font-size: 13px; align-items: flex-start; }
.alert .icon { margin-top: 1px; }
.alert--warning { background: var(--warning-soft); color: #92400E; }
.alert--danger  { background: var(--danger-soft); color: #991B1B; }
.alert--info    { background: var(--info-soft); color: #1E40AF; }
.alert--success { background: var(--success-soft); color: #065F46; }
.alert__list { margin: 4px 0 0; padding-inline-start: 18px; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(16,24,40,.45);
  display: flex; align-items: center; justify-content: center;
  padding: 16px; z-index: 60; overflow-y: auto;
}
.modal {
  background: var(--surface); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg); width: 100%; max-width: 480px; margin: auto;
}
.modal--wide { max-width: 720px; }
.modal__head { padding: 16px 18px 0; }
.modal__title { font-weight: 800; font-size: 16px; }
.modal__sub { font-size: 12.5px; color: var(--text-muted); margin-top: 3px; }
.modal__body { padding: 16px 18px; }
.modal__foot { display: flex; gap: 8px; padding: 0 18px 18px; }
.modal__foot .btn { flex: 1; }

.toast-host {
  position: fixed; bottom: 20px; inset-inline-start: 50%;
  transform: translateX(50%); z-index: 90;
  display: flex; flex-direction: column; gap: 8px; align-items: center;
}
.toast {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px; border-radius: var(--radius);
  box-shadow: var(--shadow-lg); font-size: 13px; font-weight: 700;
  animation: rise .18s ease-out;
}
.toast--success { background: var(--success); color: #fff; }
.toast--error   { background: var(--danger); color: #fff; }
.toast--info    { background: var(--text); color: #fff; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

.form-error { color: var(--danger); font-size: 12.5px; margin-top: 8px; }
.form-ok    { color: var(--success); font-size: 12.5px; margin-top: 8px; }

/* صف التبويبات لا ينكمش تحت محتواه، فيدفع الصفحة كلها على الشاشات الضيّقة.
   الانزلاق الأفقي هنا أنسب من اللفّ: يبقى ترتيب التصنيفات مقروءًا. */
.tabs {
  display: flex; gap: 4px;
  min-width: 0; max-width: 100%; overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto;
  padding: 7px 14px; border-radius: var(--radius); border: none;
  font-family: inherit; font-weight: 700; font-size: 13px; cursor: pointer;
  background: transparent; color: var(--text-muted);
}
.tab:hover { background: var(--bg); }
.tab--active { background: var(--brand); color: #fff; }

.muted { color: var(--text-muted); }
.faint { color: var(--text-faint); }
.tone-success { color: var(--success); }
.tone-warning { color: var(--warning); }
.tone-danger  { color: var(--danger); }
.tone-brand   { color: var(--brand); }
.strong { font-weight: 700; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.split { display: flex; justify-content: space-between; gap: 12px; }
.grid-2 { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }

/* ───────────────────────────── الجوال ───────────────────────────── */

@media (max-width: 900px) {
  /* الشريط المطويّ يبقى خارج الشاشة بـ transform، لكنه يظلّ يوسّع منطقة
     التمرير الأفقي فتنزلق الصفحة كلها. قصّ الأفقي عند الجذر يمنع ذلك —
     ولا يخفي محتوى حقيقيًا لأن الجداول تنزلق داخل حاوياتها لا في الصفحة. */
  html, body { overflow-x: hidden; }

  /* inset-inline-start في RTL هو الحافة اليمنى — وهي مكان الشريط.
     استعمال inset-inline-end يضعه يسارًا فيظهر في منتصف الشاشة عند الطيّ. */
  .sidebar {
    position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 70;
    width: min(var(--sidebar-w), 84vw);
    transform: translateX(100%); transition: transform .2s ease;
    box-shadow: var(--shadow-lg);
  }
  [dir="ltr"] .sidebar { transform: translateX(-100%); }
  .sidebar--open { transform: translateX(0) !important; }
  .topbar__burger {
    display: inline-flex; align-items: center; justify-content: center;
    width: 36px; height: 36px; border-radius: var(--radius);
    background: var(--bg); border: 1px solid var(--border);
    color: var(--text-muted); cursor: pointer;
  }
  .scrim { position: fixed; inset: 0; background: rgba(16,24,40,.4); z-index: 65; }
  .page { padding: 14px; }
  .topbar { padding: 0 14px; }
  .topbar__sub { display: none; }
}

/* ───────────────────────────── الطباعة ───────────────────────────── */

@media print {
  .sidebar, .topbar, .no-print, .toast-host { display: none !important; }
  body { background: #fff; font-size: 12px; }
  .page { padding: 0; max-width: none; }
  .card { border: none; box-shadow: none; break-inside: avoid; }
  .card__head { border-bottom: 1px solid #000; padding-inline: 0; }
  .card__body { padding-inline: 0; }
  .table th { color: #000; border-bottom: 1px solid #000; }
  .table td { border-bottom: 1px solid #DDD; }
  .table tbody tr:hover { background: none; }
  .print-only { display: block !important; }
}
.print-only { display: none; }
