/* ==========================================================================
   PHONE LAYER — هندام admin
   طبقة تُحمّل بعد styles.css وتعيد بناء التجربة على الهواتف (≤ 768px)
   ========================================================================== */

:root {
  --m-gutter: 16px;
  --m-tap: 44px;
  --m-topbar-h: 56px;
  --m-tabbar-h: 60px;
  --m-safe-t: env(safe-area-inset-top, 0px);
  --m-safe-b: env(safe-area-inset-bottom, 0px);
  --m-radius: 16px;
  --m-ink: #0d1f1a;
  --m-ink-2: #5b6b66;
  --m-line: rgba(13, 31, 26, 0.09);
  --m-surface: #ffffff;
  --m-bg: #f4f6f5;
  --m-brand: #0b5c45;
  --m-brand-deep: #04211a;
  --m-gold: #d98a1a;
}

.m-only { display: none; }

/* أيقونات SVG داخل عناصر التنقل */
.nav-icon svg,
.m-icon-btn svg,
.m-tab-ico svg {
  width: 21px;
  height: 21px;
  display: block;
}
.m-has-icon { display: inline-flex; align-items: center; gap: 7px; }
.m-has-icon svg { width: 17px; height: 17px; flex: 0 0 auto; }
.m-svg-badge { display: grid !important; place-items: center; }
.m-svg-badge svg { width: 19px; height: 19px; }

@media (max-width: 768px) {
  html, body { overflow-x: hidden; max-width: 100%; }
  body {
    background: var(--m-bg);
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
  }
  .m-only { display: flex; }
  .m-hide { display: none !important; }
  input, select, textarea { font-size: 16px !important; }
  button, a, .nav-item, .pill-btn { -webkit-tap-highlight-color: transparent; }

  /* ---------- الهيكل ---------- */
  .dashboard-shell { display: block; min-height: 100dvh; }
  .dashboard-main {
    padding: calc(var(--m-topbar-h) + var(--m-safe-t) + 14px) var(--m-gutter)
             calc(var(--m-tabbar-h) + var(--m-safe-b) + 28px);
    width: 100%;
    max-width: 100%;
  }

  /* ---------- الشريط العلوي ---------- */
  .topbar {
    position: fixed;
    inset: 0 0 auto 0;
    height: calc(var(--m-topbar-h) + var(--m-safe-t));
    padding: var(--m-safe-t) var(--m-gutter) 0;
    margin: 0;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    background: var(--m-brand-deep);
    border-bottom: 1px solid rgba(255, 255, 255, 0.07);
    z-index: 900;
  }
  .topbar-left { flex: 1; min-width: 0; }
  .breadcrumbs { display: none; }
  .topbar-left #page-title {
    font-size: 16.5px;
    font-weight: 800;
    letter-spacing: -0.2px;
    color: #fff;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .topbar-right {
    width: auto;
    flex: 0 0 auto;
    justify-content: flex-end;
    gap: 4px;
  }
  .topbar-right .admin-details { display: none; }
  .topbar-right .admin-profile-pill {
    padding: 0;
    margin-inline-start: 4px;
    background: none;
    border: none;
    box-shadow: none;
  }
  .topbar .admin-avatar {
    width: 34px;
    height: 34px;
    min-width: 34px;
    font-size: 14px;
    display: grid;
    place-items: center;
    border: 1.5px solid rgba(255, 255, 255, 0.22);
  }
  .topbar .m-icon-btn,
  .topbar .icon-action-btn {
    width: var(--m-tap);
    height: var(--m-tap);
    min-width: var(--m-tap);
    display: grid;
    place-items: center;
    border: none;
    background: transparent;
    color: rgba(255, 255, 255, 0.86);
    border-radius: 12px;
    font-size: 0;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
  }
  .topbar .m-icon-btn:active { background: rgba(255, 255, 255, 0.12); }
  .topbar .m-icon-btn.active { background: rgba(255, 255, 255, 0.16); color: #fff; }

  /* شريط بحث منسدل */
  .m-search-row {
    position: fixed;
    top: calc(var(--m-topbar-h) + var(--m-safe-t));
    inset-inline: 0;
    padding: 0 var(--m-gutter);
    max-height: 0;
    overflow: hidden;
    background: var(--m-brand-deep);
    z-index: 899;
    transition: max-height 0.22s ease, padding 0.22s ease;
  }
  .m-search-row.open {
    max-height: 72px;
    padding: 0 var(--m-gutter) 10px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.07);
  }
  .m-search-row .search-bar-wrap { display: flex; width: 100%; }

  /* ---------- الدرج الجانبي ---------- */
  .sidebar {
    position: fixed;
    inset: 0 0 0 auto;
    width: min(84vw, 320px);
    height: 100dvh;
    padding: calc(18px + var(--m-safe-t)) 14px calc(18px + var(--m-safe-b));
    transform: translateX(101%);
    transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
    z-index: 1000;
    overflow-y: auto;
    overscroll-behavior: contain;
    box-shadow: -18px 0 50px rgba(0, 0, 0, 0.35);
    border: none;
  }
  html[dir='ltr'] .sidebar { inset: 0 auto 0 0; transform: translateX(-101%); }
  .sidebar.m-open { transform: translateX(0); }

  .m-drawer-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(2, 16, 12, 0.5);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    z-index: 999;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.28s ease;
  }
  .m-drawer-backdrop.m-open { opacity: 1; pointer-events: auto; }
  body.m-drawer-open { overflow: hidden; }

  .sidebar-nav {
    flex-direction: column;
    overflow: visible;
    flex: 0 0 auto;
    gap: 2px;
    padding: 0;
  }
  .sidebar-footer { margin-top: 22px; }
  .nav-group-label {
    display: block;
    font-size: 10.5px;
    letter-spacing: 1.2px;
    padding: 16px 12px 6px;
    color: rgba(255, 255, 255, 0.38);
  }
  .nav-item {
    width: 100%;
    min-height: 46px;
    gap: 12px;
    padding: 11px 12px;
    border-radius: 12px;
    white-space: normal;
    text-align: start;
    font-size: 14.5px;
    transition: background 0.15s ease;
  }
  .nav-item:active { background: rgba(255, 255, 255, 0.1); }
  .nav-icon {
    width: 21px;
    height: 21px;
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    opacity: 0.85;
  }
  .nav-item.active .nav-icon { opacity: 1; }

  /* ---------- شريط التبويبات السفلي ---------- */
  .m-tabbar {
    position: fixed;
    inset: auto 0 0 0;
    height: calc(var(--m-tabbar-h) + var(--m-safe-b));
    padding-bottom: var(--m-safe-b);
    display: flex;
    align-items: stretch;
    background: rgba(255, 255, 255, 0.94);
    backdrop-filter: saturate(180%) blur(14px);
    -webkit-backdrop-filter: saturate(180%) blur(14px);
    border-top: 1px solid var(--m-line);
    z-index: 950;
  }
  .m-tab {
    flex: 1 1 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    border: none;
    background: none;
    color: var(--m-ink-2);
    cursor: pointer;
    padding: 6px 2px;
    font-family: inherit;
    transition: color 0.15s ease;
  }
  .m-tab-ico {
    position: relative;
    display: grid;
    place-items: center;
    width: 30px;
    height: 26px;
    border-radius: 9px;
    transition: background 0.18s ease, transform 0.18s ease;
  }
  .m-tab-label {
    font-size: 10.5px;
    font-weight: 700;
    letter-spacing: -0.1px;
  }
  .m-tab.active { color: var(--m-brand); }
  .m-tab.active .m-tab-ico { background: rgba(11, 92, 69, 0.12); }
  .m-tab:active .m-tab-ico { transform: scale(0.9); }
  .m-tab-badge {
    position: absolute;
    top: -3px;
    inset-inline-end: -4px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 8px;
    background: #dc2626;
    color: #fff;
    font-size: 10px;
    font-weight: 800;
    line-height: 16px;
    text-align: center;
    box-shadow: 0 0 0 2px #fff;
  }

  /* ---------- بطاقات ومسافات ---------- */
  .kpi-grid > *,
  .analytics-grid > *,
  .dashboard-grid > *,
  .dashboard-card,
  .glass-card,
  .table-card { min-width: 0; }

  .analytics-grid,
  .dashboard-grid,
  .cards-grid,
  .stats-grid { grid-template-columns: 1fr !important; gap: 14px; }

  /* مؤشرات الأداء: بطاقتان في الصف، مضغوطتان */
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px;
  }
  .kpi-card {
    display: flex;
    flex-direction: column;
    padding: 14px 13px;
    border-radius: 15px;
    gap: 6px;
    min-height: 132px;
  }
  .kpi-card .kpi-label { margin-top: 2px; }
  .kpi-card .kpi-value-wrap { margin-top: auto; }
  .kpi-icon-wrap {
    width: 32px;
    height: 32px;
    display: grid;
    place-items: center;
    border-radius: 10px;
  }
  .kpi-icon-wrap svg { width: 18px; height: 18px; }
  .kpi-trend { font-size: 10px; padding: 3px 7px; }
  .kpi-label { font-size: 11.5px; font-weight: 700; color: var(--m-ink-2); }
  .kpi-value-wrap strong {
    font-size: 21px;
    letter-spacing: -0.6px;
    font-variant-numeric: tabular-nums;
  }
  .kpi-unit { font-size: 11px; }
  .kpi-hint { display: none; }

  .glass-card,
  .dashboard-card {
    border-radius: var(--m-radius);
    padding: 16px 14px;
    box-shadow: 0 1px 2px rgba(13, 31, 26, 0.05), 0 6px 18px -12px rgba(13, 31, 26, 0.3);
  }

  .hero-banner {
    flex-direction: column;
    align-items: stretch;
    gap: 14px;
    padding: 20px 18px;
    border-radius: 18px;
    text-align: start;
  }
  .hero-banner h2 { font-size: 19px; line-height: 1.45; }
  .hero-banner p { font-size: 13px; }
  .hero-banner .primary-btn-gold { width: 100%; justify-content: center; min-height: 46px; }

  /* رأس القسم + الفلاتر */
  .section-header-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    margin-bottom: 14px;
  }
  /* العنوان مكرّر في الشريط العلوي، فنُخفيه هنا */
  .section-header-bar h2 { display: none; }
  .section-header-bar p { font-size: 12.5px; line-height: 1.6; }

  .filter-actions {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 8px;
    padding: 2px var(--m-gutter) 4px;
    margin-inline: calc(var(--m-gutter) * -1);
    scroll-padding-inline: var(--m-gutter);
  }
  .filter-actions::-webkit-scrollbar { display: none; }
  .filter-actions > * { flex: 0 0 auto; }
  .pill-btn { min-height: 38px; border-radius: 999px; font-size: 13px; padding: 0 15px; }
  .custom-select,
  .primary-btn-gold { min-height: 44px; border-radius: 12px; }

  /* ---------- الجداول → بطاقات ---------- */
  .table-responsive { overflow: visible; }
  .table-card {
    background: transparent;
    border: none;
    box-shadow: none;
    padding: 0;
  }
  .data-table thead { display: none; }
  .data-table,
  .data-table tbody,
  .data-table tr,
  .data-table td { display: block; width: 100%; }

  .data-table tbody tr {
    border: 1px solid var(--m-line);
    border-radius: var(--m-radius);
    padding: 4px 14px 10px;
    margin-bottom: 10px;
    background: var(--m-surface);
    box-shadow: 0 1px 2px rgba(13, 31, 26, 0.04), 0 8px 20px -16px rgba(13, 31, 26, 0.35);
  }
  .data-table tbody tr:hover { background: var(--m-surface); }

  .data-table td {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 14px;
    padding: 8px 0;
    border: none;
    text-align: start;
    white-space: normal;
    font-size: 13.5px;
    color: var(--m-ink);
  }
  .data-table td::before {
    content: attr(data-label);
    flex: 0 0 38%;
    font-size: 11.5px;
    font-weight: 700;
    color: var(--m-ink-2);
    letter-spacing: -0.1px;
  }
  .data-table td:not([data-label])::before { display: none; }
  .data-table td .m-val {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: flex-end;
    gap: 6px;
    text-align: end;
    font-weight: 600;
  }

  /* الخلية الأولى = عنوان البطاقة */
  .data-table tbody tr td:first-child {
    display: block;
    padding: 12px 0 10px;
    margin-bottom: 2px;
    border-bottom: 1px solid var(--m-line);
    font-size: 15px;
    font-weight: 800;
    letter-spacing: -0.2px;
    color: var(--m-ink);
    line-height: 1.5;
  }
  .data-table tbody tr td:first-child::before { display: none; }
  .data-table tbody tr td:first-child small,
  .data-table tbody tr td:first-child span:not(.badge):not(.status-tag) {
    font-weight: 600;
    color: var(--m-ink-2);
  }

  /* خلية الحالة */
  .data-table td.m-status-cell { align-items: center; }

  /* خلية الإجراءات = تذييل البطاقة */
  .data-table td.m-actions-cell {
    display: flex;
    flex-wrap: wrap;
    justify-content: stretch;
    gap: 8px;
    margin-top: 6px;
    padding-top: 12px;
    border-top: 1px solid var(--m-line);
  }
  .data-table td.m-actions-cell::before { display: none; }
  .data-table td.m-actions-cell button,
  .data-table td.m-actions-cell a {
    flex: 1 1 42%;
    min-height: 40px;
    border-radius: 11px;
    font-size: 13px;
    font-weight: 700;
  }

  /* صف الحالة/الفراغ */
  .data-table tr.m-state-row {
    border: 1px dashed rgba(13, 31, 26, 0.16);
    background: transparent;
    box-shadow: none;
    padding: 6px;
  }
  .data-table tr.m-state-row td {
    display: block;
    text-align: center;
    padding: 16px 8px;
    font-size: 13.5px;
    color: var(--m-ink-2);
  }
  .data-table tr.m-state-row td::before { display: none; }

  /* هياكل التحميل */
  .m-skel-wrap { display: grid; gap: 9px; margin-bottom: 12px; }
  .m-skel {
    height: 12px;
    border-radius: 6px;
    background: linear-gradient(90deg, #e7ebe9 25%, #f2f5f4 50%, #e7ebe9 75%);
    background-size: 240% 100%;
    animation: m-shimmer 1.4s ease-in-out infinite;
  }
  .m-skel-lg { height: 16px; width: 62%; }
  .m-skel-sm { width: 40%; }
  .m-skel-text { font-size: 12.5px; color: var(--m-ink-2); }
  @keyframes m-shimmer {
    from { background-position: 140% 0; }
    to { background-position: -40% 0; }
  }
  @media (prefers-reduced-motion: reduce) {
    .m-skel { animation: none; }
  }

  /* ---------- النوافذ كورقة سفلية ---------- */
  .modal-backdrop {
    padding: 0;
    align-items: flex-end;
    background: rgba(2, 16, 12, 0.45);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
  }
  .modal-card {
    width: 100%;
    max-width: 100%;
    max-height: 90dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    border-radius: 22px 22px 0 0;
    padding-bottom: calc(18px + var(--m-safe-b));
    animation: m-sheet-up 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  }
  @keyframes m-sheet-up {
    from { transform: translateY(6%); opacity: 0.6; }
    to { transform: translateY(0); opacity: 1; }
  }
  /* مقبض السحب */
  .modal-card::before {
    content: '';
    display: block;
    width: 38px;
    height: 4px;
    margin: 2px auto 12px;
    border-radius: 4px;
    background: rgba(13, 31, 26, 0.16);
  }
  .modal-header { align-items: flex-start; gap: 10px; }
  .modal-header h3 { font-size: 17px; line-height: 1.45; letter-spacing: -0.2px; }
  .modal-body [style*='grid-template-columns:1fr 1fr'],
  .modal-body [style*='grid-template-columns: 1fr 1fr'] {
    grid-template-columns: 1fr !important;
  }
  .modal-footer-btns { flex-direction: column-reverse; gap: 8px; }
  .modal-footer-btns > * { width: 100%; min-height: 46px; border-radius: 12px; }

  /* ---------- الرسوم ---------- */
  .chart-metrics-ribbon { flex-wrap: wrap; gap: 10px; }
  .chart-metrics-ribbon > * { flex: 1 1 45%; min-width: 0; }
  .card-title-bar { flex-wrap: wrap; gap: 8px; min-width: 0; }
  .chart-canvas-container { width: 100% !important; min-width: 0; }
  .chart-wrap, .chart-container { height: 230px !important; }
  canvas { max-width: 100% !important; }

  /* ---------- التنبيهات فوق الشريط السفلي ---------- */
  .toast-container {
    bottom: calc(var(--m-tabbar-h) + var(--m-safe-b) + 14px) !important;
    inset-inline: var(--m-gutter) !important;
    top: auto !important;
    align-items: stretch;
  }
  .toast { width: 100%; border-radius: 14px; font-size: 13.5px; }
}

/* شاشات ضيقة */
@media (max-width: 380px) {
  :root { --m-gutter: 13px; }
  .data-table td::before { flex-basis: 44%; font-size: 11px; }
  .m-tab-label { font-size: 9.5px; }
  .kpi-value-wrap strong { font-size: 19px; }
}
