/* FuraaAI — dark, RTL academic assistant UI (Qwen-inspired) */
:root {
  --bg:            #1b1b1d;
  --bg-elev:       #232326;
  --sidebar:       #171718;
  --sidebar-hover: #2a2a2e;
  --border:        #313135;
  --text:          #ececf1;
  --text-dim:      #9a9aa5;
  --text-faint:    #6b6b76;
  --accent:        #2f6bff;
  --accent-hover:  #4780ff;
  --danger:        #e5484d;
  --success:       #34c759;
  --radius:        14px;
  --radius-lg:     22px;
  --font: "Segoe UI", "Noto Kufi Arabic", "Cairo", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  direction: rtl;
  -webkit-font-smoothing: antialiased;
  font-size: 15px;
}
a { color: var(--accent); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }

/* ---------- App shell ---------- */
.app { display: flex; height: 100vh; overflow: hidden; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 268px;
  min-width: 268px;
  background: var(--sidebar);
  border-inline-start: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin .25s ease, transform .25s ease;
}
/* Desktop: collapsing slides the sidebar off the right edge (RTL: inline-start
   is the right side, so a negative start margin pulls it off-screen and lets
   the main pane expand to fill the space). */
.sidebar.collapsed { margin-inline-start: -268px; }
.sb-top { padding: 12px; display: flex; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.brand .logo {
  width: 30px; height: 30px; border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), #7a3cff);
  display: grid; place-items: center; color: #fff; font-weight: 800;
}
.icon-btn {
  background: transparent; border: 0; color: var(--text-dim);
  width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center;
}
.icon-btn:hover { background: var(--sidebar-hover); color: var(--text); }

.sb-new {
  margin: 4px 12px 8px; padding: 11px 12px;
  background: transparent; border: 1px solid var(--border); color: var(--text);
  border-radius: var(--radius); display: flex; align-items: center; gap: 10px;
  font-weight: 600; font-size: 14px;
}
.sb-new:hover { background: var(--sidebar-hover); }

.sb-section { padding: 4px 12px; }
.sb-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: 10px; color: var(--text-dim);
  font-size: 14px; cursor: pointer; border: 0; background: transparent; width: 100%; text-align: start;
}
.sb-item:hover { background: var(--sidebar-hover); color: var(--text); }
.sb-label { padding: 14px 14px 6px; font-size: 12px; color: var(--text-faint); font-weight: 600; }

.chat-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.chat-row {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px; border-radius: 10px; color: var(--text-dim);
  font-size: 14px; cursor: pointer; position: relative;
}
.chat-row:hover { background: var(--sidebar-hover); color: var(--text); }
.chat-row.active { background: var(--sidebar-hover); color: var(--text); }
.chat-row .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row .del { opacity: 0; color: var(--text-faint); }
.chat-row:hover .del { opacity: 1; }
.chat-row .del:hover { color: var(--danger); }

.sb-user {
  border-top: 1px solid var(--border); padding: 10px 12px;
  display: flex; align-items: center; gap: 10px;
}
.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: linear-gradient(135deg, #2f6bff, #7a3cff);
  display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 14px;
}
.sb-user .meta { flex: 1; overflow: hidden; }
.sb-user .name { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sb-user .credit { font-size: 12px; color: var(--text-faint); }

/* ---------- Main ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  height: 54px; display: flex; align-items: center; gap: 12px;
  padding: 0 16px; border-bottom: 1px solid transparent;
}
.model-pill {
  display: flex; align-items: center; gap: 8px; padding: 7px 12px;
  border-radius: 10px; font-weight: 600; color: var(--text);
}
.model-pill:hover { background: var(--sidebar-hover); }
.agent-badge {
  margin-inline-start: auto; font-size: 12px; color: var(--text-dim);
  border: 1px solid var(--border); padding: 6px 12px; border-radius: 999px;
  display: flex; align-items: center; gap: 6px;
}
.agent-badge .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--success); }
.credit-badge {
  margin-inline-start: 10px; font-size: 13px; font-weight: 600; color: var(--text);
  border: 1px solid var(--accent); background: rgba(47,107,255,.12);
  padding: 6px 14px; border-radius: 999px; display: flex; align-items: center; gap: 6px; white-space: nowrap;
}
.credit-badge b { color: var(--accent-hover); }

/* ---------- Conversation area ---------- */
.stream { flex: 1; overflow-y: auto; }
.stream-inner { max-width: 780px; margin: 0 auto; padding: 24px 20px 140px; }

.hero { text-align: center; margin-top: 22vh; }
.hero h1 { font-size: 30px; font-weight: 700; margin: 0; }
.hero p { color: var(--text-dim); margin-top: 10px; }

.msg { display: flex; gap: 14px; margin: 22px 0; }
.msg .who {
  width: 30px; height: 30px; border-radius: 8px; flex: none;
  display: grid; place-items: center; font-size: 13px; font-weight: 700;
}
.msg.user .who { background: #3a3a40; color: var(--text); }
.msg.assistant .who { background: linear-gradient(135deg, var(--accent), #7a3cff); color: #fff; }
.msg .body { padding-top: 3px; line-height: 1.9; overflow-wrap: anywhere; }
.msg .body p { margin: 0 0 12px; }
.msg .body pre {
  background: #0f0f10; border: 1px solid var(--border); border-radius: 10px;
  padding: 14px; overflow-x: auto; direction: ltr; text-align: left;
}
.msg .body code { background: #0f0f10; padding: 2px 6px; border-radius: 6px; direction: ltr; }
.msg .body pre code { background: none; padding: 0; }
.attach-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.chip {
  display: flex; align-items: center; gap: 6px; font-size: 12px;
  background: var(--bg-elev); border: 1px solid var(--border);
  padding: 5px 10px; border-radius: 8px; color: var(--text-dim); max-width: 220px;
}
.chip .fname {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 150px;
}
.chip b { flex: none; }
.chip.uploading { border-color: var(--accent); color: var(--text); }
.chip.chip-error { border-color: var(--danger); color: #ff9b9e; }
.chip .spinner {
  width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.25); border-top-color: var(--accent-hover);
  animation: spin .7s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Conversation load spinner (shown while a chat's history is fetched) */
.conv-loading { display: grid; place-items: center; padding: 64px 0; color: var(--text-dim); }
.conv-loading .ld {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--accent-hover);
  animation: spin .7s linear infinite;
}

/* Copy-answer action bar under assistant messages */
.msg-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 8px; }
.act-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 11px; font-size: 12.5px; font-weight: 600;
  background: transparent; border: 1px solid var(--border); border-radius: 9px;
  color: var(--text-dim); opacity: .85; transition: .15s;
}
.act-btn:hover { color: var(--text); background: var(--sidebar-hover); opacity: 1; }
.act-btn:disabled { cursor: default; opacity: .6; }

/* AI-detection score badge — colour-graded by AI likelihood. */
.ai-badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 11px; border-radius: 999px; font-size: 12.5px; font-weight: 600;
}
.ai-badge em { font-style: normal; opacity: .7; font-weight: 500; }
.ai-badge.lo  { background: rgba(88,217,119,.14);  color: #58d977; }
.ai-badge.mid { background: rgba(255,180,60,.15);  color: #ffb43c; }
.ai-badge.hi  { background: rgba(229,72,77,.15);   color: #ff7a7e; }

/* Early-stop notice (long-form hit the balance/cost cap). */
.stopped-note {
  margin-top: 10px; padding: 8px 12px; font-size: 12.5px;
  color: #ffb43c; background: rgba(255,180,60,.1);
  border: 1px solid rgba(255,180,60,.25); border-radius: 9px;
}

/* ---------- Edit a sent message ---------- */
.msg.user .body { position: relative; }
.edit-btn {
  margin-inline-start: 8px; padding: 2px 7px; font-size: 12px; line-height: 1.4;
  background: transparent; border: 1px solid var(--border); border-radius: 8px;
  color: var(--text-faint); opacity: .5; transition: opacity .15s; vertical-align: middle;
}
.msg.user:hover .edit-btn, .edit-btn:focus-visible { opacity: 1; }
.edit-btn:hover { color: var(--text); background: var(--sidebar-hover); }
.msg.user.editing .edit-btn { display: none; }
.edit-area {
  width: 100%; margin-top: 4px; background: var(--bg); color: var(--text);
  border: 1px solid var(--accent); border-radius: 12px; padding: 10px 12px;
  font-family: inherit; font-size: 15px; line-height: 1.7; resize: none; outline: none;
}
.edit-actions { display: flex; gap: 8px; margin-top: 8px; }
.edit-actions button {
  padding: 7px 15px; border: 0; border-radius: 9px; font-size: 13px; font-weight: 600;
  background: var(--accent); color: #fff;
}
.edit-actions button:hover { background: var(--accent-hover); }
.edit-actions .cancel { background: #3a3a40; color: var(--text); }
.edit-actions .cancel:hover { background: #47474e; }

/* ---------- Composer ---------- */
.composer-wrap {
  position: absolute; bottom: 0; inset-inline: 0;
  padding: 0 20px 22px; background: linear-gradient(transparent, var(--bg) 32%);
}
.composer {
  max-width: 780px; margin: 0 auto; background: var(--bg-elev);
  border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 10px 12px; box-shadow: 0 8px 30px rgba(0,0,0,.35);
}
.composer .files {
  display: flex; flex-wrap: wrap; gap: 8px; padding: 4px 4px 8px;
  max-height: 104px; overflow-y: auto;   /* never let attachments fill the screen */
}
.files-count { width: 100%; font-size: 12px; color: var(--text-dim); padding: 2px 2px 0; }
.composer .row { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea {
  flex: 1; background: transparent; border: 0; color: var(--text);
  resize: none; outline: none; font-family: inherit; font-size: 15px;
  max-height: 200px; line-height: 1.6; padding: 8px 6px;
}
.composer textarea::placeholder { color: var(--text-faint); }
.round-btn {
  width: 38px; height: 38px; border-radius: 50%; border: 0; flex: none;
  display: grid; place-items: center; background: #3a3a40; color: var(--text);
}
.round-btn:hover { background: #47474e; }
.round-btn.send { background: var(--accent); color: #fff; }
.round-btn.send:hover { background: var(--accent-hover); }
/* Streaming: the send button turns into a Stop button. */
.round-btn.stop { background: var(--danger); color: #fff; font-size: 15px; line-height: 1; }
.round-btn.stop:hover { background: #f1585d; }
.round-btn:disabled { opacity: .5; cursor: default; }
.composer-tools { display: flex; align-items: center; gap: 8px; padding: 8px 4px 2px; }
.cite-field {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-dim);
}
.cite-field .cite-ic { font-size: 13px; }
.cite-field .cite-label { font-weight: 600; }
.cite-select {
  background: #3a3a40; color: var(--text); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 10px; font-family: inherit; font-size: 12px;
  outline: none; cursor: pointer;
}
.cite-select:hover { background: #47474e; }
.cite-select:focus { border-color: var(--accent); }
.hint { text-align: center; font-size: 11px; color: var(--text-faint); margin-top: 8px; }

.typing::after { content: "▋"; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* "thinking" status shown before the first token streams in */
.status { display: inline-flex; align-items: center; gap: 4px; font-size: 15px; color: var(--text-faint); }
.status .lbl {
  background: linear-gradient(90deg, var(--text-faint) 30%, var(--text) 50%, var(--text-faint) 70%);
  background-size: 200% 100%;
  -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: statshimmer 1.5s linear infinite;
}
.status .dots::after { content: ""; animation: statdots 1.2s steps(1) infinite; }
@keyframes statshimmer { to { background-position: -200% 0; } }
@keyframes statdots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }

/* ---------- Auth pages ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-card {
  width: 100%; max-width: 400px; background: var(--bg-elev);
  border: 1px solid var(--border); border-radius: 20px; padding: 34px 30px;
}
.auth-card .logo-big {
  width: 54px; height: 54px; border-radius: 15px; margin: 0 auto 14px;
  background: linear-gradient(135deg, var(--accent), #7a3cff);
  display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 24px;
}
.auth-card h1 { text-align: center; font-size: 22px; margin: 0 0 4px; }
.auth-card .sub { text-align: center; color: var(--text-dim); margin: 0 0 24px; font-size: 14px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 6px; }
.field input, .field textarea, .field select {
  width: 100%; padding: 11px 13px; background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 11px; outline: none; font-family: inherit; font-size: 14px;
}
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); }
.btn {
  width: 100%; padding: 12px; border: 0; border-radius: 11px;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 15px;
}
.btn:hover { background: var(--accent-hover); }
.btn.secondary { background: #3a3a40; }
.btn.secondary:hover { background: #47474e; }
.auth-alt { text-align: center; margin-top: 18px; color: var(--text-dim); font-size: 14px; }
.error-box {
  background: rgba(229,72,77,.12); border: 1px solid rgba(229,72,77,.4);
  color: #ff9b9e; padding: 10px 12px; border-radius: 10px; font-size: 13px; margin-bottom: 14px; display: none;
}
.error-box.show { display: block; }

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: #3a3a40; border-radius: 8px; }
::-webkit-scrollbar-thumb:hover { background: #4a4a52; }

/* Off-canvas backdrop (mobile) */
.backdrop {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 45;
}

@media (max-width: 760px) {
  /* Mobile: sidebar becomes an off-canvas overlay. `.collapsed` = hidden. */
  .sidebar {
    position: fixed; top: 0; inset-inline-end: 0; z-index: 50; height: 100%;
    margin: 0 !important;                    /* margin trick is desktop-only */
    transform: translateX(0);
    box-shadow: -10px 0 34px rgba(0,0,0,.45);
  }
  .sidebar.collapsed { transform: translateX(100%); }   /* slid off right edge (RTL) */
  #toggleSb { display: none; }               /* the in-sidebar arrows are desktop-only */
  .agent-badge { display: none; }            /* label too long for a phone topbar */
  .model-pill { font-size: 15px; }
  .credit-badge { margin-inline-start: auto; padding: 6px 10px; }
  .backdrop.show { display: block; }
  .stream-inner { padding: 20px 14px 150px; }
  .hero { margin-top: 16vh; }
  .hero h1 { font-size: 24px; }
}
/* The reopen (hamburger) lives in the topbar and is always available. */
#openSb { display: grid; }

/* ---------- drag & drop upload hint ---------- */
body.drag-over::after {
  content: "أفلِت الملفات هنا لإرفاقها";
  position: fixed; inset: 0; z-index: 9999;
  display: grid; place-items: center;
  font-size: 20px; font-weight: 700; color: #fff;
  background: rgba(20, 22, 34, .72);
  border: 3px dashed rgba(255, 255, 255, .55);
  border-radius: 12px;
  pointer-events: none;
}

/* ---------- Per-session reference library (المراجع) ---------- */
.ref-btn {
  margin-inline-start: 10px; font-size: 13px; font-weight: 600; color: var(--text);
  border: 1px solid var(--border); background: var(--bg-elev);
  padding: 6px 12px; border-radius: 999px; cursor: pointer;
  display: flex; align-items: center; gap: 6px; white-space: nowrap;
}
.ref-btn:hover { background: var(--sidebar-hover); border-color: var(--accent); }
.ref-count {
  display: none; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px;
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 700;
  align-items: center; justify-content: center;
}

.ref-modal {
  position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.ref-modal[hidden] { display: none; }
.ref-box {
  width: min(560px, 100%); max-height: 80vh; overflow-y: auto;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 18px 18px 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.ref-head { display: flex; align-items: center; justify-content: space-between; }
.ref-head b { font-size: 16px; }
.ref-x { background: transparent; border: 0; color: var(--text-dim); font-size: 16px; cursor: pointer; }
.ref-x:hover { color: var(--text); }
.ref-note { color: var(--text-dim); font-size: 13px; line-height: 1.7; margin: 8px 0 14px; }

.ref-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.ref-empty { color: var(--text-faint); font-size: 13px; text-align: center; padding: 18px 0; }
.ref-item {
  display: flex; align-items: center; gap: 10px;
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 11px; background: var(--bg);
}
.ref-item.is-bad { border-color: var(--danger); }
.ref-ic { font-size: 18px; }
.ref-info { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.ref-name { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ref-sub { font-size: 11px; color: var(--text-faint); }
.ref-bad { color: var(--danger); }
.ref-del { background: transparent; border: 0; color: var(--text-faint); cursor: pointer; }
.ref-del:hover { color: var(--danger); }

.ref-actions { display: flex; align-items: center; gap: 12px; }
.ref-add {
  background: var(--accent); border: 0; color: #fff; font-weight: 600; font-size: 14px;
  padding: 10px 16px; border-radius: 10px; cursor: pointer;
}
.ref-add:hover { background: var(--accent-hover); }
.ref-status { font-size: 12px; color: var(--text-dim); }

@media (max-width: 640px) {
  .ref-btn { padding: 6px 10px; font-size: 0; gap: 0; }   /* icon only on phones */
  .ref-btn .ref-count { font-size: 11px; }
}

.ref-ok   { color: var(--success); }
.ref-wait { color: var(--text-faint); }
