:root {
  --primary: #ff5a00;
  --primary-dark: #d94a00;
  --bg: #f5f6f8;
  --panel: #fff;
  --text: #17191c;
  --muted: #6b7280;
  --border: #e5e7eb;
  --ok: #16a34a;
  --bad: #dc2626;
  --radius: 14px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: 'Cairo', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  overflow-x: hidden;
}
img { max-width: 100%; }

h1, h2, h3 { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.center { text-align: center; }
.hint { background: #fff4ec; border: 1px solid #ffd9c2; border-radius: 10px; padding: 10px 12px; margin: 0 0 12px; font-size: 14px; }
.error { background: #fef2f2; border: 1px solid #fecaca; color: var(--bad); border-radius: 10px; padding: 10px 12px; margin: 14px 0 0; font-size: 14px; }

/* ═════ عناصر النماذج ═════ */
label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 14px; }
input, select, textarea {
  display: block; width: 100%; margin-top: 6px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 10px; background: #fff;
  font: inherit; color: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(255, 90, 0, .35); border-color: var(--primary); }
textarea { resize: vertical; }

.btn {
  border: 1px solid var(--border); background: #fff; color: var(--text);
  padding: 9px 16px; border-radius: 10px; font: inherit; font-weight: 600; cursor: pointer;
  transition: background .15s, opacity .15s;
}
.btn:hover { background: #f3f4f6; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

/* ═════ الدخول ═════ */
.login { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 20px; background: linear-gradient(160deg, #fff 0%, #fff1e8 100%); }
.login-card { width: 100%; max-width: 400px; background: #fff; border-radius: 20px; padding: 32px 28px; box-shadow: 0 20px 50px rgba(0, 0, 0, .08); }
.login-card h1 { font-size: 20px; text-align: center; margin-bottom: 22px; }
.login-logo { display: block; width: 88px; height: 88px; margin: 0 auto 12px; border-radius: 20px; }

/* ═════ التخطيط ═════ */
.app { display: grid; grid-template-columns: 240px minmax(0, 1fr); width: 100%; max-width: 100vw; min-height: 100vh; }
.sidebar { background: #fff; border-inline-end: 1px solid var(--border); padding: 18px 14px; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; height: 100dvh; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; padding: 4px 8px 18px; }
.brand img { width: 38px; height: 38px; border-radius: 10px; }
#nav { display: flex; flex-direction: column; gap: 4px; }
#nav a { color: var(--text); text-decoration: none; padding: 10px 12px; border-radius: 10px; font-weight: 600; }
#nav a:hover { background: #f3f4f6; }
#nav a.active { background: #fff1e8; color: var(--primary-dark); }
.sidebar-foot { margin-top: auto; display: grid; gap: 8px; }
.sidebar-foot .muted { font-size: 12px; overflow: hidden; text-overflow: ellipsis; text-align: center; }

.content { padding: 26px; min-width: 0; max-width: 1400px; width: 100%; margin-inline: auto; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.page-head h2 { font-size: 22px; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.badge { background: var(--primary); color: #fff; border-radius: 99px; padding: 1px 10px; font-size: 13px; vertical-align: middle; }

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.panel h3 { font-size: 15px; margin-bottom: 14px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.send-grid { align-items: start; }

/* ═════ البطاقات ═════ */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 14px; margin-bottom: 16px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; }
.card .label { color: var(--muted); font-size: 13px; }
.card .value { font-size: 30px; font-weight: 700; line-height: 1.3; }
.card .sub { color: var(--muted); font-size: 12px; }
.card.accent .value { color: var(--primary); }

/* ═════ الرسوم ═════ */
.bars { display: grid; gap: 10px; }
.bar-row { display: grid; grid-template-columns: minmax(70px, 38%) 1fr auto; gap: 10px; align-items: center; font-size: 13.5px; }
.bar-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { background: #f1f2f4; border-radius: 99px; height: 10px; overflow: hidden; }
.bar-fill { background: linear-gradient(90deg, #ff8a3d, var(--primary)); height: 100%; border-radius: 99px; }
.bar-row .num { font-weight: 700; min-width: 28px; text-align: end; }

.donut-wrap { display: flex; align-items: center; gap: 24px; flex-wrap: wrap; }
.donut { width: 140px; height: 140px; border-radius: 50%; position: relative; }
.donut::after { content: ''; position: absolute; inset: 28px; background: #fff; border-radius: 50%; }
.legend { display: grid; gap: 8px; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-inline-end: 8px; vertical-align: -1px; }

.columns { display: flex; align-items: flex-end; gap: 6px; height: 170px; direction: ltr; }
.col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; font-size: 11px; color: var(--muted); }
.col .stick { width: 100%; background: linear-gradient(180deg, #ff8a3d, var(--primary)); border-radius: 5px 5px 0 0; min-height: 2px; }
.col .n { color: var(--text); font-weight: 700; font-size: 11px; }

/* ═════ الجداول ═════ */
.toolbar { display: grid; grid-template-columns: 2fr 1fr 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.toolbar input, .toolbar select { margin-top: 0; }
.table-wrap { background: #fff; border: 1px solid var(--border); border-radius: var(--radius); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 11px 14px; text-align: start; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { background: #fafafa; color: var(--muted); font-weight: 600; font-size: 13px; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fffaf6; }
td .sub { display: block; color: var(--muted); font-size: 12px; }
.pill { display: inline-block; border-radius: 99px; padding: 1px 10px; font-size: 12.5px; font-weight: 600; }
.pill.ok { background: #dcfce7; color: #166534; }
.pill.off { background: #f3f4f6; color: #6b7280; }
.pill.android { background: #e8f7ec; color: #1a7f37; }
.pill.ios { background: #e8eefc; color: #1d4ed8; }
.pill.warn { background: #fef3c7; color: #92400e; }
.pill.bad { background: #fee2e2; color: #991b1b; }
tr.clickable { cursor: pointer; }
.meter { height: 8px; background: #f1f2f4; border-radius: 99px; overflow: hidden; margin: 6px 0 12px; }
.meter > i { display: block; height: 100%; background: linear-gradient(90deg, #ff8a3d, var(--primary)); border-radius: 99px; }
.kpi { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 8px; }
.kpi div { background: #f8fafc; border-radius: 12px; padding: 10px 12px; }
.kpi .k { color: var(--muted); font-size: 12px; }
.kpi .v { font-size: 22px; font-weight: 800; line-height: 1.3; }
.actions { display: flex; gap: 6px; justify-content: flex-end; }

.inline-form { display: grid; grid-template-columns: 1fr 1fr auto; gap: 10px; margin-bottom: 16px; align-items: center; }
.inline-form input { margin-top: 0; }

/* ═════ المعاينة ═════ */
.preview { background: #f1f2f4; border-radius: 16px; padding: 14px; }
.preview-top { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; margin-bottom: 8px; }
.preview-top img { width: 18px; height: 18px; border-radius: 5px; }
.pv-title { font-weight: 700; word-break: break-word; }
.pv-body { color: #374151; font-size: 14px; white-space: pre-wrap; word-break: break-word; }
.pv-image { width: 100%; max-height: 180px; object-fit: cover; border-radius: 10px; margin-top: 10px; }
.image-pick { display: grid; gap: 8px; margin-bottom: 14px; font-weight: 600; font-size: 14px; }
.image-pick-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.image-pick input[type=url] { margin-top: 0; font-weight: 500; }
.check-list { max-height: 280px; overflow: auto; border: 1px solid var(--border); border-radius: 12px; padding: 8px; background: #fff; }
.check-list label { display: flex; align-items: center; gap: 8px; font-weight: 500; margin: 0; padding: 8px; border-radius: 8px; }
.check-list label:hover { background: #fff7f2; }
.check-list input { width: auto; margin: 0; }

/* ═════ نافذة + إشعار ═════ */
dialog { border: 0; border-radius: 18px; padding: 0; width: min(560px, 94vw); box-shadow: 0 30px 80px rgba(0, 0, 0, .25); }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.dialog-foot { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; }
.details { margin: 0; padding: 8px 20px; max-height: 60vh; overflow-y: auto; }
.details div { display: grid; grid-template-columns: 140px 1fr; gap: 10px; padding: 8px 0; border-bottom: 1px dashed var(--border); }
.details dt { color: var(--muted); }
.details dd { margin: 0; word-break: break-all; }

.toast { position: fixed; bottom: 22px; inset-inline: 0; margin: auto; width: fit-content; max-width: 90vw; background: #111827; color: #fff; padding: 11px 20px; border-radius: 12px; z-index: 50; box-shadow: 0 10px 30px rgba(0, 0, 0, .25); }
.toast.bad { background: var(--bad); }

/* ═════ تجاوب الشاشات ═════ */

/* شاشات ضخمة */
@media (min-width: 1600px) {
  body { font-size: 16px; }
  .app { grid-template-columns: 270px minmax(0, 1fr); }
  .content { max-width: 1600px; padding: 34px; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
}

/* لابتوب صغير / تابلت أفقي */
@media (max-width: 1100px) {
  .app { grid-template-columns: 200px minmax(0, 1fr); }
  .content { padding: 20px; }
  .toolbar { grid-template-columns: 1.5fr 1fr 1fr 1fr; }
}

/* تابلت عمودي وما دون: القائمة تصبح شريطًا علويًا */
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; }
  .sidebar {
    min-width: 0; max-width: 100%;
    position: sticky; top: 0; z-index: 20; height: auto; overflow: visible;
    flex-direction: row; flex-wrap: wrap; align-items: center; justify-content: space-between;
    gap: 6px 10px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
    border-inline-end: 0; border-bottom: 1px solid var(--border);
  }
  .brand { padding: 0; font-size: 16px; }
  .brand img { width: 30px; height: 30px; }
  .sidebar-foot { margin: 0; grid-auto-flow: column; align-items: center; gap: 10px; }
  .sidebar-foot .muted { max-width: 34vw; white-space: nowrap; }
  .sidebar-foot .btn { width: auto; padding: 6px 12px; font-size: 13px; }
  #nav {
    flex-direction: row; flex: 1 1 100%; order: 3; gap: 6px; overflow-x: auto; min-width: 0; max-width: 100%;
    -webkit-overflow-scrolling: touch; scrollbar-width: none; padding-bottom: 2px;
  }
  #nav::-webkit-scrollbar { display: none; }
  #nav a { white-space: nowrap; padding: 8px 14px; background: #f3f4f6; flex: 0 0 auto; }
  #nav a.active { background: #fff1e8; }
  .content { padding: 16px; padding-inline: max(16px, env(safe-area-inset-left), env(safe-area-inset-right)); padding-bottom: max(24px, env(safe-area-inset-bottom)); }
  .grid-2 { grid-template-columns: 1fr; }
  .toolbar { grid-template-columns: 1fr 1fr; }
  .toolbar input { grid-column: 1 / -1; }
  .inline-form { grid-template-columns: 1fr; }
  .page-head h2 { font-size: 19px; }
}

/* جوال: الجداول تتحول إلى بطاقات */
@media (max-width: 720px) {
  /* 16px تمنع تكبير iOS التلقائي عند التركيز على الحقول */
  input, select, textarea { font-size: 16px; }
  .btn { min-height: 40px; }
  .btn.sm { min-height: 36px; padding: 6px 12px; }
  #nav a { padding: 9px 14px; }

  .table-wrap { background: transparent; border: 0; overflow: visible; }
  table.rtable, .rtable tbody, .rtable tr, .rtable td { display: block; width: 100%; }
  .rtable thead { display: none; }
  .rtable tr {
    background: #fff; border: 1px solid var(--border); border-radius: var(--radius);
    padding: 10px 14px; margin-bottom: 10px;
  }
  .rtable tbody tr:hover { background: #fff; }
  .rtable td {
    display: flex; justify-content: space-between; align-items: center; gap: 12px;
    padding: 6px 0; border-bottom: 1px dashed var(--border); white-space: normal; text-align: end !important;
  }
  .rtable td::before { content: attr(data-label); color: var(--muted); font-size: 13px; font-weight: 600; flex: 0 0 auto; text-align: start; }
  .rtable td.cell-main { display: block; text-align: start !important; font-size: 15.5px; padding-top: 2px; border-bottom: 1px solid var(--border); }
  .rtable td.cell-main::before { content: none; }
  .rtable td.cell-main .sub { margin-top: 2px; }
  .rtable td.cell-actions { display: block; border-bottom: 0; padding: 10px 0 2px; }
  .rtable td.cell-actions::before { content: none; }
  .rtable td.cell-actions .actions { justify-content: stretch; }
  .rtable td.cell-actions .actions .btn { flex: 1; }
  .rtable td:last-child { border-bottom: 0; }
  .rtable td .sub { display: block; }
  .rtable td[data-label] { overflow-wrap: anywhere; }

  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .card { padding: 12px 14px; }
  .card .value { font-size: 24px; }
  .panel { padding: 14px; }
  .donut-wrap { justify-content: center; }
  .bar-row { grid-template-columns: minmax(60px, 34%) 1fr auto; gap: 8px; font-size: 13px; }
  .columns { gap: 3px; height: 150px; }
  .details div { grid-template-columns: 1fr; gap: 0; }
  .details dt { font-size: 12.5px; }
  dialog { width: 96vw; max-height: 92dvh; border-radius: 16px; }
  .dialog-foot .btn { width: 100%; }
  .login-card { padding: 26px 20px; border-radius: 18px; }
  .toast { bottom: max(16px, env(safe-area-inset-bottom)); }
}

/* جوال صغير جدًا */
@media (max-width: 380px) {
  .cards { grid-template-columns: 1fr; }
  .kpi { grid-template-columns: 1fr; }
  .sidebar-foot .muted { display: none; }
  .page-head { flex-wrap: wrap; }
  .toolbar { grid-template-columns: 1fr; }
}

/* شاشة أفقية قصيرة (جوال بالعرض) */
@media (max-height: 480px) and (orientation: landscape) {
  .login { place-items: start center; }
  .sidebar { position: static; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
