/*
 * NAUSS training assistant — answer styling for LibreChat.
 *
 * Restores the design of the assistant's own web UI (web/index.html) inside
 * LibreChat: each quoted provision as a card with a gold rule on its right,
 * the citation as the card's footer with a «نص مطابق للأصل» badge, and the
 * assistant's explanation in a separate dashed box that says it is not
 * regulation text.
 *
 * HOW IT IS SCOPED
 * Every rule hangs off  .message-content:has(a[href="#nauss-verified"]).
 * That link is the «نص مطابق للأصل» badge, which only this assistant's
 * answers contain (src/api.py writes it). Conversational replies and
 * refusals carry an empty link to #nauss-chat or #nauss-note instead (see the
 * end of this file). Other agents' answers, user messages and LibreChat's
 * own pages are untouched. If this file fails to load, answers fall back to
 * LibreChat's plain rendering, still correct.
 *
 * HOW IT IS LOADED
 * Not by editing LibreChat. The reverse proxy (nginx) adds one <link> to
 * each page it serves, so LibreChat upgrades and security patches apply
 * normally. The worst an upgrade can do is rename something a rule targets,
 * which makes that rule stop matching; it cannot break the app.
 *
 * The markdown shape this expects is documented in src/api.py, _markdown().
 */

@font-face {
  font-family: "NAUSS Cairo";
  src: url("/nauss/fonts/Cairo.woff2") format("woff2");
  font-weight: 200 1000;
  font-display: swap;
}

:root {
  --n-gold: #c6aa76;
  --n-teal: #2e6066;
  --n-ink: #1a2e34;
  --n-muted: #5a7078;
  --n-ok: #1d8f61;
  --n-card: #fdfbf6;
  --n-card-edge: rgba(46, 96, 102, 0.14);
  --n-rule: rgba(46, 96, 102, 0.24);
  --n-pill: rgba(46, 96, 102, 0.1);
  --n-explain-bg: rgba(46, 96, 102, 0.045);
  --n-explain-edge: rgba(46, 96, 102, 0.3);
  --n-note-bg: rgba(198, 170, 118, 0.13);
  --n-note-edge: rgba(198, 170, 118, 0.38);
  --n-shadow: 0 6px 18px rgba(0, 28, 34, 0.07);
}

/* Dark mode: the card stays light, as it was in the original UI. The
   regulation text is the one thing that should never look like the
   assistant talking. */
html.dark {
  --n-card: rgba(255, 255, 255, 0.94);
  --n-card-edge: rgba(255, 255, 255, 0.12);
  --n-explain-bg: rgba(255, 255, 255, 0.045);
  --n-explain-edge: rgba(255, 255, 255, 0.24);
  --n-note-bg: rgba(198, 170, 118, 0.14);
  --n-note-edge: rgba(198, 170, 118, 0.3);
  --n-shadow: 0 10px 26px rgba(0, 0, 0, 0.28);
}

/* ── the answer as a whole ─────────────────────────────────────────── */
.message-content:has(a[href="#nauss-verified"]) {
  direction: rtl;
  text-align: start;
  font-family: "NAUSS Cairo", "Cairo", "Segoe UI", Tahoma, sans-serif;
  line-height: 1.95;
}

/* intro */
.message-content:has(a[href="#nauss-verified"]) > p:first-child {
  font-size: 1.02em;
  margin-bottom: 0.25rem;
}

/* ── the quote: card body ──────────────────────────────────────────── */
.message-content:has(a[href="#nauss-verified"]) > blockquote {
  margin: 1.1rem 0 0;
  padding: 1rem 1.2rem 0.85rem;
  background: var(--n-card);
  color: var(--n-ink);
  border: 1px solid var(--n-card-edge);
  border-bottom: 0;
  border-inline-start: 4px solid var(--n-gold);
  border-start-start-radius: 16px;
  border-start-end-radius: 16px;
  box-shadow: var(--n-shadow);
  line-height: 2.05;
  font-style: normal;
  font-weight: 500;
}

.message-content:has(a[href="#nauss-verified"]) > blockquote p {
  margin: 0;
  color: var(--n-ink);
}

/* LibreChat's typography draws “ ” around every blockquote. The card and
   its badge already say "this is the document"; the marks were landing in
   the wrong place in right-to-left text anyway. */
.message-content:has(a[href="#nauss-verified"]) > blockquote p::before,
.message-content:has(a[href="#nauss-verified"]) > blockquote p::after {
  content: none;
}

/* A quoted table keeps its rows and columns. LibreChat's own table
   styles are made for its dark chat surface; inside the light card they
   are reset. A wide table (the eight-column rubric) scrolls sideways
   inside the card instead of squeezing every column to one word. */
.message-content:has(a[href="#nauss-verified"]) > blockquote:has(table) {
  overflow-x: auto;
}

.message-content:has(a[href="#nauss-verified"]) > blockquote table {
  display: table;
  width: 100%;
  border-collapse: collapse;
  margin: 0.35rem 0;
  font-size: 0.9em;
  line-height: 1.75;
  background: transparent;
}

.message-content:has(a[href="#nauss-verified"]) > blockquote thead,
.message-content:has(a[href="#nauss-verified"]) > blockquote tbody,
.message-content:has(a[href="#nauss-verified"]) > blockquote tr {
  background: transparent;
  border: 0;
}

.message-content:has(a[href="#nauss-verified"]) > blockquote th,
.message-content:has(a[href="#nauss-verified"]) > blockquote td {
  border: 1px solid var(--n-rule);
  padding: 0.4rem 0.65rem;
  min-width: 6.5em;
  text-align: start;
  vertical-align: top;
  color: var(--n-ink);
  background: transparent;
}

.message-content:has(a[href="#nauss-verified"]) > blockquote th {
  background: var(--n-pill);
  color: var(--n-teal);
  font-weight: 700;
}

/* a figure column («30», «80 %») needs no minimum width */
.message-content:has(a[href="#nauss-verified"]) > blockquote td:last-child,
.message-content:has(a[href="#nauss-verified"]) > blockquote th:last-child {
  min-width: 0;
}

/* ── the citation: card footer ─────────────────────────────────────── */
/* The footer's bare separators (« • », « · ») are there for plain
   rendering only. font-size: 0 hides them; every real part is an element
   and gets its size back below. */
.message-content:has(a[href="#nauss-verified"]) > blockquote + p {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  row-gap: 4px;
  margin: 0 0 1.1rem;
  padding: 0.6rem 1.2rem 0.7rem;
  background: var(--n-card);
  color: var(--n-muted);
  border: 1px solid var(--n-card-edge);
  border-top: 1px dashed var(--n-rule);
  border-inline-start: 4px solid var(--n-gold);
  border-end-start-radius: 16px;
  border-end-end-radius: 16px;
  box-shadow: var(--n-shadow);
  font-size: 0;
  line-height: 1.7;
  white-space: normal;
}

.message-content:has(a[href="#nauss-verified"]) > blockquote + p > * {
  font-size: calc(var(--markdown-font-size, 1rem) * 0.8);
  margin-inline-end: 8px;
}

.message-content:has(a[href="#nauss-verified"]) > blockquote + p strong {
  color: var(--n-teal);
  font-weight: 700;
}

/* document name, preceded by the same faint bullet as the original UI */
.message-content:has(a[href="#nauss-verified"]) > blockquote + p em {
  font-style: normal;
}

.message-content:has(a[href="#nauss-verified"]) > blockquote + p em::before {
  content: "•";
  margin-inline-end: 8px;
  opacity: 0.4;
}

/* page number pill */
.message-content:has(a[href="#nauss-verified"]) > blockquote + p code {
  font-family: inherit;
  font-weight: 600;
  color: var(--n-teal);
  background: var(--n-pill);
  padding: 1px 10px;
  border-radius: 999px;
}

/* «نص مطابق للأصل» badge — pushed to the far (left) end of the footer */
.message-content:has(a[href="#nauss-verified"]) a[href="#nauss-verified"] {
  margin-inline-start: auto;
  margin-inline-end: 0;
  color: var(--n-ok);
  font-weight: 600;
  text-decoration: none;
  pointer-events: none;
  cursor: default;
}

.message-content:has(a[href="#nauss-verified"]) a[href="#nauss-verified"]::before {
  content: "✓ ";
}

/* ── the explanation: dashed box with its label ────────────────────── */
.message-content:has(a[href="#nauss-verified"]) > h6 {
  margin: 1.3rem 0 0;
  padding: 0.7rem 1.1rem 0;
  background: var(--n-explain-bg);
  border: 1px dashed var(--n-explain-edge);
  border-bottom: 0;
  border-start-start-radius: 14px;
  border-start-end-radius: 14px;
  font-size: 0.74em;
  font-weight: 600;
  letter-spacing: 0;
  opacity: 0.75;
  text-transform: none;
}

.message-content:has(a[href="#nauss-verified"]) > h6::before {
  content: "ⓘ ";
}

.message-content:has(a[href="#nauss-verified"]) > h6 + p {
  margin: 0 0 0.9rem;
  padding: 0.3rem 1.1rem 0.85rem;
  background: var(--n-explain-bg);
  border: 1px dashed var(--n-explain-edge);
  border-top: 0;
  border-end-start-radius: 14px;
  border-end-end-radius: 14px;
  font-size: 0.97em;
  line-height: 1.95;
}

/* ── partial-coverage note ─────────────────────────────────────────── */
.message-content:has(a[href="#nauss-verified"]) > p:has(> em:only-child) {
  padding: 0.65rem 1rem;
  border-radius: 12px;
  background: var(--n-note-bg);
  border: 1px solid var(--n-note-edge);
  font-size: 0.86em;
}

.message-content:has(a[href="#nauss-verified"]) > p:has(> em:only-child) em {
  font-style: normal;
}

/* ── conversational replies and refusals ───────────────────────────── */
/* Replies with no quotes («أهلاً…», «في أي مسار أنت؟», «لم أجد…») carry an
   empty link to #nauss-chat or #nauss-note instead of the badge. They get
   the same text as an answer's intro line, with no cards: this is the
   assistant talking, and should look like it. */
.message-content:has(a[href="#nauss-chat"]),
.message-content:has(a[href="#nauss-note"]) {
  direction: rtl;
  text-align: start;
  font-family: "NAUSS Cairo", "Cairo", "Segoe UI", Tahoma, sans-serif;
  font-size: 1.02em;
  line-height: 1.95;
}

.message-content a[href="#nauss-chat"],
.message-content a[href="#nauss-note"] {
  display: none;
}

/* the two tracks in «في أي مسار أنت؟», and nearest sections in a refusal */
.message-content:has(a[href="#nauss-chat"]) > ul,
.message-content:has(a[href="#nauss-note"]) > ul {
  list-style: none;
  margin: 0.35rem 0 0.8rem;
  padding-inline-start: 0;
}

.message-content:has(a[href="#nauss-chat"]) > ul > li,
.message-content:has(a[href="#nauss-note"]) > ul > li {
  position: relative;
  margin: 0.3rem 0;
  padding-inline-start: 1.15rem;
}

.message-content:has(a[href="#nauss-chat"]) > ul > li::before,
.message-content:has(a[href="#nauss-note"]) > ul > li::before {
  content: "";
  position: absolute;
  inset-inline-start: 0;
  top: 0.85em;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--n-gold);
}

.message-content:has(a[href="#nauss-chat"]) strong {
  color: var(--n-teal);
}

html.dark .message-content:has(a[href="#nauss-chat"]) strong {
  color: var(--n-gold);
}

/* example questions: the list that ends a chat reply, as soft chips */
.message-content:has(a[href="#nauss-chat"]) > ul:last-child {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.message-content:has(a[href="#nauss-chat"]) > ul:last-child > li {
  margin: 0;
  padding: 5px 14px;
  border-radius: 12px;
  border: 1px solid var(--n-explain-edge);
  background: var(--n-explain-bg);
  font-size: 0.9em;
}

.message-content:has(a[href="#nauss-chat"]) > ul:last-child > li::before {
  content: none;
}

/* LibreChat sets its font on many elements directly; override them all
   except code. */
html body *:not(pre):not(code):not(kbd):not(pre *):not(code *) {
  font-family: "NAUSS Cairo", "Cairo", "Segoe UI", Tahoma, sans-serif !important;
}


/* LibreChat animates the welcome heading piece by piece, each piece in
   its own inline-block box; Arabic letters cannot join across boxes.
   Put the pieces back inline so the heading shapes as one word run. */
h1, h2, h1 span, h2 span {
  letter-spacing: normal !important;
}
h1 span, h2 span {
  display: inline !important;
  transform: none !important;
}

/* The welcome heading is <p class="split-parent">: LibreChat's split-text
   animation wraps the text in inline-block pieces, and Arabic letters
   cannot join across them. Put every piece back inline. */
.split-parent,
.split-parent * {
  display: inline !important;
  transform: none !important;
  letter-spacing: normal !important;
}
