:root {
  --accent: #8a1538;
  --accent-600: #6f102d;
  --accent-50: #fbeef2;
  --accent-100: #f4d6de;
  --bg: #f5f3ef;
  --surface: #ffffff;
  --surface-2: #faf8f5;
  --ink: #1f1b1c;
  --ink-2: #4d4548;
  --muted: #7a7075;
  --line: #e6e0da;
  --line-2: #d4ccc4;
  --ok: #1f7a4d;
  --warn: #a15c00;
  --danger: #b3261e;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(40, 20, 25, 0.05), 0 8px 24px rgba(40, 20, 25, 0.06);
  --font: 'IBM Plex Sans Arabic', 'Segoe UI', Tahoma, 'Noto Naskh Arabic', Arial, sans-serif;
  --topbar-h: 64px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 15.5px;
  line-height: 1.75;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
button { font-family: inherit; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.only-mobile { display: none !important; }

/* ---------- شريط العنوان ---------- */
.topbar {
  height: var(--topbar-h);
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 11px; flex: none;
  background: var(--accent); color: #fff; font-size: 22px;
  display: grid; place-items: center;
}
.brand-text { min-width: 0; }
.brand h1 { font-size: 17px; margin: 0; line-height: 1.2; }
.brand p { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex: none; }

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--line); color: var(--ink-2);
}
.badge::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.badge.ok::before { background: var(--ok); }
.badge.warn::before { background: var(--warn); }
.badge.err::before { background: var(--danger); }

/* ---------- أزرار ---------- */
.btn {
  border: 1px solid var(--line-2); background: var(--surface); color: var(--ink);
  padding: 8px 14px; border-radius: 10px; font-size: 14px; cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
  text-decoration: none; display: inline-flex; align-items: center; gap: 6px; line-height: 1.3;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-600); }
.btn.primary:disabled { opacity: .55; cursor: not-allowed; }
.btn.ghost { background: transparent; }
.btn.danger { background: #fff; border-color: var(--danger); color: var(--danger); }

/* ---------- التخطيط ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  height: calc(100vh - var(--topbar-h));
  overflow: hidden;
}
.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden; }
.chat-scroll { flex: 1; min-height: 0; overflow-y: auto; padding: 24px clamp(12px, 4vw, 48px); scroll-behavior: smooth; }

/* ---------- الترحيب ---------- */
.welcome-card {
  max-width: 820px; margin: 8px auto 0;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 24px 26px; box-shadow: var(--shadow);
}
.welcome-card h2 { margin: 0 0 8px; font-size: 20px; }
.welcome-card h3 { font-size: 14px; color: var(--muted); margin: 18px 0 8px; font-weight: 600; }
.welcome-card p { margin: 0; color: var(--ink-2); }
.law-meta { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.law-meta span { font-size: 12.5px; background: var(--surface-2); border: 1px solid var(--line); padding: 3px 10px; border-radius: 999px; color: var(--ink-2); }
.suggestions { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 8px; }
.suggestion {
  text-align: start; border: 1px solid var(--line); background: var(--surface-2);
  border-radius: 12px; padding: 10px 14px; cursor: pointer; font-size: 14px; color: var(--ink);
  transition: border-color .15s, background .15s;
}
.suggestion:hover { border-color: var(--accent); background: var(--accent-50); }

/* ---------- الرسائل ---------- */
.messages { max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; padding-bottom: 12px; }
.msg { display: flex; gap: 12px; }
.msg .avatar {
  width: 34px; height: 34px; border-radius: 10px; flex: none; display: grid; place-items: center;
  font-size: 15px; font-weight: 600;
}
.msg.user .avatar { background: var(--line); color: var(--ink-2); }
.msg.assistant .avatar { background: var(--accent); color: #fff; }
.bubble {
  min-width: 0; flex: 1;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 18px; box-shadow: var(--shadow);
}
.msg.user .bubble { background: var(--accent-50); border-color: var(--accent-100); white-space: pre-wrap; }
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble h2, .bubble h3, .bubble h4 { font-size: 15.5px; margin: 16px 0 6px; color: var(--accent-600); }
.bubble h2 { font-size: 16.5px; }
.bubble p { margin: 8px 0; }
.bubble ul, .bubble ol { margin: 6px 0; padding-inline-start: 22px; }
.bubble li { margin: 3px 0; }
.bubble code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 13.5px; }
.bubble blockquote { margin: 8px 0; padding: 6px 12px; border-inline-start: 3px solid var(--accent-100); background: var(--surface-2); border-radius: 6px; color: var(--ink-2); }
.bubble hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }
.bubble table { border-collapse: collapse; width: 100%; margin: 8px 0; font-size: 14px; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--line); padding: 6px 10px; text-align: start; vertical-align: top; }
.bubble th { background: var(--surface-2); }

.cite {
  display: inline-flex; align-items: center; vertical-align: baseline;
  margin: 0 3px; padding: 0 8px; height: 22px; border-radius: 999px;
  font-size: 12px; font-weight: 600; line-height: 1;
  background: var(--accent-50); color: var(--accent-600); border: 1px solid var(--accent-100);
  cursor: pointer; white-space: nowrap;
}
.cite:hover { background: var(--accent-100); }

.status-line { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13.5px; }
.status-line .dots { display: inline-flex; gap: 3px; }
.status-line .dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); opacity: .35; animation: pulse 1.2s infinite; }
.status-line .dots i:nth-child(2) { animation-delay: .2s; }
.status-line .dots i:nth-child(3) { animation-delay: .4s; }
@keyframes pulse { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.sources { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--line-2); }
.sources h4 { margin: 0 0 8px; font-size: 13px; color: var(--muted); font-weight: 600; }
.source {
  display: block; width: 100%; text-align: start; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 10px;
  padding: 8px 12px; margin-bottom: 6px; font-size: 13.5px; color: var(--ink);
}
.source:hover { border-color: var(--accent); }
.source strong { color: var(--accent-600); }
.source .quote { display: block; color: var(--ink-2); margin-top: 2px; }
.source .chapter { color: var(--muted); font-size: 12px; }

.meta-line { margin-top: 10px; font-size: 12px; color: var(--muted); display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.meta-line button { font-size: 12px; padding: 2px 8px; }
.error-box { background: #fdf2f1; border: 1px solid #f1c5c1; color: var(--danger); padding: 10px 14px; border-radius: 10px; }

/* ---------- المُحرّر ---------- */
.composer {
  display: flex; gap: 10px; align-items: flex-end;
  padding: 12px clamp(12px, 4vw, 48px) 6px;
  background: linear-gradient(to bottom, rgba(245,243,239,0), var(--bg) 30%);
}
.composer textarea {
  flex: 1; resize: none; max-height: 180px;
  font: inherit; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line-2); background: var(--surface); color: var(--ink);
  outline: none; box-shadow: var(--shadow);
}
.composer textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-50); }
.composer-actions { display: flex; gap: 8px; }
.composer .btn { height: 46px; padding: 0 18px; }
.disclaimer { margin: 0; padding: 0 clamp(12px, 4vw, 48px) 12px; font-size: 12px; color: var(--muted); text-align: center; }

/* ---------- لوحة القانون ---------- */
.lawpanel { border-inline-start: 1px solid var(--line); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.lawpanel-head { padding: 14px 14px 10px; border-bottom: 1px solid var(--line); }
.lawpanel-title { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.lawpanel-head input {
  width: 100%; font: inherit; font-size: 14px; padding: 9px 12px; border-radius: 10px;
  border: 1px solid var(--line-2); background: var(--surface-2); outline: none;
}
.lawpanel-head input:focus { border-color: var(--accent); }
.lawtree { overflow-y: auto; padding: 8px 10px 20px; flex: 1; min-height: 0; }
.lawtree details { margin-bottom: 4px; }
.lawtree summary {
  cursor: pointer; list-style: none; padding: 8px 10px; border-radius: 8px; font-weight: 600; font-size: 14px;
  display: flex; justify-content: space-between; align-items: center; color: var(--ink-2);
}
.lawtree summary::-webkit-details-marker { display: none; }
.lawtree summary:hover { background: var(--surface-2); }
.lawtree summary .count { font-weight: 400; font-size: 12px; color: var(--muted); }
.art-item {
  display: block; width: 100%; text-align: start; background: none; border: 0; cursor: pointer;
  padding: 7px 10px 7px 8px; margin: 1px 0; border-radius: 8px; color: var(--ink);
  border-inline-start: 2px solid transparent;
}
.art-item:hover { background: var(--accent-50); border-inline-start-color: var(--accent); }
.art-item .lbl { font-weight: 600; font-size: 13.5px; color: var(--accent-600); }
.art-item .snip { display: block; font-size: 12.5px; color: var(--muted); line-height: 1.5; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.art-item mark { background: #fff1a8; padding: 0 1px; border-radius: 2px; }
.no-results { color: var(--muted); font-size: 13.5px; padding: 12px 10px; }
.amend-tag { display: inline-block; font-size: 10.5px; padding: 0 6px; border-radius: 999px; background: #fff4e5; color: var(--warn); margin-inline-start: 6px; vertical-align: middle; }

/* ---------- درج المادة ---------- */
[hidden] { display: none !important; }
.backdrop { position: fixed; inset: 0; background: rgba(20, 10, 12, 0.35); z-index: 30; }
.drawer {
  position: fixed; top: 0; bottom: 0; inset-inline-end: 0;
  width: min(560px, 94vw); background: var(--surface); z-index: 31;
  box-shadow: 0 0 40px rgba(0,0,0,.2); display: flex; flex-direction: column;
  animation: slideIn .18s ease-out;
}
@keyframes slideIn { from { transform: translateX(-24px); opacity: 0; } to { transform: none; opacity: 1; } }
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; padding: 18px 20px 8px; }
.drawer-head h2 { margin: 0; font-size: 19px; color: var(--accent-600); }
.drawer-head p { margin: 2px 0 0; font-size: 13px; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 20px 10px; }
.badges a, .badges span { font-size: 12px; padding: 2px 10px; border-radius: 999px; background: #fff4e5; color: var(--warn); text-decoration: none; border: 1px solid #f5dcb5; }
.drawer-body { flex: 1; overflow-y: auto; padding: 4px 20px 20px; border-top: 1px solid var(--line); }
.drawer-body p { margin: 10px 0; padding: 6px 10px; border-radius: 8px; line-height: 1.9; }
.drawer-body p.hl { background: #fff7cc; box-shadow: inset 3px 0 0 #e3b400; }
.drawer-foot { display: flex; justify-content: space-between; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--line); background: var(--surface-2); }

/* ---------- الاستجابة ---------- */
@media (max-width: 960px) {
  .only-mobile { display: inline-flex !important; }
  .layout { grid-template-columns: 1fr; }
  .lawpanel {
    position: fixed; top: var(--topbar-h); bottom: 0; inset-inline-end: 0; width: min(380px, 92vw); z-index: 25;
    transform: translateX(100%); transition: transform .2s ease; box-shadow: 0 0 30px rgba(0,0,0,.15);
  }
  html[dir="rtl"] .lawpanel { transform: translateX(-100%); }
  .lawpanel.open { transform: none !important; }
  .brand p { display: none; }
  .topbar { padding: 0 12px; }
}
@media (max-width: 560px) {
  .composer { flex-wrap: wrap; }
  .composer textarea { width: 100%; }
  .composer-actions { width: 100%; justify-content: flex-end; }
  .msg .avatar { display: none; }
}

/* ---------- قاعدة المعرفة متعددة المصادر ---------- */
.lawpanel-head select {
  width: 100%; font: inherit; font-size: 13.5px; padding: 8px 10px; border-radius: 10px; margin-bottom: 8px;
  border: 1px solid var(--line-2); background: var(--surface); color: var(--ink);
}
.lawtree details.sub { margin-inline-start: 8px; border-inline-start: 2px solid var(--line); padding-inline-start: 4px; }
.lawtree details.sub summary { font-weight: 500; font-size: 13px; }
.results-head { font-size: 12.5px; color: var(--muted); padding: 6px 10px; margin: 0; }
.src-tag {
  display: inline-block; font-size: 11px; padding: 1px 8px; border-radius: 999px; margin-inline-end: 6px;
  background: var(--accent-50); color: var(--accent-600); border: 1px solid var(--accent-100); vertical-align: middle;
}
.src-tag.guide, .tag.guide { background: #eef5ff; color: #1d4f91; border: 1px solid #c9dcf5; }
.tag { display: inline-block; font-size: 11px; padding: 1px 8px; border-radius: 999px; vertical-align: middle; }
.cite.guide { background: #eef5ff; color: #1d4f91; border-color: #c9dcf5; }
.art-item .src-tag { display: block; width: max-content; margin-bottom: 2px; }

.sources-list { margin-top: 14px; font-size: 13.5px; }
.sources-list summary { cursor: pointer; color: var(--accent-600); font-weight: 600; }
.sources-list ul { margin: 8px 0 0; padding-inline-start: 18px; color: var(--ink-2); }
.sources-list li { margin: 4px 0; }

.trail { margin-top: 12px; font-size: 13px; color: var(--ink-2); }
.trail summary { cursor: pointer; color: var(--muted); font-weight: 600; }
.trail ul { margin: 6px 0 0; padding-inline-start: 16px; }
.trail li { margin: 4px 0; }
.trail-k { display: inline-block; font-size: 11.5px; padding: 0 7px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); margin-inline-end: 4px; }
.trail-hit {
  border: 1px solid var(--line); background: var(--surface-2); color: var(--accent-600); border-radius: 999px;
  font-size: 12px; padding: 1px 8px; cursor: pointer; margin: 1px 2px;
}
.trail-hit:hover { border-color: var(--accent); }
.drawer-source { margin: 0 0 4px; font-size: 12.5px; color: var(--muted); }
.table-wrap { overflow-x: auto; }
.bubble table { display: table; }

/* ---------- تهيئة اللابتوب والموبايل ---------- */
.lbl-short { display: none; }
.layout { height: calc(100dvh - var(--topbar-h)); }
html, body { overscroll-behavior: none; }
.composer { padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px)); }

/* شاشات اللابتوب الصغيرة (ارتفاع محدود) */
@media (max-height: 820px) {
  .chat-scroll { padding-top: 14px; }
  .welcome-card { padding: 18px 20px; }
  .welcome-card h2 { font-size: 18px; }
  .suggestions { gap: 6px; }
  .suggestion { padding: 8px 12px; font-size: 13.5px; }
  .disclaimer { padding-bottom: 8px; }
}
@media (min-width: 961px) and (max-width: 1200px) {
  .layout { grid-template-columns: minmax(0, 1fr) 300px; }
}

/* الموبايل والأجهزة اللمسية */
@media (pointer: coarse) {
  .cite { height: 26px; padding: 0 10px; font-size: 12.5px; }
  .btn { min-height: 40px; }
  .art-item { padding-top: 9px; padding-bottom: 9px; }
}
@media (max-width: 640px) {
  :root { --topbar-h: 54px; }
  body { font-size: 15px; }
  .lbl-full { display: none; }
  .lbl-short { display: inline; }
  .topbar { padding: 0 10px; gap: 8px; }
  .brand { gap: 8px; }
  .brand-mark { width: 34px; height: 34px; font-size: 18px; border-radius: 9px; }
  .brand h1 { font-size: 15px; white-space: nowrap; }
  .topbar-actions { gap: 6px; }
  .topbar-actions .btn { padding: 6px 10px; font-size: 16px; line-height: 1; }
  .badge { font-size: 11px; padding: 3px 8px; max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .chat-scroll { padding: 12px 10px; }
  .welcome-card { padding: 16px 14px; border-radius: 12px; }
  .welcome-card h2 { font-size: 17px; }
  .welcome-card p { font-size: 14px; }
  .suggestions { grid-template-columns: 1fr; }
  .messages { gap: 12px; }
  .bubble { padding: 12px 14px; border-radius: 12px; }
  .msg .avatar { display: none; }
  .composer { flex-wrap: nowrap; gap: 8px; padding: 8px 10px calc(6px + env(safe-area-inset-bottom, 0px)); }
  .composer textarea { width: auto; font-size: 16px; padding: 11px 12px; } /* 16px يمنع تكبير iOS عند التركيز */
  .composer-actions { width: auto; }
  .composer .btn { height: 46px; width: 46px; padding: 0; justify-content: center; font-size: 18px; }
  .disclaimer { font-size: 11px; padding: 0 10px 6px; line-height: 1.4; }
  .drawer { width: 100vw; }
  .drawer-head { padding: 14px 14px 6px; }
  .drawer-head h2 { font-size: 17px; }
  .badges, .drawer-body { padding-inline: 14px; }
  .drawer-foot { padding: 10px 14px calc(10px + env(safe-area-inset-bottom, 0px)); }
  .lawpanel { width: min(360px, 100vw); }
  .sources { font-size: 13px; }
  .source { padding: 8px 10px; }
}

/* ---------- تسجيل الدخول وتنبيه الجودة ---------- */
.login { position: fixed; inset: 0; z-index: 50; background: var(--bg); display: grid; place-items: center; padding: 20px; }
.login-card {
  width: min(420px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  padding: 28px 24px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 12px; align-items: stretch; text-align: center;
}
.login-card .brand-mark { margin: 0 auto 4px; width: 52px; height: 52px; font-size: 28px; }
.login-card h2 { margin: 0; font-size: 20px; }
.login-card p { margin: 0; color: var(--ink-2); font-size: 14px; }
.login-card input {
  font: inherit; font-size: 16px; padding: 12px 14px; border-radius: 10px; border: 1px solid var(--line-2); background: var(--surface-2); outline: none; text-align: center;
}
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-50); }
.login-card .btn { justify-content: center; height: 46px; }
.login-error { color: var(--danger); font-size: 13.5px; }
.uncited { margin-top: 12px; font-size: 13px; background: #fff8e6; border: 1px solid #f3dfae; border-radius: 10px; padding: 8px 12px; color: #7a5200; }
.uncited summary { cursor: pointer; font-weight: 600; }
.uncited ul { margin: 6px 0 0; padding-inline-start: 18px; }
.kb-date { color: var(--muted); }
