  * { box-sizing: border-box; margin: 0; padding: 0; }
  :root {
    --brand-navy: #1a3558;
    --brand-navy-dark: #0f2340;
    --brand-blue: #1e56b8;
    --brand-blue-light: #3d7bdc;
    --primary: #1e56b8; --primary-dark: #164694;
    --success: #15803d; --danger: #b91c1c;
    --warning: #b45309; --info: #1e56b8;
    --bg: #f5f6f8; --card: #ffffff;
    --text: #111827; --text-muted: #6b7280;
    --border: #e5e7eb;
    --shadow: 0 1px 2px rgba(17,24,39,0.05);
    --shadow-lg: 0 4px 16px rgba(17,24,39,0.08);
    --radius: 8px;
    --radius-lg: 12px;
  }
  * { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }
  body { font-family: 'Tajawal', system-ui, sans-serif; background: var(--bg); color: var(--text); min-height: 100vh; line-height: 1.6; }
  .app { display: flex; min-height: 100vh; }
  .sidebar { width: 250px; background: #0f2340; color: #e5e7eb; padding: 20px 14px; position: sticky; top: 0; height: 100vh; overflow-y: auto; border-left: 1px solid rgba(255,255,255,0.06); }
  .logo { display: flex; flex-direction: column; align-items: center; gap: 4px; margin-bottom: 18px; padding: 14px 10px; background: white; border-radius: var(--radius); }
  .logo img { width: 100%; max-width: 180px; height: auto; display: block; }
  .logo small { color: #6b7280; font-size: 11px; font-weight: 500; }
  .nav-item { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 6px; cursor: pointer; margin-bottom: 2px; color: #9ca3af; transition: background .15s, color .15s; font-size: 14px; font-weight: 500; }
  .nav-item:hover { background: rgba(255,255,255,0.05); color: #e5e7eb; }
  .nav-item.active { background: #1e56b8; color: white; }
  .nav-item .icon { width: 18px; height: 18px; flex-shrink: 0; }
  .nav-item .icon svg { width: 100%; height: 100%; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
  .nav-section-title { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: #6b7280; padding: 14px 12px 6px; }
  .nav-footer { position: absolute; bottom: 16px; right: 14px; left: 14px; font-size: 11px; color: #6b7280; text-align: center; }
  main { flex: 1; padding: 24px 32px; overflow-x: hidden; background: var(--bg); }
  .page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; flex-wrap: wrap; gap: 10px; padding-bottom: 14px; border-bottom: 1px solid var(--border); }
  .page-header h1 { font-size: 20px; font-weight: 600; color: var(--text); letter-spacing: -0.01em; }
  .page-header p { color: var(--text-muted); font-size: 13px; margin-top: 2px; }
  .btn { padding: 8px 14px; border: 1px solid transparent; border-radius: 6px; cursor: pointer; font-weight: 500; font-family: inherit; font-size: 13px; transition: background .15s, border-color .15s; display: inline-flex; align-items: center; gap: 6px; line-height: 1.3; }
  .btn-primary { background: var(--primary); color: white; }
  .btn-primary:hover { background: var(--primary-dark); }
  .btn-secondary { background: white; color: var(--text); border-color: var(--border); }
  .btn-secondary:hover { background: #f9fafb; border-color: #d1d5db; }
  .btn-sm { padding: 5px 10px; font-size: 12px; }
  .btn-icon { padding: 5px 7px; background: transparent; border-radius: 5px; color: var(--text-muted); }
  .btn-icon:hover { background: var(--border); color: var(--text); }
  .stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; margin-bottom: 20px; }
  .stat-card { background: var(--card); border-radius: var(--radius); padding: 16px 18px; border: 1px solid var(--border); }
  .stat-card.accent { border-top: 2px solid var(--primary); }
  .stat-label { color: var(--text-muted); font-size: 12px; font-weight: 500; text-transform: uppercase; letter-spacing: 0.03em; margin-bottom: 6px; }
  .stat-value { font-size: 24px; font-weight: 600; color: var(--text); line-height: 1.2; letter-spacing: -0.02em; }
  .stat-sub { font-size: 11px; color: var(--text-muted); margin-top: 4px; }
  .card { background: var(--card); border-radius: var(--radius); padding: 18px 20px; border: 1px solid var(--border); margin-bottom: 14px; }
  .card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; padding-bottom: 10px; border-bottom: 1px solid var(--border); }
  .card-title { font-size: 14px; font-weight: 600; color: var(--text); }
  .toolbar { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
  .search-input, select, input, textarea { padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px; font-family: inherit; font-size: 13px; background: white; color: var(--text); outline: none; transition: border-color .15s, box-shadow .15s; }
  .search-input { flex: 1; min-width: 200px; }
  .search-input:focus, select:focus, input:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(30,86,184,0.12); }
  table { width: 100%; border-collapse: collapse; font-size: 13px; }
  th, td { padding: 10px 12px; text-align: right; border-bottom: 1px solid var(--border); vertical-align: middle; }
  th { font-weight: 600; color: var(--text-muted); background: #f9fafb; font-size: 11px; text-transform: uppercase; letter-spacing: 0.03em; border-bottom: 1px solid var(--border); }
  tr:last-child td { border-bottom: none; }
  tr:hover td { background: #f9fafb; }
  .empty { text-align: center; padding: 40px 20px; color: var(--text-muted); font-size: 13px; }
  .empty-title { font-weight: 500; color: #374151; margin-bottom: 4px; }
  .badge { padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: 500; display: inline-block; border: 1px solid transparent; }
  .badge-success { background: #f0fdf4; color: #166534; border-color: #bbf7d0; }
  .badge-warning { background: #fffbeb; color: #92400e; border-color: #fde68a; }
  .badge-danger  { background: #fef2f2; color: #991b1b; border-color: #fecaca; }
  .badge-info    { background: #eff6ff; color: #1e40af; border-color: #bfdbfe; }
  .badge-muted   { background: #f3f4f6; color: #4b5563; border-color: #e5e7eb; }
  .badge-pink    { background: #f1f5fc; color: #1a3558; border-color: #d6e2f5; }
  .badge-purple  { background: #eef2ff; color: #3730a3; border-color: #c7d2fe; }
  .modal-overlay { position: fixed; inset: 0; background: rgba(17,24,39,0.5); display: none; align-items: center; justify-content: center; z-index: 999; padding: 20px; }
  .modal-overlay.active { display: flex; }
  .modal { background: white; border-radius: var(--radius-lg); max-width: 520px; width: 100%; max-height: 90vh; overflow-y: auto; padding: 24px; box-shadow: var(--shadow-lg); }
  .modal h3 { margin-bottom: 16px; font-size: 16px; font-weight: 600; color: var(--text); }
  .form-grid { display: grid; gap: 14px; }
  .form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
  .form-group label { display: block; margin-bottom: 6px; font-size: 13px; color: #334155; font-weight: 500; }
  .form-group input, .form-group select, .form-group textarea { width: 100%; }
  .modal-actions { display: flex; gap: 10px; justify-content: flex-start; margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); }
  .chart-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 20px; margin-bottom: 20px; }
  @media (max-width: 900px) {
    .chart-grid { grid-template-columns: 1fr; }
    .sidebar { width: 72px; padding: 20px 10px; }
    .logo-text, .nav-item span:not(.icon) { display: none; }
    .nav-item { justify-content: center; }
    main { padding: 20px; }
    .form-row { grid-template-columns: 1fr; }
  }
  .page { display: none; }
  .page.active { display: block; }
  .avatar { width: 30px; height: 30px; border-radius: 50%; background: #1e56b8; color: white; display: inline-flex; align-items: center; justify-content: center; font-weight: 600; margin-left: 8px; vertical-align: middle; font-size: 12px; }
  .avatar.celebrity { background: #1a3558; }
  .stars { color: #f59e0b; letter-spacing: 2px; font-size: 16px; }
  .stars .empty-star { color: #e2e8f0; }
  .toast { position: fixed; bottom: 20px; left: 20px; background: #111827; color: white; padding: 10px 18px; border-radius: 6px; box-shadow: var(--shadow-lg); z-index: 1000; opacity: 0; transform: translateY(10px); transition: all .2s; pointer-events: none; font-size: 13px; }
  .toast.show { opacity: 1; transform: translateY(0); }
  .toast.success { background: #15803d; }
  .toast.error { background: #b91c1c; }
  .loading-overlay { position: fixed; inset: 0; background: rgba(255,255,255,0.9); z-index: 9998; display: flex; align-items: center; justify-content: center; flex-direction: column; gap: 12px; color: var(--text-muted); font-size: 14px; }
  .loading-overlay.hidden { display: none; }
  .spinner { width: 32px; height: 32px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin 0.8s linear infinite; }
  @keyframes spin { to { transform: rotate(360deg); } }
  .tier-chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 8px; background: #f1f5f9; font-size: 13px; margin: 4px 4px 4px 0; }
  .tier-chip button { background: none; border: none; color: var(--danger); cursor: pointer; font-size: 14px; padding: 0; }
  .tier-add-form { display: flex; gap: 6px; margin-top: 8px; }
  .tier-add-form input { flex: 1; }
  .role-banner { padding: 12px; background: rgba(255,255,255,0.06); border-radius: 10px; margin-bottom: 16px; }
  .role-banner label { color: #cbd5e1; display: block; margin-bottom: 6px; font-size: 12px; font-weight: 500; }
  .role-banner select { width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid #2a4768; background: #0f2340; color: white; font-family: inherit; font-size: 14px; cursor: pointer; }
  .role-banner select:focus { outline: none; border-color: #2d6fd4; }
  .role-hidden { display: none !important; }
  .attachment-link { display: inline-flex; align-items: center; gap: 4px; padding: 3px 8px; background: #dbeafe; color: #1e40af; border-radius: 6px; text-decoration: none; font-size: 12px; }
  .attachment-link:hover { background: #bfdbfe; }
  .file-input-wrap { display: flex; align-items: center; gap: 8px; }
  .file-input-wrap input[type="file"] { flex: 1; font-size: 13px; }

  /* شاشة تسجيل الدخول */
  .login-screen {
    position: fixed; inset: 0;
    background: linear-gradient(135deg, #1a3558 0%, #0f2340 50%, #1e56b8 100%);
    display: flex; align-items: center; justify-content: center;
    z-index: 9999; padding: 20px;
  }
  .login-screen::before {
    content: ''; position: absolute; inset: 0;
    background-image: radial-gradient(circle at 20% 30%, rgba(45,111,212,0.3), transparent 50%),
                      radial-gradient(circle at 80% 70%, rgba(30,86,184,0.25), transparent 50%);
    pointer-events: none;
  }
  .login-card {
    background: white; border-radius: 18px; padding: 36px 32px;
    width: 100%; max-width: 420px; box-shadow: 0 25px 50px rgba(0,0,0,0.25);
    position: relative; z-index: 2;
  }
  .login-logo { text-align: center; margin-bottom: 24px; }
  .login-logo img { max-width: 180px; height: auto; }
  .login-logo div { color: var(--text-muted); font-size: 13px; margin-top: 6px; font-weight: 500; }
  .login-card h2 { font-size: 22px; margin-bottom: 6px; text-align: center; color: var(--brand-navy); }
  .login-card .login-sub { text-align: center; color: var(--text-muted); margin-bottom: 22px; font-size: 14px; }
  .login-form { display: flex; flex-direction: column; gap: 14px; }
  .login-form label { font-size: 13px; font-weight: 500; color: #334155; margin-bottom: 4px; display: block; }
  .login-form input { width: 100%; padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; font-family: inherit; font-size: 14px; outline: none; transition: border-color .15s; }
  .login-form input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(30,86,184,0.15); }
  .login-btn {
    padding: 13px; background: linear-gradient(135deg, #1e56b8, #2d6fd4);
    color: white; border: none; border-radius: 10px; cursor: pointer;
    font-weight: 600; font-size: 15px; font-family: inherit; margin-top: 6px;
    transition: transform .1s, box-shadow .15s;
  }
  .login-btn:hover { box-shadow: 0 6px 18px rgba(30,86,184,0.4); }
  .login-btn:active { transform: scale(0.98); }
  .login-error { padding: 10px 14px; background: #fee2e2; color: #991b1b; border-radius: 8px; font-size: 13px; display: none; }
  .login-error.show { display: block; }
  .login-hints { margin-top: 18px; padding: 12px; background: #f8fafc; border-radius: 10px; font-size: 12px; color: var(--text-muted); }
  .login-hints strong { color: var(--brand-navy); display: block; margin-bottom: 6px; }
  .login-hints code { background: #e0ebf9; color: var(--brand-navy); padding: 1px 6px; border-radius: 4px; font-size: 11px; }
  .app.hidden { display: none; }

  /* بطاقة المستخدم الحالي في الشريط الجانبي */
  .user-card { padding: 10px; background: rgba(255,255,255,0.04); border: 1px solid rgba(255,255,255,0.06); border-radius: var(--radius); margin-bottom: 16px; display: flex; align-items: center; gap: 10px; }
  .user-card .user-avatar { width: 32px; height: 32px; border-radius: 50%; background: #1e56b8; color: white; display: flex; align-items: center; justify-content: center; font-weight: 600; font-size: 13px; flex-shrink: 0; }
  .user-card .user-info { flex: 1; min-width: 0; }
  .user-card .user-name { color: #e5e7eb; font-size: 12px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .user-card .user-role { color: #9ca3af; font-size: 11px; }
  .logout-btn { background: transparent; border: none; color: #9ca3af; cursor: pointer; padding: 6px; border-radius: 5px; transition: background .15s, color .15s; display: inline-flex; }
  .logout-btn:hover { background: rgba(185,28,28,0.15); color: #fca5a5; }

  /* رأس تقرير PDF */
  .pdf-header { display: none; padding: 20px 24px; background: linear-gradient(135deg, #1a3558, #1e56b8); color: white; border-radius: 12px 12px 0 0; margin-bottom: 20px; }
  .pdf-header .pdf-top { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-bottom: 10px; }
  .pdf-header h2 { font-size: 22px; font-weight: 700; }
  .pdf-header .pdf-meta { font-size: 13px; opacity: 0.9; }
  .pdf-header img { max-width: 140px; height: auto; background: white; padding: 6px 10px; border-radius: 8px; }
  .pdf-period { background: white; color: #0f2340; padding: 10px 16px; border-radius: 8px; font-size: 13px; display: inline-block; }
  .exporting .pdf-header { display: block; }
  .date-filter-card { padding: 20px; }
  .date-filter-row { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
  .date-filter-row .form-group { margin: 0; min-width: 160px; }
  .date-filter-row label { font-size: 13px; color: #334155; font-weight: 500; margin-bottom: 6px; display: block; }
  .quick-ranges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
  .quick-range { padding: 5px 12px; background: #e0ebf9; color: #1a3558; border: none; border-radius: 20px; font-size: 12px; cursor: pointer; font-family: inherit; }
  .quick-range:hover { background: #c7d9f0; }
  .quick-range.active { background: #1e56b8; color: white; }

/* ==================================================
   Mobile responsive overrides (added v2)
   ================================================== */

/* Topbar: hidden على الشاشات الكبيرة، يظهر على الهاتف */
.topbar {
  display: none;
  position: sticky; top: 0; z-index: 40;
  background: #0f2340; color: #e5e7eb;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  padding-right: calc(14px + env(safe-area-inset-right, 0px));
  padding-left: calc(14px + env(safe-area-inset-left, 0px));
  align-items: center; gap: 10px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  box-shadow: 0 1px 4px rgba(0,0,0,0.15);
}
.topbar-title { flex: 1; font-weight: 600; font-size: 15px; text-align: center; }
.hamburger, .topbar-logout {
  background: transparent; border: none; color: #e5e7eb;
  width: 40px; height: 40px; min-width: 40px; min-height: 40px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 8px; cursor: pointer; padding: 0;
  transition: background .15s;
}
.hamburger:hover, .topbar-logout:hover { background: rgba(255,255,255,0.08); }
.hamburger:active, .topbar-logout:active { background: rgba(255,255,255,0.14); }

.topbar-logout { color: #fca5a5; }

.sidebar-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(15,35,64,0.55);
  z-index: 49;
  opacity: 0; transition: opacity .2s;
  -webkit-tap-highlight-color: transparent;
}
.sidebar-backdrop.active { display: block; opacity: 1; }

.sidebar-close-mobile {
  display: none;
  background: transparent; border: none; color: #9ca3af;
  width: 32px; height: 32px; min-width: 32px; min-height: 32px;
  align-items: center; justify-content: center;
  border-radius: 6px; cursor: pointer; padding: 0;
  margin-right: auto;
}
.sidebar-close-mobile:hover { background: rgba(255,255,255,0.08); color: #e5e7eb; }

@media (max-width: 900px) {
  /* على الهاتف: التطبيق عمود (topbar فوق، main تحت)، القائمة الجانبية fixed */
  .app { display: flex; flex-direction: column; }
  main { width: 100%; padding: 16px; min-height: 0; }
  .topbar { display: flex; width: 100%; }
  .sidebar {
    position: fixed; top: 0; right: 0; bottom: 0;
    width: min(84vw, 300px); height: 100vh; height: 100dvh;
    padding: 18px 14px;
    padding-top: calc(18px + env(safe-area-inset-top, 0px));
    padding-bottom: calc(18px + env(safe-area-inset-bottom, 0px));
    padding-right: calc(14px + env(safe-area-inset-right, 0px));
    z-index: 50;
    overflow-y: auto;
    transform: translateX(100%);
    transition: transform .28s cubic-bezier(.25,.8,.25,1);
    box-shadow: -4px 0 20px rgba(0,0,0,0.25);
  }
  .sidebar.open { transform: translateX(0); }
  /* أعد إظهار النصوص في عناصر القائمة (كانت مخفية في الإصدار السابق) */
  .logo-text, .nav-item span:not(.icon) { display: inline; }
  .logo img { max-width: 150px; }
  .nav-item { justify-content: flex-start; padding: 12px 14px; font-size: 14px; min-height: 44px; }
  .nav-item .icon { width: 20px; height: 20px; }
  .nav-section-title { padding: 16px 12px 8px; }
  .sidebar-close-mobile { display: inline-flex; }
  .user-card .user-info { min-width: 0; }

  main { padding: 16px; }
  .page-header { flex-direction: column; align-items: stretch; gap: 12px; }
  .page-header > div:first-child { text-align: center; }
  .page-header h1 { font-size: 18px; }

  .stats-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat-card { padding: 12px 14px; }
  .stat-value { font-size: 20px; }
  .chart-grid { grid-template-columns: 1fr; gap: 14px; }

  /* تمرير أفقي آمن للجداول داخل البطاقات */
  .card:has(table) {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .card table { min-width: 640px; }
  th, td { padding: 8px 10px; font-size: 12px; }

  /* المودالات: تنبثق من الأسفل بشكل bottom-sheet */
  .modal-overlay { align-items: flex-end; padding: 0; }
  .modal {
    border-radius: 18px 18px 0 0;
    max-width: 100%;
    max-height: 92vh; max-height: 92dvh;
    padding: 20px 18px;
    padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  }
  .form-row { grid-template-columns: 1fr; }
  .modal-actions { flex-wrap: wrap; }
  .modal-actions .btn { flex: 1; min-height: 44px; justify-content: center; }

  /* أزرار مريحة للمس */
  .btn { min-height: 40px; }
  .btn-sm { min-height: 36px; padding: 8px 12px; font-size: 13px; }
  .btn-icon { min-width: 36px; min-height: 36px; padding: 8px; }

  /* منع تكبير iOS التلقائي */
  input, select, textarea, .search-input { font-size: 16px; }

  /* أشرطة البحث والفلاتر */
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar > .search-input,
  .toolbar > select { width: 100%; }

  /* إخفاء حواشي التذييل الطويلة */
  .nav-footer { position: static; text-align: center; margin-top: 20px; padding: 10px; }

  /* صفحة التقارير */
  .date-filter-row { flex-direction: column; align-items: stretch; }
  .date-filter-row .form-group { min-width: 0; width: 100%; }
  .quick-ranges { justify-content: center; }
  .quick-range { min-height: 36px; display: inline-flex; align-items: center; }
}

/* شاشات صغيرة جداً (هواتف ضيقة) */
@media (max-width: 420px) {
  .stats-grid { grid-template-columns: 1fr; }
  .sidebar { width: 90vw; }
  .topbar-title { font-size: 14px; }
  .login-card { padding: 26px 20px; }
}

/* منع اختفاء مؤشر التركيز على العناصر التفاعلية */
button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
.nav-item:focus-visible {
  outline: 2px solid #3d7bdc;
  outline-offset: 2px;
}

/* تقليل الحركة للمستخدمين الذين طلبوا ذلك */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

/* ==================================================
   Chart sizing — prevent Chart.js from growing too tall
   ================================================== */
.card canvas {
  width: 100% !important;
  height: 280px !important;
  max-height: 40vh !important;
  display: block;
}
@media (max-width: 900px) {
  .card canvas { height: 220px !important; max-height: 34vh !important; }
}
@media (max-width: 420px) {
  .card canvas { height: 190px !important; max-height: 32vh !important; }
}
