:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #11161d;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #1f8a5b;
  --accent-text: #ffffff;
  --danger: #c0392b;
  --warn: #b7791f;
  --ok: #1f8a5b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .06);
  --font: "SF Arabic", -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Kufi Arabic", Tahoma, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1218;
    --surface: #171b22;
    --surface-2: #1f242d;
    --text: #e9edf2;
    --muted: #98a2b3;
    --border: #2a303a;
    --accent: #2eab72;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }

/* لازم تسبق أي قاعدة display حتى تحترم خاصية hidden */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.6;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
}

h1 { font-size: 1.35rem; margin: 0 0 .25rem; }
h2 { font-size: 1.05rem; margin: 1.25rem 0 .5rem; }
h3 { font-size: .95rem; margin: 0 0 .25rem; }
p { margin: .25rem 0 .75rem; }

.muted { color: var(--muted); }
.small, small { font-size: .82rem; }

/* ------------------------------------------------ بوابات الدخول والإعداد */

.gate {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

.gate-card { width: 100%; max-width: 420px; }

/* ------------------------------------------------ الهيكل */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }

.dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--muted); flex: none;
}
.dot.ok { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 22%, transparent); }
.dot.bad { background: var(--danger); box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 22%, transparent); }

.chip {
  font-size: .72rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
  white-space: nowrap;
}

.tabs {
  position: sticky;
  top: 57px;
  z-index: 19;
  display: flex;
  gap: 4px;
  padding: 8px 12px;
  overflow-x: auto;
  scrollbar-width: none;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
  flex: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: .92rem;
  padding: 8px 14px;
  min-height: 40px;
  border-radius: 999px;
  cursor: pointer;
}
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }

main { max-width: 760px; margin: 0 auto; padding: 4px 16px 48px; }

.panel { display: none; }
.panel.active { display: block; }

.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; }
.stack { display: grid; gap: 12px; }

/* ------------------------------------------------ البطاقات */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  display: grid;
  gap: 12px;
}

.session-card { display: grid; gap: 10px; }

.session-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.session-name { font-weight: 700; font-size: 1.05rem; }
.session-meta { font-size: .82rem; color: var(--muted); direction: ltr; text-align: right; word-break: break-all; }

.badge {
  font-size: .75rem; font-weight: 600;
  padding: 3px 10px; border-radius: 999px;
  border: 1px solid transparent; white-space: nowrap;
}
.badge.ok    { background: color-mix(in srgb, var(--ok) 14%, transparent);     color: var(--ok);     border-color: color-mix(in srgb, var(--ok) 32%, transparent); }
.badge.warn  { background: color-mix(in srgb, var(--warn) 14%, transparent);   color: var(--warn);   border-color: color-mix(in srgb, var(--warn) 32%, transparent); }
.badge.bad   { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 32%, transparent); }
.badge.idle  { background: var(--surface-2); color: var(--muted); border-color: var(--border); }

.card-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* ------------------------------------------------ الأزرار والحقول */

.btn {
  font: inherit;
  font-size: .92rem;
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  transition: transform .06s ease, opacity .15s ease;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: progress; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, transparent); background: transparent; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: .85rem; border-radius: 9px; }

label { display: grid; gap: 6px; font-size: .88rem; font-weight: 600; }

input, select, textarea {
  font: inherit;
  font-size: 16px; /* يمنع تكبير الصفحة تلقائياً في iOS */
  font-weight: 400;
  padding: 11px 13px;
  min-height: 44px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent);
  outline-offset: 1px;
  border-color: var(--accent);
}
textarea { min-height: 84px; resize: vertical; }

/* ------------------------------------------------ الحالة والسجلات */

.grid-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
}
.stat b { display: block; font-size: 1.1rem; direction: ltr; }
.stat span { font-size: .78rem; color: var(--muted); }

.logs {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  font-size: .8rem;
}
.log-line {
  display: grid;
  grid-template-columns: 70px 1fr;
  gap: 10px;
  padding: 7px 12px;
  border-bottom: 1px solid var(--border);
}
.log-line:last-child { border-bottom: 0; }
.log-line time { color: var(--muted); direction: ltr; font-variant-numeric: tabular-nums; }
.log-line .msg { direction: ltr; text-align: left; word-break: break-word; }
.log-line .who { color: var(--accent); font-weight: 600; }

/* ------------------------------------------------ النوافذ والتنبيهات */

.modal {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  padding: 16px;
  background: rgba(8, 11, 16, .6);
  backdrop-filter: blur(3px);
}
.modal-card {
  width: 100%; max-width: 400px;
  background: var(--surface);
  border-radius: 18px;
  padding: 18px;
  display: grid; gap: 10px;
  box-shadow: var(--shadow);
}
.qr-box {
  display: grid; place-items: center;
  background: #fff;
  border-radius: 12px;
  padding: 12px;
  min-height: 240px;
}
.qr-box img { width: 100%; max-width: 260px; display: block; }

.toast {
  position: fixed;
  inset-inline: 16px;
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 100;
  margin-inline: auto;
  max-width: 420px;
  padding: 12px 16px;
  border-radius: 12px;
  background: #11161d;
  color: #fff;
  text-align: center;
  font-size: .9rem;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .2s ease, transform .2s ease;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.err { background: var(--danger); }
.toast.ok { background: var(--accent); }

.skeleton {
  height: 72px;
  border-radius: var(--radius);
  background: linear-gradient(90deg, var(--surface) 25%, var(--surface-2) 50%, var(--surface) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s infinite;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

.empty { text-align: center; color: var(--muted); padding: 28px 16px; }

.key-row { display: grid; gap: 6px; }
.key-value {
  direction: ltr; text-align: left;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .8rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 10px;
  word-break: break-all;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
