/* ============================================================================
   أداة "المساجد المنتهية" — صفحة بيانات

   المبدأ الحاكم: البيانات هي الصفحة.
   الجدول يبدأ فوق حد الشاشة الأولى. النموذج درج يُفتح عند الطلب. الرسوم
   مطوية لأنها تُراجَع شهرياً لا يومياً. الفلاتر ملاصقة للجدول الذي تتحكم به.
   ========================================================================= */

/* -------------------------------------------------------- نص لقارئ الشاشة */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.page-records {
  padding-block: var(--sp-6) var(--sp-8);
}

/* ============================================================ رأس الصفحة */
.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--sp-4);
  margin-bottom: var(--sp-5);
}

.page-title {
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  flex-wrap: wrap;
}

.page-title h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: var(--fs-xl);
  font-weight: 700;
  color: var(--paper);
}

/* حالة الاتصال شارة صغيرة لا قسماً كاملاً — معلومة مطمئنة لا مهمة */
.conn-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 3px var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--radius-pill);
  background: var(--surface-sunken);
  color: var(--muted);
  font-size: var(--fs-caption);
  white-space: nowrap;
}

.conn-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
}

.conn-badge.is-ok .conn-dot { background: var(--green); }
.conn-badge.is-wait .conn-dot { background: var(--gold); }
.conn-badge.is-off .conn-dot { background: var(--red); }
.conn-badge.is-ok { color: var(--green); }
.conn-badge.is-off { color: var(--red); }

/* =========================================================== شريط البطاقات */
.stat-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--sp-3);
  margin-bottom: var(--sp-5);
}

.stat-cell {
  padding: var(--sp-4);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-1);
}

.stat-cell-label {
  display: block;
  margin-bottom: var(--sp-2);
  color: var(--muted);
  font-size: var(--fs-caption);
}

.stat-cell-value {
  display: block;
  font-family: var(--font-mono);
  font-size: var(--fs-lg);
  font-weight: 600;
  color: var(--paper);
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}

/* الرقم المجرّد لا معنى له — الدلتا هي التي تجيب "هل هذا جيد؟" */
.stat-cell-delta {
  display: block;
  margin-top: var(--sp-2);
  font-size: var(--fs-caption);
  color: var(--muted);
}

.stat-cell-delta.is-up { color: var(--green); }
.stat-cell-delta.is-down { color: var(--red); }

/* =========================================================== شريط الأدوات */
.records-toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--sp-2);
  margin-bottom: var(--sp-3);
}

.toolbar-gap { flex: 1 1 auto; }

.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
  flex: 1 1 240px;
  max-width: 340px;
}

.search-wrap svg {
  position: absolute;
  inset-inline-start: var(--sp-3);
  color: var(--muted);
  pointer-events: none;
}

.search-wrap input {
  width: 100%;
  padding: var(--sp-2) var(--sp-3);
  padding-inline-start: calc(var(--sp-3) + 22px);
  border: 1px solid var(--line);
  border-radius: var(--radius-xs);
  background: var(--surface-1);
  color: var(--paper);
  font-family: var(--font-body);
  font-size: var(--fs-sm);
}

.search-wrap input:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-glow);
}

.records-toolbar select {
  padding: var(--sp-2) var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--radius-xs);
  background: var(--surface-1);
  color: var(--text-2);
  font-family: var(--font-body);
  font-size: var(--fs-sm);
  cursor: pointer;
}

.records-toolbar select:hover { border-color: var(--line-strong); }

.records-toolbar select:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-glow);
}

/* الفلتر المفعَّل يجب أن يُرى: بلا هذا ينسى المستخدم أنه يرى جزءاً من بياناته */
.records-toolbar select.is-set {
  border-color: var(--gold);
  color: var(--gold-light);
}

.chip-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--radius-xs);
  background: var(--surface-1);
  color: var(--text-2);
  font-family: var(--font-body);
  font-size: var(--fs-sm);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

.chip-btn:hover {
  border-color: var(--line-strong);
  color: var(--paper);
}

.chip-btn:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.chip-btn.is-danger { color: var(--red); }
.chip-btn.is-danger:hover { border-color: var(--red); color: var(--red); }

/* ====================================================== شريط الاختيار المتعدد */
.bulk-bar {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin-bottom: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  border: 1px solid var(--gold);
  border-radius: var(--radius-sm);
  background: var(--gold-wash);
  color: var(--gold-light);
  font-size: var(--fs-sm);
}

/* ================================================================ الجدول */
.records-card {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-1);
  overflow: hidden;
}

.table-scroll { overflow-x: auto; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
}

.data-table th,
.data-table td {
  padding: var(--sp-3) var(--sp-4);
  text-align: start;
  white-space: nowrap;
}

.data-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  border-bottom: 1px solid var(--line-strong);
  background: var(--surface-2);
  color: var(--muted);
  font-size: var(--fs-caption);
  font-weight: 500;
}

.data-table th.is-sortable { cursor: pointer; user-select: none; }
.data-table th.is-sortable:hover { color: var(--paper); }
.data-table th.is-active { color: var(--gold); }
.data-table th[data-dir="asc"]::after { content: " ↑"; }
.data-table th[data-dir="desc"]::after { content: " ↓"; }

.data-table tbody td {
  border-bottom: 1px solid var(--line);
  color: var(--text-2);
}

.data-table tbody tr:last-child td { border-bottom: 0; }
.data-table tbody tr:hover td { background: var(--surface-2); }
.data-table tbody tr.is-selected td { background: var(--gold-wash); }

/* الأرقام بخط أحادي وأعمدة متساوية — تتراصف الخانات فتُقارَن بالعين */
.data-table td.num,
.data-table td.mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.data-table td.name {
  color: var(--paper);
  font-weight: 500;
  white-space: normal;
  min-width: 180px;
}

.data-table td.price {
  color: var(--gold-light);
  font-weight: 600;
}

.col-check { width: 40px; }
.col-actions { width: 84px; }

.data-table input[type="checkbox"] {
  width: 15px;
  height: 15px;
  accent-color: var(--gold);
  cursor: pointer;
}

/* صف المجموع: جدول أموال بلا مجموع ناقص. يعكس ما بعد الفلترة لا كل شيء */
.data-table tfoot td {
  border-top: 1px solid var(--line-strong);
  background: var(--surface-sunken);
  color: var(--paper);
  font-weight: 600;
}

.data-table tfoot .price {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  color: var(--gold-light);
}

.row-actions {
  display: flex;
  gap: var(--sp-1);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-xs);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}

.icon-btn:hover { color: var(--paper); border-color: var(--line-strong); }
.icon-btn.danger:hover { color: var(--red); border-color: var(--red); }

.icon-btn:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 1px;
}

.pill {
  display: inline-block;
  padding: 1px var(--sp-2);
  border-radius: var(--radius-pill);
  font-size: var(--fs-caption);
  white-space: nowrap;
}

.pill.is-wait { color: var(--gold); }

.records-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  border-top: 1px solid var(--line);
  background: var(--surface-sunken);
}

.table-count { color: var(--muted); font-size: var(--fs-caption); }

/* ------------------------------------------------------ هيكل التحميل */
.data-table tr.is-skeleton td { padding-block: var(--sp-4); }

.data-table tr.is-skeleton span {
  display: block;
  height: 11px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--surface-3) 0%, var(--line-strong) 50%, var(--surface-3) 100%);
  background-size: 200% 100%;
  animation: skeleton-sweep 1.4s ease-in-out infinite;
}

@keyframes skeleton-sweep {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}

/* -------------------------------------------------------- الحالة الفارغة */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-8) var(--sp-5);
  color: var(--muted);
  font-size: var(--fs-body);
  text-align: center;
}

.empty-state svg { color: var(--gold); opacity: 0.5; }

.empty-state h3 {
  margin: 0;
  color: var(--paper);
  font-size: var(--fs-md);
  font-weight: 600;
}

.empty-state p { max-width: 36ch; margin: 0; }
.empty-state button { margin-top: var(--sp-2); }

/* ============================================================= التحليلات */
.analytics {
  margin-top: var(--sp-5);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-1);
}

.analytics > summary {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-4);
  color: var(--paper);
  font-size: var(--fs-body);
  font-weight: 500;
  cursor: pointer;
  list-style: none;
}

.analytics > summary::-webkit-details-marker { display: none; }
.analytics > summary:hover { color: var(--gold-light); }

.analytics > summary .hint { margin: 0; margin-inline-start: auto; }

.analytics > summary .chev {
  color: var(--gold);
  transition: transform var(--dur-fast) var(--ease);
}

/* السهم يشير لليسار في RTL، ولأسفل عند الفتح */
.analytics > summary .chev { transform: rotate(180deg); }
.analytics[open] > summary .chev { transform: rotate(90deg); }

.analytics[open] > summary { border-bottom: 1px solid var(--line); }

.analytics .chart-grid { padding: var(--sp-5); }

.page-note {
  margin-top: var(--sp-5);
  color: var(--muted);
  font-size: var(--fs-sm);
  line-height: 1.8;
}

/* ================================================================= الدرج */
.drawer-scrim {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(4, 8, 15, 0.7);
  animation: scrim-in var(--dur-fast) var(--ease);
}

@keyframes scrim-in { from { opacity: 0; } to { opacity: 1; } }

/* ينزلق من اليمين لأن الصفحة RTL — يخرج من الجهة التي بدأ منها النظر */
.drawer {
  position: fixed;
  inset-block: 0;
  inset-inline-end: 0;
  z-index: 61;
  display: flex;
  flex-direction: column;
  width: min(460px, 100%);
  border-inline-start: 1px solid var(--line-strong);
  background: var(--surface-1);
  box-shadow: -18px 0 48px rgba(0, 0, 0, 0.45);
  animation: drawer-in var(--dur) var(--ease);
}

@keyframes drawer-in {
  from { transform: translateX(-100%); opacity: 0.4; }
  to { transform: translateX(0); opacity: 1; }
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-5);
  border-bottom: 1px solid var(--line);
}

.drawer-head h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: var(--fs-lg);
  color: var(--paper);
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-5);
}

.drawer-foot {
  display: flex;
  gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5);
  border-top: 1px solid var(--line);
  background: var(--surface-sunken);
}

.drawer-foot .primary { flex: 1; }

.grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-4);
}

/* حقل به خطأ: الحدّ الأحمر عند الحقل نفسه، لا رسالة في صندوق بعيد */
.field.has-error input,
.field.has-error select {
  border-color: var(--red);
}

.field-error {
  display: block;
  margin-top: var(--sp-1);
  color: var(--red);
  font-size: var(--fs-caption);
}

/* ============================================================== التنبيهات */
.toast {
  position: fixed;
  inset-block-end: var(--sp-5);
  inset-inline-start: 50%;
  transform: translateX(50%);
  z-index: 70;
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  max-width: min(520px, calc(100vw - 32px));
  padding: var(--sp-3) var(--sp-5);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-pill);
  background: var(--surface-3);
  color: var(--paper);
  font-size: var(--fs-sm);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  animation: toast-in var(--dur-fast) var(--ease);
}

@keyframes toast-in {
  from { opacity: 0; transform: translate(50%, 12px); }
  to { opacity: 1; transform: translate(50%, 0); }
}

.toast.is-error { border-color: var(--red); color: var(--red); }
.toast.is-ok { border-color: var(--green); }

.toast button {
  border: 0;
  background: transparent;
  color: var(--gold);
  font-family: var(--font-body);
  font-size: var(--fs-sm);
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.toast button:hover { text-decoration: underline; }

/* ================================================================ الجوال */
@media (max-width: 900px) {
  .stat-strip { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 720px) {
  .page-head { align-items: stretch; }
  .page-head .primary { width: 100%; justify-content: center; }

  .search-wrap { max-width: none; flex-basis: 100%; }
  .records-toolbar select { flex: 1 1 30%; }

  /* الجدول يصير بطاقات: ثمانية أعمدة على ٣٧٥ بكسل تعني تمريراً أفقياً
     داخل تمرير عمودي، وعمود الإجراءات يقع في طرف لا يُرى.
     التسميات تأتي من data-label الذي يضعه completed.js. */
  .records-card { border: 0; background: transparent; }
  .table-scroll { overflow: visible; }

  .data-table,
  .data-table tbody,
  .data-table tfoot,
  .data-table tr,
  .data-table td { display: block; width: 100%; }

  .data-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
  }

  .data-table tbody tr {
    display: flex;
    flex-direction: column;
    margin-bottom: var(--sp-3);
    padding: var(--sp-4);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    background: var(--surface-1);
  }

  .data-table tbody tr:hover td,
  .data-table tbody tr.is-selected td { background: transparent; }

  .data-table tbody tr.is-selected {
    border-color: var(--gold);
    background: var(--gold-wash);
  }

  .data-table tbody td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp-4);
    padding: var(--sp-2) 0;
    border: 0;
    white-space: normal;
  }

  .data-table tbody td + td { border-top: 1px solid var(--line); }

  .data-table tbody td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    color: var(--muted);
    font-size: var(--fs-caption);
  }

  /* اسم المسجد عنوان البطاقة، والسعر تحته — هما ما تبحث عنه العين أولاً */
  .data-table td.name {
    order: -3;
    padding-bottom: var(--sp-2);
    border-top: 0;
    font-size: var(--fs-md);
  }

  .data-table td.price { order: -2; border-top: 0; font-size: var(--fs-md); }
  .data-table td.col-check { order: -4; border-top: 0; }
  .data-table td.col-check::before { content: "تحديد"; }

  .data-table td.name::before,
  .data-table td.price::before { content: attr(data-label); }

  .data-table td.row-actions { justify-content: stretch; padding-top: var(--sp-3); }
  .data-table td.row-actions::before { content: none; }
  .data-table td.row-actions .icon-btn { flex: 1; min-height: 40px; }

  .data-table tr.is-skeleton td::before { content: none; }

  .data-table tfoot td {
    display: flex;
    justify-content: space-between;
    border-radius: var(--radius-sm);
  }

  .records-foot { padding-inline: 0; background: transparent; border: 0; }
  .chart-grid { grid-template-columns: 1fr; }
  .drawer { width: 100%; }
}
.chart-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: var(--sp-4);
}

.chart-card {
  padding: var(--sp-5);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-sunken);
}

.chart-card h3 {
  margin: 0 0 var(--sp-4);
  font-family: var(--font-body);
  font-size: var(--fs-body);
  font-weight: 600;
  color: var(--paper);
}

.chart-body {
  min-height: 150px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.chart-svg {
  width: 100%;
  height: 150px;
  overflow: visible;
}

.chart-svg .bar {
  fill: var(--bar-fill, var(--gold));
  opacity: 0.9;
  transition: opacity var(--dur-fast) var(--ease);
}

.chart-svg .bar:hover {
  opacity: 1;
}

.chart-svg .bar-label {
  fill: var(--muted);
  font-size: 2.6px;
  text-anchor: middle;
  font-family: var(--font-body);
}

.chart-scale {
  display: flex;
  justify-content: space-between;
  margin-top: var(--sp-2);
  color: var(--muted);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: var(--fs-caption);
}

.chart-empty {
  margin: 0;
  color: var(--muted);
  font-size: var(--fs-sm);
  text-align: center;
}

/* أشرطة أفقية (المحافظات والسنوات) */
.hbar-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}

.hbar {
  display: grid;
  grid-template-columns: minmax(70px, 1fr) 2fr auto;
  align-items: center;
  gap: var(--sp-3);
  font-size: var(--fs-sm);
}

.hbar-name {
  color: var(--text-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hbar-track {
  height: 8px;
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  overflow: hidden;
}

.hbar-fill {
  display: block;
  height: 100%;
  border-radius: var(--radius-pill);
  background: var(--gold);
  opacity: 0.85;
}

.hbar-val {
  color: var(--gold-light);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: var(--fs-caption);
  white-space: nowrap;
}

/* -------------------------------------------- اختيار من أداة القبلة */
.from-qibla {
  margin-bottom: var(--sp-5);
  padding: var(--sp-4);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-sunken);
}

.from-qibla label {
  display: block;
  margin-bottom: var(--sp-2);
  color: var(--text-2);
  font-size: var(--fs-sm);
  font-weight: 500;
}

.from-qibla select {
  width: 100%;
  padding: var(--sp-2) var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--radius-xs);
  background: var(--surface-1);
  color: var(--paper);
  font-family: var(--font-body);
  font-size: var(--fs-body);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}

.from-qibla select:hover {
  border-color: var(--line-strong);
}

.from-qibla select:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-glow);
}

/* -------------------------------------------------- زر التدمير
   بلا هذا يبدو زر الحذف مطابقاً لزر الإضافة الذهبي تماماً — وهو أخطر
   ما في حوار التأكيد: أن يتساوى الفعل الذي لا رجعة فيه مع غيره. */
button.primary.danger {
  background: var(--red);
  border-color: var(--red);
  color: #2a0a0a;
}

button.primary.danger:hover {
  background: #fca5a5;
  border-color: #fca5a5;
}

button.primary.danger:focus-visible {
  outline: 2px solid var(--red);
  outline-offset: 2px;
}
