* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --green: #25d366;
  --green-dark: #128c7e;
  --bg: #f0f4f3;
  --card: #ffffff;
  --text: #1f2d2a;
  --muted: #6b7c78;
  --border: #dde5e3;
  --red: #e0483e;
  --amber: #d78a00;
}

body {
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ---- تسجيل الدخول ---- */
.login-screen {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(160deg, var(--green-dark), #075e54);
}
.login-box {
  background: var(--card); border-radius: 16px; padding: 40px;
  width: 360px; max-width: 92vw; text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.login-box .logo { font-size: 52px; margin-bottom: 8px; }
.login-box h1 { font-size: 22px; margin-bottom: 4px; }
.login-box form { margin-top: 24px; display: flex; flex-direction: column; gap: 12px; }

/* ---- الهيدر ---- */
header {
  background: var(--green-dark); color: #fff;
  padding: 12px 24px;
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 12px;
}
.header-title { display: flex; align-items: center; gap: 10px; }
.header-title h1 { font-size: 19px; font-weight: 600; }
.logo-sm { font-size: 26px; }
nav { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.tab-btn, .tab-link {
  background: transparent; border: none; color: #d7f5e3;
  padding: 8px 14px; border-radius: 8px; cursor: pointer;
  font-size: 14px; font-family: inherit; text-decoration: none;
}
.tab-btn:hover, .tab-link:hover { background: rgba(255,255,255,.12); }
.tab-btn.active { background: #fff; color: var(--green-dark); font-weight: 600; }

/* ---- المحتوى ---- */
main { max-width: 1100px; margin: 0 auto; padding: 28px 20px 60px; }
.tab { display: none; }
.tab.active { display: block; }
.section-head {
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 12px; margin-bottom: 20px;
}
.section-head h2 { font-size: 20px; }

/* ---- عناصر عامة ---- */
input, select, textarea {
  font-family: inherit; font-size: 14px;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: #fff; color: var(--text); width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--green); box-shadow: 0 0 0 3px rgba(37,211,102,.15);
}
.btn {
  font-family: inherit; font-size: 14px; cursor: pointer;
  padding: 10px 18px; border-radius: 8px;
  border: 1px solid var(--border); background: #fff; color: var(--text);
  white-space: nowrap;
}
.btn:hover { background: #f5f8f7; }
.btn-primary { background: var(--green); border-color: var(--green); color: #fff; font-weight: 600; }
.btn-primary:hover { background: #1fb958; }
.btn-danger { background: #fff; color: var(--red); border-color: #f2c4c1; }
.btn-danger:hover { background: #fdf0ef; }
.btn-ghost { background: transparent; border-color: rgba(255,255,255,.4); color: #fff; }
.btn-ghost:hover { background: rgba(255,255,255,.12); }
.btn-block { width: 100%; }
.btn-sm { padding: 5px 12px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: wait; }

.inline-form { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.inline-form input, .inline-form select { width: auto; min-width: 220px; }

.panel {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 12px; padding: 18px; margin-bottom: 18px;
}

.error-text { color: var(--red); font-size: 13px; }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; line-height: 1.9; }

/* ---- بطاقات الجلسات ---- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 18px; }
.session-card {
  background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 20px; display: flex; flex-direction: column; gap: 12px;
}
.session-card .top { display: flex; justify-content: space-between; align-items: start; }
.session-card h3 { font-size: 17px; }
.session-card .sid { font-size: 12px; color: var(--muted); direction: ltr; }
.badge {
  display: inline-block; padding: 4px 12px; border-radius: 20px;
  font-size: 13px; font-weight: 600;
}
.badge.connected { background: #e2f8eb; color: #128c4b; }
.badge.qr { background: #fff4dd; color: var(--amber); }
.badge.other { background: #f1f1f1; color: var(--muted); }
.qr-box { text-align: center; padding: 8px; }
.qr-box img { width: 230px; height: 230px; border-radius: 8px; border: 1px solid var(--border); }
.qr-box p { font-size: 13px; color: var(--muted); margin-top: 8px; line-height: 1.8; }
.session-phone { font-size: 14px; direction: ltr; text-align: right; }
.card-actions { display: flex; gap: 8px; margin-top: auto; }

/* ---- الجداول ---- */
.table {
  width: 100%; border-collapse: collapse; background: var(--card);
  border: 1px solid var(--border); border-radius: 12px; overflow: hidden;
  font-size: 14px;
}
.table th, .table td { padding: 11px 14px; text-align: right; border-bottom: 1px solid var(--border); }
.table th { background: #f7faf9; font-weight: 600; color: var(--muted); font-size: 13px; }
.table tr:last-child td { border-bottom: none; }
.table code { font-size: 12px; direction: ltr; display: inline-block; }
.status-sent { color: #128c4b; font-weight: 600; }
.status-failed { color: var(--red); font-weight: 600; }
td.ltr { direction: ltr; text-align: right; }

.key-result { background: #e9fbf1; border-color: #b8ecce; }
.key-row { display: flex; gap: 10px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.key-row code {
  background: #fff; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--border);
  direction: ltr; word-break: break-all; flex: 1; min-width: 200px;
}

/* ---- نموذج الإرسال ---- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; max-width: 720px; }
.form-grid label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; }
.form-grid .span-2 { grid-column: span 2; }
.result-text { margin-inline-start: 12px; font-size: 14px; }
.result-text.ok { color: #128c4b; }
.result-text.err { color: var(--red); }

/* ---- بوابة المشترك ---- */
.auth-box { text-align: right; }
.auth-box .logo, .auth-box h1, .auth-box > .muted { text-align: center; }
.auth-tabs {
  display: flex; gap: 6px; margin: 22px 0 16px;
  background: #eef3f1; border-radius: 10px; padding: 4px;
}
.auth-tab {
  flex: 1; border: none; background: transparent; padding: 9px;
  border-radius: 8px; cursor: pointer; font-family: inherit; font-size: 14px; color: var(--muted);
}
.auth-tab.active { background: #fff; color: var(--green-dark); font-weight: 700; box-shadow: 0 1px 4px rgba(0,0,0,.08); }

.portal { max-width: 760px; }
.step-panel { margin-bottom: 20px; }
.step-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.step-head h2 { font-size: 17px; flex: 1; }
.step-num {
  width: 30px; height: 30px; border-radius: 50%; background: var(--green);
  color: #fff; font-weight: 700; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.ok-line { margin-bottom: 12px; font-size: 15px; }
.link-code {
  background: #f2f7f5; padding: 10px 14px; border-radius: 8px; border: 1px solid var(--border);
  direction: ltr; word-break: break-all; flex: 1; min-width: 200px; font-size: 13px;
}
.link-help { margin: 14px 0; }
.link-help summary { cursor: pointer; color: var(--green-dark); font-weight: 600; font-size: 14px; }
.link-help pre {
  background: #1f2d2a; color: #d7f5e3; padding: 14px; border-radius: 10px;
  overflow-x: auto; direction: ltr; text-align: left; font-size: 12.5px; line-height: 1.7;
  margin: 10px 0; font-family: Consolas, monospace;
}
.danger-note { font-size: 13px; margin-top: 10px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

/* ---- عناصر جديدة ---- */
.pending-note { margin: 16px 0; line-height: 2; color: var(--text); font-size: 15px; }
.usage-line {
  display: flex; gap: 20px; flex-wrap: wrap; margin-top: 14px;
  background: #f2f7f5; border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 16px; font-size: 14px;
}
.usage-line b { color: var(--green-dark); }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: 12px; }
.table-wrap .table { min-width: 640px; }
.row-pending td { background: #fffbf0; }
.actions-cell { white-space: nowrap; }
.actions-cell .btn { margin-bottom: 4px; }

/* ---- شريط البحث والفلترة ---- */
.filter-bar {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  background: var(--card); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px; margin-bottom: 16px;
}
.filter-bar input[type="search"] { flex: 1; min-width: 220px; }
.filter-bar select { width: auto; min-width: 150px; }
.filter-bar .muted { font-size: 13px; margin-inline-start: auto; white-space: nowrap; }

/* ---- نافذة التعديل ---- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 32, 28, .55);
  display: flex; align-items: center; justify-content: center;
  padding: 20px; z-index: 50; overflow-y: auto;
}
.modal {
  background: var(--card); border-radius: 16px; padding: 22px;
  width: 100%; max-width: 640px; max-height: 90vh; overflow-y: auto;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .3);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 18px; padding-bottom: 12px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { font-size: 18px; }
.modal-toggles { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; }
.modal-link-row {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  background: #f7faf9; border: 1px solid var(--border); border-radius: 10px; padding: 10px;
}
.modal-link-row .link-code { font-size: 11.5px; }
.modal-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding-top: 6px; }
.modal label small { font-weight: 400; }

.panel-title { font-size: 16px; margin-bottom: 6px; }
.small { font-size: 13px; margin-bottom: 12px; }
.checkbox-row { flex-direction: row !important; align-items: center; gap: 8px; font-weight: 400 !important; }
.checkbox-row input { width: auto; }

/* ---- بطاقات الباقات ---- */
.pkg-title { font-weight: 700; font-size: 14px; margin: 4px 0 2px; text-align: right; }
.pkg-cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.pkg-card {
  border: 2px solid var(--border); border-radius: 12px; padding: 14px 8px;
  text-align: center; cursor: pointer; transition: border-color .15s, background .15s;
  background: #fff; user-select: none;
}
.pkg-card:hover { border-color: #9fdcb8; }
.pkg-card.selected {
  border-color: var(--green); background: #e9fbf1;
  box-shadow: 0 0 0 3px rgba(37, 211, 102, .15);
}
.pkg-icon { font-size: 26px; }
.pkg-name { font-weight: 700; margin: 4px 0; }
.pkg-price { color: var(--green-dark); font-weight: 700; font-size: 14px; }
.pkg-price small { font-weight: 400; color: var(--muted); }
.pkg-limit { font-size: 12.5px; margin-top: 3px; }
.pkg-chip {
  display: inline-block; background: #e9fbf1; border: 1px solid #b8ecce;
  border-radius: 20px; padding: 6px 16px; font-size: 14px; font-weight: 600; margin-top: 10px;
}

/* ================= التجاوب مع الهاتف ================= */

@media (max-width: 900px) {
  main { padding: 20px 14px 50px; }
  .cards { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  /* الهيدر: عنوان في سطر وأزرار تنزلق أفقياً */
  header { padding: 10px 12px; flex-direction: column; align-items: stretch; gap: 8px; }
  .header-title { justify-content: center; }
  .header-title h1 { font-size: 17px; }
  nav {
    justify-content: flex-start; flex-wrap: nowrap; overflow-x: auto;
    -webkit-overflow-scrolling: touch; padding-bottom: 4px; gap: 4px;
  }
  nav::-webkit-scrollbar { display: none; }
  .tab-btn, .tab-link { padding: 8px 12px; font-size: 13px; flex-shrink: 0; }

  main { padding: 16px 10px 40px; }
  .section-head { flex-direction: column; align-items: stretch; }
  .section-head h2 { font-size: 18px; }

  /* النماذج تتمدد بعرض الشاشة */
  .inline-form { flex-direction: column; align-items: stretch; }
  .inline-form input, .inline-form select { width: 100%; min-width: 0; }
  .inline-form .btn { width: 100%; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: span 1; }

  /* لمسات أكبر للأصابع */
  input, select, textarea { padding: 12px; font-size: 16px; } /* 16px يمنع زوم iOS التلقائي */
  .btn { padding: 12px 16px; }
  .btn-sm { padding: 8px 12px; font-size: 13px; }

  .panel { padding: 14px; }
  .step-head h2 { font-size: 15.5px; }
  .qr-box img { width: min(230px, 70vw); height: auto; }
  .login-box { padding: 28px 20px; }
  .key-row { flex-direction: column; align-items: stretch; }
  .key-row .btn { width: 100%; }
  .link-code { font-size: 11.5px; }
  .usage-line { flex-direction: column; gap: 6px; }
  .danger-note { flex-direction: column; align-items: stretch; text-align: center; }

  .table { font-size: 12.5px; }
  .table th, .table td { padding: 8px 10px; }
  .table-wrap .table { min-width: 560px; }

  .result-text { display: block; margin: 10px 0 0; }
  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar input[type="search"], .filter-bar select { width: 100%; min-width: 0; }
  .filter-bar .muted { margin: 0; text-align: center; }
  .modal-backdrop { padding: 0; align-items: flex-start; }
  .modal { max-width: 100%; border-radius: 0; min-height: 100vh; max-height: none; }
  .modal-toggles { flex-direction: column; align-items: flex-start; gap: 10px; }
  .modal-actions .btn { flex: 1; }
  .pkg-cards { grid-template-columns: 1fr; }
  .pkg-card { display: flex; align-items: center; gap: 10px; text-align: right; padding: 10px 14px; }
  .pkg-card .pkg-icon { font-size: 22px; }
  .pkg-card .pkg-name { margin: 0; min-width: 70px; }
  .pkg-card .pkg-price { margin-inline-start: auto; }
}
