/* تصميم: صفحة قراءة عربية دافئة بروح الورق القديم — عنوان بخط أميري كلاسيكي،
   واجهة بخط Tajawal حديث، لوحة ألوان كريمية مع لمسة ذهبية وتركواز،
   مع تدرجات ولمسات حركية تخليها حيّة وجذابة. */

/* ===== شاشة التحميل الافتتاحية ===== */
.splash {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  opacity: 1;
  transition: opacity 0.5s ease;
}
.splash.splash-hide {
  opacity: 0;
  pointer-events: none;
}
.splash-glow {
  position: absolute;
  width: 260px;
  height: 260px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(var(--teal-rgb), 0.28) 0%, rgba(var(--gold-rgb), 0.12) 55%, transparent 75%);
  animation: splash-pulse 1.6s ease-in-out infinite;
}
.splash-logo {
  position: relative;
  width: 148px;
  height: 148px;
  object-fit: contain;
  animation: splash-in 0.6s cubic-bezier(0.22, 1, 0.36, 1) both;
  filter: drop-shadow(0 8px 24px rgba(var(--navy-rgb), 0.18));
}
@keyframes splash-in {
  from { opacity: 0; transform: scale(0.7); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes splash-pulse {
  0%, 100% { transform: scale(0.9); opacity: 0.7; }
  50% { transform: scale(1.08); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .splash-glow,
  .splash-logo {
    animation: none;
  }
}

/* ===== بوابة الربط الإجبارية ===== */
.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: var(--bg);
}
.auth-gate[hidden] { display: none; }
.auth-gate-card {
  max-width: 420px;
  width: 100%;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 28px;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}
.auth-gate-logo { width: 92px; height: 92px; object-fit: contain; margin-bottom: 4px; }
.auth-gate-title { font-family: var(--font-display); font-size: 1.3rem; margin: 0; color: var(--ink); }
.auth-gate-text { color: var(--muted); font-size: 0.92rem; line-height: 1.8; margin: 0; }
.auth-gate-btn { width: 100%; padding: 13px 16px; font-size: 0.98rem; margin-top: 6px; }

:root {
  --font-display: 'Amiri', 'Noto Naskh Arabic', serif;
  --font-body: 'Tajawal', ui-sans-serif, system-ui, sans-serif;

  --bg: #faf6ee;
  --surface: #ffffff;
  --surface-alt: #f3ead9;
  --border: #e3d6bb;
  --ink: #2a2118;
  --muted: #6b5f4e;
  --gold: #a9792a;
  --gold-soft: #f0dfb8;
  --gold-rgb: 169, 121, 42;
  --teal: #0f7a8a;
  --teal-soft: #dff1f3;
  --teal-rgb: 15, 122, 138;
  --navy: #16202c;
  --navy-rgb: 22, 32, 44;
  --danger: #a3372f;
  --shadow: 0 1px 2px rgba(42, 33, 24, 0.06), 0 10px 30px -15px rgba(42, 33, 24, 0.25);
  --shadow-lg: 0 4px 10px rgba(42, 33, 24, 0.08), 0 24px 48px -20px rgba(42, 33, 24, 0.35);
  --radius: 14px;
  --gradient-brand: linear-gradient(135deg, var(--teal) 0%, #0a5a66 100%);
  --gradient-gold: linear-gradient(135deg, var(--gold) 0%, #c99a44 100%);
  --gradient-hero: linear-gradient(150deg, var(--teal-soft) 0%, var(--gold-soft) 100%);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14100b;
    --surface: #211b14;
    --surface-alt: #1a1510;
    --border: #3a2f22;
    --ink: #f1e9d8;
    --muted: #b8a989;
    --gold: #e0b454;
    --gold-soft: #332710;
    --gold-rgb: 224, 180, 84;
    --teal: #4fd0d6;
    --teal-soft: #0f2a2e;
    --teal-rgb: 79, 208, 214;
    --navy: #0a0f16;
    --navy-rgb: 10, 15, 22;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px -15px rgba(0, 0, 0, 0.6);
    --shadow-lg: 0 4px 14px rgba(0, 0, 0, 0.35), 0 30px 60px -20px rgba(0, 0, 0, 0.7);
    --gradient-brand: linear-gradient(135deg, var(--teal) 0%, #1a6b70 100%);
    --gradient-gold: linear-gradient(135deg, var(--gold) 0%, #a9792a 100%);
    --gradient-hero: linear-gradient(150deg, var(--teal-soft) 0%, var(--gold-soft) 100%);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #14100b;
  --surface: #211b14;
  --surface-alt: #1a1510;
  --border: #3a2f22;
  --ink: #f1e9d8;
  --muted: #b8a989;
  --gold: #e0b454;
  --gold-soft: #332710;
  --gold-rgb: 224, 180, 84;
  --teal: #4fd0d6;
  --teal-soft: #0f2a2e;
  --teal-rgb: 79, 208, 214;
  --navy: #0a0f16;
  --navy-rgb: 10, 15, 22;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px -15px rgba(0, 0, 0, 0.6);
  --shadow-lg: 0 4px 14px rgba(0, 0, 0, 0.35), 0 30px 60px -20px rgba(0, 0, 0, 0.7);
  --gradient-brand: linear-gradient(135deg, var(--teal) 0%, #1a6b70 100%);
  --gradient-gold: linear-gradient(135deg, var(--gold) 0%, #a9792a 100%);
  --gradient-hero: linear-gradient(150deg, var(--teal-soft) 0%, var(--gold-soft) 100%);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--bg);
  background-image:
    radial-gradient(circle at 8% 0%, color-mix(in srgb, var(--teal) 7%, transparent), transparent 40%),
    radial-gradient(circle at 100% 15%, color-mix(in srgb, var(--gold) 8%, transparent), transparent 45%);
  background-attachment: fixed;
  color: var(--ink);
  font-family: var(--font-body);
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
}
.page { direction: rtl; min-height: 100%; display: flex; flex-direction: column; }
button { font-family: inherit; }
::selection { background: color-mix(in srgb, var(--teal) 30%, transparent); }

/* ---------- الهيدر ---------- */
.topbar {
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 40;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(14px) saturate(1.3);
  border-bottom: 1px solid var(--border);
}
.topbar::after {
  content: "";
  display: block;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--gold) 20%, var(--teal) 55%, transparent 90%);
  opacity: 0.6;
}
.topbar-inner {
  max-width: 960px;
  margin-inline: auto;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  gap: 16px;
}
.brand { display: flex; align-items: center; gap: 10px; flex: 1 1 auto; min-width: 0; }
.brand-mark-wrap {
  width: 38px; height: 38px; border-radius: 50%; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
}
.brand-mark { width: 100%; height: 100%; object-fit: contain; flex: 0 0 auto; }
.brand-name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.18rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: linear-gradient(90deg, var(--ink), var(--teal) 140%);
  -webkit-background-clip: text;
  background-clip: text;
}

.tabs { display: flex; gap: 4px; background: var(--surface-alt); padding: 4px; border-radius: 999px; flex: 0 0 auto; border: 1px solid var(--border); }
.tab-btn {
  border: none; background: transparent; cursor: pointer;
  padding: 8px 18px; border-radius: 999px; font-size: 0.92rem; font-weight: 700;
  color: var(--muted); transition: background 0.2s, color 0.2s, box-shadow 0.2s;
  white-space: nowrap;
}
.tab-btn.is-active { background: var(--gradient-brand); color: #fff; box-shadow: 0 4px 14px -5px rgba(var(--teal-rgb), 0.6); }

.auth-area { flex: 0 0 auto; }
.auth-pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 16px; border-radius: 999px; font-size: 0.85rem; font-weight: 700;
  background: var(--teal-soft); color: var(--teal); border: 1px solid transparent;
}

/* ---------- الأزرار ---------- */
.btn {
  border-radius: 10px; border: 1px solid transparent; cursor: pointer;
  padding: 9px 16px; font-size: 0.88rem; font-weight: 700;
  transition: transform 0.12s, background 0.15s, border-color 0.15s, opacity 0.15s, box-shadow 0.2s;
  display: inline-flex; align-items: center; gap: 8px; justify-content: center;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--gradient-brand); color: #fff; box-shadow: 0 6px 16px -6px rgba(var(--teal-rgb), 0.6); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); box-shadow: 0 8px 20px -6px rgba(var(--teal-rgb), 0.7); transform: translateY(-1px); }
.btn-outline { background: var(--surface); border-color: var(--border); color: var(--ink); }
.btn-outline:hover:not(:disabled) { border-color: var(--teal); color: var(--teal); }
.btn-ghost { background: transparent; color: var(--muted); border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { color: var(--ink); border-color: var(--ink); }

/* ---------- الهيرو ---------- */
.hero { max-width: 960px; margin: 20px auto 0; padding-inline: 20px; }
.hero-card {
  position: relative; overflow: hidden;
  background: var(--gradient-hero);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 22px 26px;
  display: flex; align-items: center; gap: 16px;
  box-shadow: var(--shadow);
}
.hero-card::before {
  content: "";
  position: absolute; inset: 0;
  background-image: radial-gradient(circle, color-mix(in srgb, var(--ink) 10%, transparent) 1px, transparent 1px);
  background-size: 16px 16px;
  opacity: 0.35;
  mask-image: linear-gradient(120deg, black, transparent 70%);
}
.hero-icon {
  flex: 0 0 auto; width: 44px; height: 44px; border-radius: 12px;
  background: var(--surface); display: flex; align-items: center; justify-content: center;
  font-size: 1.3rem; box-shadow: var(--shadow); position: relative;
}
.hero-text { margin: 0; color: var(--ink); font-size: 0.96rem; position: relative; line-height: 1.8; }
.hero-text strong { color: var(--teal); }

/* ---------- الأقسام (Views) ---------- */
.view { display: none; max-width: 960px; margin: 0 auto; width: 100%; padding: 18px 20px 28px; flex: 1; }
.view.is-active { display: flex; flex-direction: column; animation: view-enter 0.35s ease; }
#view-library.is-active, #view-reader.is-active { display: block; }
@keyframes view-enter {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- عرض اسأل ---------- */
.chat { flex: 1; display: flex; flex-direction: column; gap: 14px; padding-block: 8px 16px; }
.chat-empty {
  margin: auto; text-align: center; color: var(--muted); max-width: 42ch;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.chat-empty-icon {
  font-size: 1.8rem; width: 64px; height: 64px; border-radius: 50%;
  background: var(--gradient-hero); display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow);
}

.msg { display: flex; animation: msg-pop 0.25s ease; }
@keyframes msg-pop { from { opacity: 0; transform: translateY(6px) scale(0.98); } to { opacity: 1; transform: none; } }
.msg-user { justify-content: flex-start; }
.msg-assistant { justify-content: flex-end; }
.bubble {
  max-width: min(640px, 86%); padding: 13px 17px; border-radius: var(--radius);
  font-size: 0.96rem; white-space: pre-wrap; box-shadow: var(--shadow);
}
.msg-user .bubble { background: var(--gradient-brand); color: #fff; border-bottom-left-radius: 4px; }
.msg-assistant .bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-right-radius: 4px; }
.bubble.is-loading { color: var(--muted); display: flex; align-items: center; gap: 8px; }

.ask-bar {
  position: sticky; bottom: env(safe-area-inset-bottom, 0px);
  display: flex; gap: 10px; padding-block: 10px;
  background: color-mix(in srgb, var(--bg) 90%, transparent); backdrop-filter: blur(10px);
}
.ask-input {
  flex: 1; border-radius: 12px; border: 1px solid var(--border); background: var(--surface);
  color: var(--ink); padding: 13px 16px; font-size: 0.96rem; font-family: inherit;
  box-shadow: var(--shadow);
}
.ask-input:focus { outline: 2px solid var(--teal); outline-offset: 1px; }
.ask-submit { min-width: 92px; }

/* ---------- سبينر ---------- */
.spinner {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid currentColor; border-left-color: transparent;
  animation: spin 0.7s linear infinite; opacity: 0.8;
}
.spinner-sm { width: 14px; height: 14px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- المكتبة (شبكة الكتب) ---------- */
.library-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px;
}
.book-card {
  position: relative; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; cursor: pointer; box-shadow: var(--shadow); text-align: right;
  display: flex; flex-direction: column; gap: 10px;
  transition: transform 0.2s, border-color 0.2s, box-shadow 0.2s;
}
.book-card::before {
  content: ""; position: absolute; inset-inline-start: 0; top: 0; bottom: 0; width: 4px;
  background: var(--gradient-gold); opacity: 0; transition: opacity 0.2s;
}
.book-card:hover { transform: translateY(-4px); border-color: var(--teal); box-shadow: var(--shadow-lg); }
.book-card:hover::before { opacity: 1; }
.book-card-icon {
  width: 42px; height: 42px; border-radius: 10px;
  background: var(--gradient-gold); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 1.15rem; flex: 0 0 auto;
  box-shadow: 0 4px 12px -4px rgba(var(--gold-rgb), 0.6);
}
.book-card-title { font-family: var(--font-display); font-weight: 700; font-size: 1.08rem; line-height: 1.5; }
.book-card-meta { color: var(--muted); font-size: 0.82rem; display: flex; align-items: center; gap: 6px; }
.book-card-meta .dot { width: 3px; height: 3px; border-radius: 50%; background: var(--muted); }

.skeleton-card {
  height: 140px; border-radius: var(--radius); background: var(--surface-alt);
  border: 1px solid var(--border); animation: pulse 1.3s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: 0.55; } }

.library-empty { text-align: center; color: var(--muted); padding: 40px 20px; }

/* ---------- القراءة ---------- */
.reader { display: flex; flex-direction: column; gap: 16px; }
.reader-back { align-self: flex-start; }
.reader-page {
  position: relative;
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
  box-shadow: var(--shadow-lg); padding: 34px clamp(20px, 5vw, 56px);
}
.reader-page::before {
  content: ""; position: absolute; inset-inline: 0; top: 0; height: 5px; border-radius: 18px 18px 0 0;
  background: var(--gradient-gold);
}
.reader-page-head { border-bottom: 1px solid var(--border); padding-bottom: 14px; margin-bottom: 20px; }
.reader-title { font-family: var(--font-display); font-weight: 700; font-size: 1.4rem; margin: 0 0 4px; }
.reader-meta { color: var(--muted); font-size: 0.85rem; margin: 0; }
.reader-text {
  font-family: var(--font-display); font-size: 1.22rem; line-height: 2.1; color: var(--ink);
  white-space: pre-wrap;
}
.reader-nav { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.reader-progress { color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }

/* ---------- نافذة ربط الحساب ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(20, 16, 11, 0.6); backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 60;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--surface); border-radius: 18px; padding: 26px; max-width: 380px; width: 100%;
  box-shadow: var(--shadow-lg); text-align: center; display: flex; flex-direction: column; gap: 12px;
  animation: modal-pop 0.25s ease;
}
@keyframes modal-pop { from { opacity: 0; transform: scale(0.95) translateY(6px); } to { opacity: 1; transform: none; } }
.modal h3 { margin: 0; font-family: var(--font-display); font-size: 1.2rem; }
.modal-hint { margin: 0; color: var(--muted); font-size: 0.88rem; }
.modal-hint-sm { font-size: 0.78rem; }
.modal-telegram-btn { width: 100%; text-decoration: none; }
.code-box {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  background: var(--surface-alt); border: 1px dashed var(--border); border-radius: 10px; padding: 14px;
}
.code-box code {
  font-size: 1.6rem; font-weight: 700; letter-spacing: 0.25em; color: var(--teal);
  font-family: var(--font-body); direction: ltr;
}
.modal-status {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  color: var(--muted); font-size: 0.85rem; padding-top: 4px;
}
.modal-status.is-success { color: var(--teal); font-weight: 700; }
.modal-close { align-self: center; }

/* ---------- الإعراب الآلي ---------- */
.eirab-form { display: flex; flex-direction: column; gap: 10px; align-items: flex-end; }
.eirab-input {
  width: 100%; resize: vertical; min-height: 86px;
  border-radius: 12px; border: 1px solid var(--border); background: var(--surface);
  color: var(--ink); padding: 13px 16px; font-size: 1rem; font-family: var(--font-display);
  box-shadow: var(--shadow);
}
.eirab-input:focus { outline: 2px solid var(--teal); outline-offset: 1px; }
.eirab-input:disabled { opacity: 0.6; cursor: not-allowed; }

.eirab-result { margin-top: 20px; display: flex; flex-direction: column; gap: 18px; }
.eirab-loading { display: flex; align-items: center; gap: 8px; color: var(--muted); justify-content: center; padding: 20px; }
.eirab-error-msg { color: var(--danger); text-align: center; padding: 16px; }

.eirab-section-title { font-family: var(--font-display); font-size: 1.05rem; margin: 0 0 12px; }
.eirab-words { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; }
.eirab-word-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 4px;
  transition: transform 0.15s, border-color 0.15s;
}
.eirab-word-card:hover { transform: translateY(-2px); border-color: var(--teal); }
.eirab-word { font-family: var(--font-display); font-weight: 700; font-size: 1.15rem; color: var(--teal); }
.eirab-word-analysis { font-size: 0.82rem; color: var(--muted); line-height: 1.6; }

.eirab-error-card {
  position: relative; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px 16px 14px 16px; box-shadow: var(--shadow); margin-bottom: 10px;
}
.eirab-error-card::before {
  content: ""; position: absolute; inset-inline-start: 0; top: 0; bottom: 0; width: 4px; background: var(--danger);
}
.eirab-error-head { font-weight: 700; margin-bottom: 8px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.eirab-error-badge {
  display: inline-flex; padding: 3px 10px; border-radius: 999px; font-size: 0.72rem; font-weight: 700;
  background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger);
}
.eirab-error-line { font-size: 0.88rem; color: var(--muted); margin-top: 4px; line-height: 1.7; }
.eirab-error-line strong { color: var(--ink); }

.eirab-ok {
  background: var(--teal-soft); color: var(--teal); border-radius: 10px; padding: 12px 16px;
  font-weight: 700; text-align: center;
}
.eirab-complex, .eirab-summary {
  background: var(--gold-soft); color: var(--muted); border-radius: 10px; padding: 12px 16px; font-size: 0.88rem;
  margin: 0;
}

@media (max-width: 560px) {
  .eirab-form { align-items: stretch; }
  .eirab-words { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
}

/* ---------- فوتر ---------- */
.site-footer {
  text-align: center; color: var(--muted); font-size: 0.78rem;
  padding: 18px 20px 28px;
}

/* ---------- جوّال ---------- */
@media (max-width: 720px) {
  .hero-card { flex-direction: column; text-align: center; padding: 18px; }
}
@media (max-width: 560px) {
  .topbar-inner { padding: 10px 14px; gap: 8px; flex-wrap: wrap; }
  .brand-name { font-size: 1rem; }
  .brand-mark-wrap { width: 32px; height: 32px; }
  .tabs { order: 3; flex-basis: 100%; justify-content: center; }
  .tab-btn { padding: 7px 12px; font-size: 0.85rem; }
  .auth-pill, .auth-area .btn { padding: 8px 12px; font-size: 0.8rem; }
  .hero { margin-top: 14px; }
  .view { padding: 14px 14px 22px; }
  .reader-page { padding: 22px 16px; }
  .reader-text { font-size: 1.1rem; line-height: 1.95; }
  .bubble { max-width: 92%; }
}
