/* ============================================================
 *  الرَّقيم — Al-Raqim  |  Core Stylesheet
 *  الطقسُ البصري: ليلٌ قديم، حبرٌ على ورقٍ مُعتَّق، ومضاتٌ قرمزية.
 * ========================================================= */

@import url('https://fonts.googleapis.com/css2?family=Aref+Ruqaa:wght@400;700&family=Amiri:ital,wght@0,400;0,700;1,400&family=Reem+Kufi:wght@400;600&display=swap');

:root {
  /* ألوان — الوضع الميستري (الافتراضي) */
  --bg-deep:      #0a0a0f;
  --bg-paper:     #14131a;
  --bg-soft:      #1c1b23;
  --ink:          #e8e3d6;
  --ink-muted:    rgba(232, 227, 214, 0.55);
  --ink-faint:    rgba(232, 227, 214, 0.22);
  --accent:       #9a1728;
  --accent-warm:  #c7352b;
  --accent-dim:   rgba(154, 23, 40, 0.35);
  --gold:         #c9a96e;
  --gold-dim:     rgba(201, 169, 110, 0.35);

  /* خطوط */
  --font-display: 'Aref Ruqaa', 'Amiri', serif;
  --font-body:    'Amiri', 'Reem Kufi', serif;
  --font-ui:      'Reem Kufi', sans-serif;

  /* حركات */
  --ease-slow:    cubic-bezier(0.19, 1, 0.22, 1);
  --ease-ink:     cubic-bezier(0.65, 0.05, 0.36, 1);

  /* معمار */
  --radius:       4px;
  --max-read:     640px;
}

[data-theme="dark"] {
  --bg-deep:  #0c0c0c;
  --bg-paper: #18181a;
  --bg-soft:  #222225;
  --ink:      #ececec;
  --accent:   #8a8280;
  --accent-warm: #a89f9b;
  --gold:     #9f9488;
}

[data-theme="light"] {
  --bg-deep:  #efe9d9;
  --bg-paper: #f4efe1;
  --bg-soft:  #e7dfc9;
  --ink:      #1a1814;
  --ink-muted:    rgba(26, 24, 20, 0.60);
  --ink-faint:    rgba(26, 24, 20, 0.25);
  --accent:   #7a0e1c;
  --accent-warm: #9a1728;
  --gold:     #8a6d3e;
}

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

html, body {
  height: 100%;
  direction: rtl;
  font-family: var(--font-body);
  font-weight: 400;
  color: var(--ink);
  background: var(--bg-deep);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ═════════════════════════════════════════════════════════
 *  الطبقات: خلفية ورقية، حبيبات، دخان، ثم الوجه.
 * ═════════════════════════════════════════════════════════ */

.stage {
  position: fixed; inset: 0;
  overflow: hidden;
  z-index: 0;
  background:
    radial-gradient(ellipse at 50% 30%, rgba(154, 23, 40, 0.04), transparent 55%),
    radial-gradient(ellipse at 20% 80%, rgba(201, 169, 110, 0.025), transparent 60%),
    linear-gradient(180deg, var(--bg-deep), var(--bg-paper) 60%, var(--bg-deep));
}

/* طبقة الحبيبات — SVG noise ثابتة، خفيفة على الأداء */
.stage::before {
  content: "";
  position: absolute; inset: -50%;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' seed='7'/><feColorMatrix values='0 0 0 0 0.9  0 0 0 0 0.85  0 0 0 0 0.75  0 0 0 0.25 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  opacity: 0.12;
  mix-blend-mode: overlay;
  pointer-events: none;
}

/* طبقة الفينييت */
.stage::after {
  content: "";
  position: absolute; inset: 0;
  background: radial-gradient(ellipse at center, transparent 40%, rgba(0,0,0,0.55) 100%);
  pointer-events: none;
}

/* ═════════════════════════════════════════════════════════
 *  الوجه في الخلفية
 *  صورة بورتريه مُفلترة، تنبضُ أحيانًا كأنها تحاولُ الخروج.
 * ═════════════════════════════════════════════════════════ */

.face-layer {
  position: fixed; inset: 0;
  z-index: 1;
  pointer-events: none;
  opacity: 0;
  transition: opacity 2.4s var(--ease-slow);
}
.face-layer.ready { opacity: 1; }

.face-portrait {
  position: absolute;
  width: 70vmin; height: 90vmin;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%) scale(1);
  filter: grayscale(0.6) contrast(1) brightness(0.65);
  opacity: 0.28;
  mix-blend-mode: screen;
  transition: opacity 2s var(--ease-slow), filter 2s var(--ease-slow);
  will-change: opacity, filter;
  pointer-events: none;
}

/* يستجيبُ للثيم: أخفُّ في الفاتح */
[data-theme="light"] .face-portrait {
  mix-blend-mode: multiply;
  opacity: 0.18;
  filter: grayscale(0.8) contrast(0.9) brightness(0.9);
}

.face-portrait.surge {
  opacity: 0.55;
  filter: grayscale(0.3) contrast(1.15) brightness(0.85);
}

/* ═════════════════════════════════════════════════════════
 *  الإطار الزخرفي — كأنه صفحة من مخطوطة
 * ═════════════════════════════════════════════════════════ */

.frame {
  position: fixed;
  inset: 24px;
  z-index: 2;
  pointer-events: none;
  border: 1px solid var(--ink-faint);
  border-radius: var(--radius);
}
.frame::before, .frame::after {
  content: "";
  position: absolute;
  width: 24px; height: 24px;
  border: 1px solid var(--gold-dim);
}
.frame::before { top: -6px; right: -6px; border-left: 0; border-bottom: 0; }
.frame::after  { bottom: -6px; left: -6px; border-right: 0; border-top: 0; }

.frame-seal {
  position: fixed;
  top: 12px; left: 50%;
  transform: translateX(-50%);
  z-index: 3;
  font-family: var(--font-display);
  font-size: 14px;
  color: var(--gold);
  letter-spacing: 0.3em;
  opacity: 0.65;
  pointer-events: none;
}

/* ═════════════════════════════════════════════════════════
 *  المسرح — حيث تتناثر الكلمات
 * ═════════════════════════════════════════════════════════ */

.ink-stage {
  position: fixed; inset: 0;
  z-index: 4;
  pointer-events: none;
  overflow: hidden;
}

.ink-word {
  position: absolute;
  font-family: var(--font-display);
  color: var(--ink);
  text-shadow: 0 0 24px rgba(232, 227, 214, 0.25);
  white-space: nowrap;
  opacity: 0;
  will-change: transform, opacity, filter;
  pointer-events: none;
}
.ink-word.red {
  color: var(--accent-warm);
  text-shadow: 0 0 24px rgba(154, 23, 40, 0.5);
}
.ink-word.gold {
  color: var(--gold);
  text-shadow: 0 0 18px rgba(201, 169, 110, 0.4);
}

/* ═════════════════════════════════════════════════════════
 *  الرسالة المجتمعة — في المنتصف
 * ═════════════════════════════════════════════════════════ */

.scroll-box {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  z-index: 5;
  width: min(var(--max-read), calc(100vw - 96px));
  max-height: 72vh;
  pointer-events: none;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(201,169,110,0.25) transparent;
  /* حافّتانِ متلاشيتانِ علويّةٌ وسفليّة — عند وجود نصٍّ طويل */
  -webkit-mask-image: linear-gradient(to bottom,
    transparent 0,
    black 56px,
    black calc(100% - 56px),
    transparent 100%);
  mask-image: linear-gradient(to bottom,
    transparent 0,
    black 56px,
    black calc(100% - 56px),
    transparent 100%);
  /* يسمح الـ pointer للتمرير، ويمنع ما سواه */
  scroll-behavior: smooth;
}
.scroll-box:hover,
.scroll-box:focus-within { pointer-events: auto; }

.scroll-box::-webkit-scrollbar { width: 2px; }
.scroll-box::-webkit-scrollbar-thumb {
  background: rgba(201,169,110,0.25);
  border-radius: 1px;
}
.scroll-box::-webkit-scrollbar-track { background: transparent; }

.verse {
  font-family: var(--font-display);
  font-size: clamp(22px, 3.2vw, 34px);
  line-height: 1.7;
  text-align: center;
  color: var(--ink);
  padding: 32px 12px;
  opacity: 0;
  transition: opacity 1.6s var(--ease-ink);
  filter: drop-shadow(0 0 24px rgba(232, 227, 214, 0.08));
}
.verse.show { opacity: 1; }
.verse.fade { opacity: 0; }

.verse .red,
.verse .rq-word.red { color: var(--accent-warm); }
.verse .gold,
.verse .rq-word.gold { color: var(--gold); }

/* كلماتٌ تظهرُ كلمةً كلمة، الحروف في كلمة واحدة متّصلة */
.rq-word {
  display: inline-block;
  white-space: nowrap;
  vertical-align: baseline;
  will-change: opacity, transform, filter;
}

/* ═════════════════════════════════════════════════════════
 *  حقل الكتابة — ألفا شفّاف، خطٌّ ذهبيٌّ يسطع عند التركيز
 * ═════════════════════════════════════════════════════════ */

.quill {
  position: fixed;
  left: 50%; bottom: 54px;
  transform: translateX(-50%);
  z-index: 10;
  width: min(620px, calc(100vw - 80px));
}

.quill-slab {
  background: transparent;
  border: 0;
  padding: 0;
  position: relative;
  display: block;
}

/* خطٌّ ذهبيٌّ خفيفٌ أسفل الحقل */
.quill-slab::before {
  content: "";
  position: absolute;
  left: 30%; right: 30%;
  bottom: -2px;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--gold-dim), transparent);
  transition: all 0.8s var(--ease-slow);
  pointer-events: none;
}

.quill-slab:focus-within::before {
  left: 0; right: 0;
  background: linear-gradient(90deg, transparent 5%, var(--gold) 50%, transparent 95%);
  box-shadow: 0 0 16px rgba(201, 169, 110, 0.35), 0 0 32px rgba(201, 169, 110, 0.15);
  height: 1px;
}

/* نقطتان ذهبيّتان خفيّتان على طرفَي الخط — عند التركيز فقط */
.quill-slab::after {
  content: "";
  position: absolute;
  left: 50%; bottom: -4px;
  width: 4px; height: 4px;
  border-radius: 50%;
  background: var(--gold);
  opacity: 0;
  transform: translateX(-50%) scale(0);
  transition: all 0.8s var(--ease-slow);
  box-shadow: 0 0 10px var(--gold);
}
.quill-slab:focus-within::after {
  opacity: 0.9;
  transform: translateX(-50%) scale(1);
  animation: quill-dot-pulse 2.4s ease-in-out infinite;
}
@keyframes quill-dot-pulse {
  0%, 100% { opacity: 0.6; transform: translateX(-50%) scale(1); }
  50%      { opacity: 1;   transform: translateX(-50%) scale(1.5); }
}

.quill-input {
  width: 100%;
  background: transparent;
  border: 0;
  outline: 0;
  color: var(--ink);
  font-family: var(--font-display);
  font-size: clamp(20px, 2.4vw, 26px);
  padding: 16px 8px;
  direction: rtl;
  text-align: center;
  caret-color: var(--gold);
  letter-spacing: 0.02em;
  font-weight: 400;
  text-shadow: 0 0 16px rgba(232, 227, 214, 0.05);
}
.quill-input::placeholder {
  color: var(--ink-muted);
  font-style: italic;
  opacity: 0.5;
  letter-spacing: 0.04em;
  transition: opacity 0.6s;
}
.quill-input:focus::placeholder { opacity: 0.25; }

/* إزالة التلميحُ القديم لأنه لم يعد ضروريًّا */
.quill-hint { display: none; }

/* زر الكيبورد للجوال */
.keyboard-pill {
  position: fixed;
  bottom: 34px; left: 50%;
  transform: translateX(-50%);
  z-index: 11;
  background: rgba(20, 19, 26, 0.45);
  backdrop-filter: blur(14px) saturate(1.2);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
  border: 1px solid var(--gold-dim);
  color: var(--gold);
  font-family: var(--font-ui);
  font-size: 12px;
  padding: 14px 26px;
  border-radius: 999px;
  letter-spacing: 0.3em;
  cursor: pointer;
  display: none;
  transition: all 0.4s var(--ease-slow);
  gap: 10px;
}
.keyboard-pill::before {
  content: "";
  position: absolute; inset: -4px;
  border-radius: 999px;
  border: 1px solid var(--gold-dim);
  opacity: 0.5;
  animation: pill-pulse 2.8s ease-in-out infinite;
  pointer-events: none;
}
@keyframes pill-pulse {
  0%, 100% { transform: scale(1);   opacity: 0.5; }
  50%      { transform: scale(1.12); opacity: 0; }
}

@media (max-width: 720px) {
  .keyboard-pill { display: inline-flex; align-items: center; }
  .quill:not(.show) { display: none; }

  /* الحقل على الجوّال — أقرب للأسفل */
  .quill { bottom: 18px; width: calc(100vw - 40px); }
  .quill-input { font-size: 18px; padding: 12px 6px; }

  .frame { inset: 12px; }
  .frame-seal { top: 8px; font-size: 12px; letter-spacing: 0.25em; }
  .topbar { top: 14px; right: 14px; gap: 10px; font-size: 11px; letter-spacing: 0.15em; }
  .topbar-left { top: 14px; left: 14px; font-size: 9px; letter-spacing: 0.25em; max-width: 42%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  .scroll-box { max-height: 65vh; width: calc(100vw - 40px); }
  .verse { padding: 16px 6px; }
  .presence { bottom: 12px; right: 14px; font-size: 9px; letter-spacing: 0.2em; }
  .scribe { bottom: 12px; left: 14px; font-size: 12px; }
}

/* ═════════════════════════════════════════════════════════
 *  شريط علوي خفيف (الإعدادات، خروج)
 * ═════════════════════════════════════════════════════════ */

.topbar {
  position: fixed; top: 18px; right: 24px;
  z-index: 20;
  display: flex; gap: 14px; align-items: center;
  font-family: var(--font-ui);
  font-size: 12px;
  letter-spacing: 0.2em;
}
.topbar a {
  color: var(--ink-muted);
  text-decoration: none;
  transition: color 0.4s;
  padding: 4px 0;
}
.topbar a:hover { color: var(--gold); }

.icon-btn {
  background: transparent;
  border: 1px solid var(--ink-faint);
  color: var(--ink-muted);
  width: 30px; height: 30px;
  border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: all 0.4s var(--ease-slow);
  padding: 0;
}
.icon-btn:hover {
  color: var(--gold);
  border-color: var(--gold-dim);
  background: rgba(201, 169, 110, 0.06);
}

.admin-mark {
  color: var(--gold);
  font-family: var(--font-display);
  font-size: 13px;
  margin-right: 2px;
  letter-spacing: 0;
}

.topbar-left {
  position: fixed; top: 18px; left: 24px;
  z-index: 20;
  font-family: var(--font-ui);
  font-size: 11px;
  color: var(--ink-faint);
  letter-spacing: 0.3em;
  text-transform: uppercase;
}

/* ═════════════════════════════════════════════════════════
 *  الدخول والتسجيل
 * ═════════════════════════════════════════════════════════ */

.gate {
  position: fixed; inset: 0;
  z-index: 5;
  display: grid; place-items: center;
  padding: 24px;
}
.gate-card {
  width: min(440px, 100%);
  padding: 44px 40px 36px;
  background: linear-gradient(180deg, rgba(20,19,26,0.82), rgba(10,10,15,0.82));
  backdrop-filter: blur(14px) saturate(1.1);
  -webkit-backdrop-filter: blur(14px) saturate(1.1);
  border: 1px solid var(--ink-faint);
  border-radius: var(--radius);
  box-shadow: 0 60px 120px -30px rgba(0,0,0,0.8);
  position: relative;
}
.gate-card::before {
  content: ""; position: absolute; inset: 8px;
  border: 1px solid var(--gold-dim);
  pointer-events: none;
}

.gate-title {
  font-family: var(--font-display);
  font-size: 44px;
  text-align: center;
  color: var(--ink);
  margin-bottom: 4px;
  letter-spacing: 0.02em;
}
.gate-subtitle {
  font-family: var(--font-ui);
  font-size: 11px;
  color: var(--ink-muted);
  text-align: center;
  letter-spacing: 0.4em;
  margin-bottom: 28px;
}

.field {
  display: block;
  margin-bottom: 18px;
}
.field label {
  display: block;
  font-family: var(--font-ui);
  font-size: 11px;
  color: var(--ink-muted);
  letter-spacing: 0.25em;
  margin-bottom: 6px;
}
.field input {
  width: 100%;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--ink-faint);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 17px;
  padding: 8px 2px;
  outline: 0;
  transition: border-color 0.4s;
  direction: rtl;
}
.field input:focus { border-bottom-color: var(--gold); }

.btn {
  width: 100%;
  margin-top: 12px;
  padding: 14px;
  background: transparent;
  border: 1px solid var(--gold-dim);
  color: var(--gold);
  font-family: var(--font-ui);
  font-size: 13px;
  letter-spacing: 0.3em;
  cursor: pointer;
  transition: all 0.5s var(--ease-slow);
}
.btn:hover {
  background: var(--gold);
  color: var(--bg-deep);
  border-color: var(--gold);
  letter-spacing: 0.4em;
}
.btn.primary {
  border-color: var(--accent-dim);
  color: var(--accent-warm);
}
.btn.primary:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--ink);
}

.gate-link {
  display: block;
  text-align: center;
  margin-top: 22px;
  color: var(--ink-muted);
  font-family: var(--font-ui);
  font-size: 12px;
  text-decoration: none;
  letter-spacing: 0.15em;
}
.gate-link:hover { color: var(--gold); }
.err {
  color: var(--accent-warm);
  font-family: var(--font-ui);
  font-size: 12px;
  text-align: center;
  margin-bottom: 16px;
  letter-spacing: 0.1em;
}

/* ═════════════════════════════════════════════════════════
 *  التفضيلات (أول دخول)
 * ═════════════════════════════════════════════════════════ */

.choice-row {
  display: flex;
  gap: 10px;
  margin-top: 8px;
}
.choice-row label {
  flex: 1;
  display: block;
  text-align: center;
  padding: 14px 6px;
  border: 1px solid var(--ink-faint);
  cursor: pointer;
  font-family: var(--font-ui);
  font-size: 12px;
  color: var(--ink-muted);
  letter-spacing: 0.2em;
  transition: all 0.4s var(--ease-slow);
  border-radius: var(--radius);
}
.choice-row input { display: none; }
.choice-row input:checked + span {
  color: var(--gold);
}
.choice-row label:has(input:checked) {
  border-color: var(--gold);
  color: var(--gold);
  background: rgba(201, 169, 110, 0.06);
}

.toggle-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px dashed var(--ink-faint);
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--ink-muted);
  letter-spacing: 0.1em;
}
.toggle-row input[type="checkbox"] { accent-color: var(--gold); transform: scale(1.2); }

/* ═════════════════════════════════════════════════════════
 *  نبضة الحضور — مؤشر صغير
 * ═════════════════════════════════════════════════════════ */

.presence {
  position: fixed;
  bottom: 18px; right: 28px;
  z-index: 15;
  font-family: var(--font-ui);
  font-size: 10px;
  color: var(--ink-muted);
  letter-spacing: 0.3em;
  display: flex; align-items: center; gap: 8px;
}
.presence-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent-warm);
  box-shadow: 0 0 12px var(--accent-warm);
  animation: pulse 3.2s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.8); }
  50%      { opacity: 1;    transform: scale(1.2); }
}

/* ═════════════════════════════════════════════════════════
 *  انتباه — عندما يكتبُ الرَّقيم
 * ═════════════════════════════════════════════════════════ */

.scribe {
  position: fixed;
  bottom: 18px; left: 28px;
  z-index: 15;
  font-family: var(--font-display);
  font-size: 14px;
  color: var(--gold);
  letter-spacing: 0.1em;
  opacity: 0;
  transition: opacity 1s var(--ease-slow);
}
.scribe.show { opacity: 0.85; }
.scribe::after {
  content: "…";
  display: inline-block;
  animation: dots 1.4s infinite;
}
@keyframes dots {
  0%, 20%  { content: "."; }
  40%      { content: ".."; }
  60%,100% { content: "…"; }
}
