:root {
  --brand: #4f46e5;
  --brand-dark: #3730a3;
  --bg: #f4f6fb;
  --card-bg: #ffffff;
  --text-main: #1e2130;
  --text-muted: #6b7280;
  --green: #16a34a;
  --yellow: #d97706;
  --red: #dc2626;
  --border: #e6e8f0;
  --sidebar-w: 260px;
}

* { box-sizing: border-box; }

body {
  font-family: "Tajawal", sans-serif;
  background: var(--bg);
  color: var(--text-main);
  margin: 0;
}

.auth-loader {
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

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

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  background: #fff;
  border-left: 1px solid var(--border);
  padding: 20px 16px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 900;
  font-size: 1.15rem;
  color: var(--brand);
  padding: 6px 8px 20px;
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg);
  border-radius: 12px;
  padding: 10px;
  margin-bottom: 18px;
}

.avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
}

.sb-user-name { font-weight: 700; font-size: .9rem; }
.sb-user-role { font-size: .75rem; color: var(--text-muted); }

.sidebar-nav { display: flex; flex-direction: column; gap: 4px; }

.nav-link {
  display: none; /* يُفعّل عبر JS حسب دور المستخدم */
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--text-main);
  text-decoration: none;
  font-size: .92rem;
  border: none;
  background: none;
  width: 100%;
  text-align: right;
  cursor: pointer;
}

.nav-link:hover { background: var(--bg); }
.nav-link.active { background: var(--brand); color: #fff; }
.nav-link.logout-btn { display: flex; color: var(--red); margin-top: 14px; }

/* ---------- Main content ---------- */
.main-content {
  flex: 1;
  padding: 26px 30px;
  max-width: calc(100% - var(--sidebar-w));
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 22px;
}

.page-header h1 { font-size: 1.4rem; font-weight: 900; margin: 0; }
.page-header .text-muted { font-size: .88rem; }

/* ---------- KPI cards ---------- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.kpi-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.kpi-card .kpi-label { font-size: .8rem; color: var(--text-muted); }
.kpi-card .kpi-value { font-size: 1.6rem; font-weight: 900; }
.kpi-card .kpi-icon {
  width: 38px; height: 38px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(79,70,229,.1); color: var(--brand); font-size: 1.1rem;
}

/* ---------- Charts / panels ---------- */
.panel {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 20px;
}
.panel h5 { font-weight: 800; margin-bottom: 16px; font-size: 1rem; }

/* ---------- Health badges ---------- */
.badge-health { padding: 5px 10px; border-radius: 20px; font-size: .75rem; font-weight: 700; }
.badge-health.green { background: rgba(22,163,74,.12); color: var(--green); }
.badge-health.yellow { background: rgba(217,119,6,.12); color: var(--yellow); }
.badge-health.red { background: rgba(220,38,38,.12); color: var(--red); }

/* ---------- Mobile menu toggle & backdrop ---------- */
.mobile-menu-btn {
  display: none;
  position: fixed;
  top: 14px;
  right: 14px;
  z-index: 1100;
  width: 42px;
  height: 42px;
  border-radius: 10px;
  border: none;
  background: var(--brand);
  color: #fff;
  font-size: 1.2rem;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(79,70,229,.3);
}
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 1040;
}
.sidebar-backdrop.show { display: block; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .mobile-menu-btn { display: flex; }
  .sidebar {
    position: fixed;
    top: 0;
    right: 0;
    width: min(280px, 85vw);
    z-index: 1050;
    transform: translateX(100%);
    transition: transform .25s ease;
    box-shadow: -4px 0 20px rgba(0,0,0,.08);
  }
  .sidebar.open { transform: translateX(0); }
  .main-content { max-width: 100%; padding: 18px; padding-top: 64px; }
  .app-wrapper { flex-direction: column; }
}

@media (max-width: 480px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .main-content { padding: 14px; padding-top: 60px; }
}

/* ---------- Single-screen Project Dashboard ---------- */
/* الصفحة بأكملها تظهر بدون تمرير على الشاشة، وتُقصّ لصفحة واحدة عند الطباعة */
body.dashboard-page {
  overflow: hidden;
  height: 100vh;
}
body.dashboard-page .app-wrapper {
  height: 100vh;
}
body.dashboard-page .sidebar {
  height: 100vh;
}
body.dashboard-page .main-content {
  height: 100vh;
  max-width: calc(100% - var(--sidebar-w));
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
/* البطاقة نفسها بنسبة أبعاد A4 أفقي (297:210) — تتوسط الشاشة دائماً */
body.dashboard-page .a4-page {
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 12px 32px rgba(0,0,0,.08), 0 0 0 1px var(--border);
  height: min(90vh, calc((100vw - var(--sidebar-w) - 40px) * 0.7071));
  aspect-ratio: 1.41421 / 1;
  max-width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 18px 26px;
  overflow: hidden;
}
body.dashboard-page .page-header {
  flex: 0 0 auto;
  margin-bottom: 0;
}
body.dashboard-page .page-header h1 {
  font-size: 1.15rem;
}
body.dashboard-page .panel {
  padding: 10px 16px;
  margin-bottom: 0;
}
body.dashboard-page .panel h5 {
  margin-bottom: 8px;
  font-size: .88rem;
}
body.dashboard-page .section-panel {
  flex: 0 0 auto;
  padding: 8px 16px;
}
body.dashboard-page .section-title {
  font-size: .72rem;
  font-weight: 800;
  color: var(--brand);
  text-transform: uppercase;
  letter-spacing: .3px;
  margin-bottom: 6px;
}
body.dashboard-page .kpi-grid {
  flex: 0 0 auto;
  margin-bottom: 0;
  gap: 8px;
  grid-template-columns: repeat(6, 1fr);
}
body.dashboard-page .kpi-grid.mini {
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}
body.dashboard-page #financialGrid {
  grid-template-columns: repeat(4, 1fr);
}
body.dashboard-page #projectInfoGrid {
  grid-template-columns: repeat(5, 1fr);
}
body.dashboard-page .kpi-card {
  padding: 8px 10px;
  gap: 1px;
}
body.dashboard-page .kpi-grid.mini .kpi-card {
  padding: 6px 8px;
}
body.dashboard-page .kpi-card .kpi-icon {
  width: 24px; height: 24px; font-size: .75rem;
}
body.dashboard-page .kpi-card .kpi-label {
  font-size: .64rem;
}
body.dashboard-page .kpi-card .kpi-value {
  font-size: .95rem;
}
body.dashboard-page .charts-row {
  flex: 1 1 auto;
  max-height: 34vh;
  min-height: 200px;
  margin-top: auto;
  margin-bottom: auto;
  display: flex;
  gap: 16px;
  width: 100%;
}
body.dashboard-page .charts-row > .panel {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}
body.dashboard-page .chart-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
}
body.dashboard-page .chart-wrap canvas {
  position: absolute;
  inset: 0;
  width: 100% !important;
  height: 100% !important;
}

/* على التابلت والجوال: نتخلى عن تصميم "صفحة واحدة بدون تمرير" لصالح
   تمرير عادي — لأن ضغط كل المحتوى (5 بطاقات × 3 أقسام + رسمين) في شاشة
   صغيرة يسبب اختفاء عناصر (خصوصاً الرسوم البيانية) بسبب overflow:hidden */
@media (max-width: 900px) {
  body.dashboard-page { overflow-y: auto; height: auto; min-height: 100vh; }
  body.dashboard-page .main-content {
    height: auto;
    min-height: 100vh;
    max-width: 100%;
    overflow: visible;
    display: block;
    padding: 12px;
  }
  body.dashboard-page .a4-page {
    height: auto;
    aspect-ratio: auto;
    box-shadow: none;
    border-radius: 0;
    max-height: none;
    overflow: visible;
    padding: 12px;
    padding-top: 56px;
  }
  body.dashboard-page .kpi-grid { grid-template-columns: repeat(3, 1fr); }
  body.dashboard-page .kpi-grid.mini { grid-template-columns: repeat(3, 1fr); }
  body.dashboard-page #financialGrid,
  body.dashboard-page #projectInfoGrid { grid-template-columns: repeat(3, 1fr); }
  body.dashboard-page .charts-row {
    flex-direction: column;
    max-height: none;
    height: auto;
    margin-top: 0;
    margin-bottom: 0;
  }
  body.dashboard-page .charts-row > .panel {
    flex: none;
    display: block;
  }
  body.dashboard-page .chart-wrap {
    position: static;
    flex: none;
    min-height: 0;
    height: 280px;
  }
  body.dashboard-page .chart-wrap canvas {
    position: static !important;
    inset: auto !important;
  }
}

@media (max-width: 480px) {
  body.dashboard-page .kpi-grid,
  body.dashboard-page .kpi-grid.mini,
  body.dashboard-page #financialGrid,
  body.dashboard-page #projectInfoGrid { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- Print: صفحة واحدة أفقية ---------- */
@media print {
  @page {
    size: A4 landscape;
    margin: 8mm;
  }
  .sidebar, #printBtn, #editBtn, .auth-loader {
    display: none !important;
  }
  body.dashboard-page {
    overflow: visible;
    height: auto;
  }
  body.dashboard-page .app-wrapper {
    height: auto;
    display: block;
  }
  body.dashboard-page .main-content {
    height: 100vh;
    max-width: 100%;
    padding: 0;
    display: block;
  }
  body.dashboard-page .a4-page {
    height: auto;
    aspect-ratio: auto;
    max-width: 100%;
    max-height: none;
    box-shadow: none;
    border-radius: 0;
    padding: 0;
  }
  body.dashboard-page .charts-row {
    page-break-inside: avoid;
  }
  body.dashboard-page .kpi-grid {
    grid-template-columns: repeat(6, 1fr);
  }
  .panel {
    break-inside: avoid;
    border: 1px solid #ddd;
  }
}

/* ---------- Login page ---------- */
.login-wrapper {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--brand) 0%, var(--brand-dark) 100%);
}
.login-card {
  background: #fff;
  border-radius: 20px;
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 50px rgba(0,0,0,.2);
}
.login-card .brand { text-align: center; margin-bottom: 24px; font-weight: 900; color: var(--brand); font-size: 1.3rem; }
