/* ============ Raed AI — التصميم ============ */
:root {
  --accent: #6d5dfc;
  --accent-2: #a78bfa;
  --accent-ink: #ffffff;
  --bg: #f7f7fb;
  --surface: #ffffff;
  --surface-2: #f0f0f6;
  --border: #e3e3ec;
  --text: #16161d;
  --muted: #6b6b7b;
  --user-bubble: #6d5dfc;
  --user-ink: #ffffff;
  --danger: #e5484d;
  --danger-bg: #fdecec;
  --shadow: 0 8px 30px rgba(20, 20, 40, .08);
  --radius: 16px;
  --sidebar-w: 280px;
  --font: "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232c;
  --border: #2a2f3a;
  --text: #ececf1;
  --muted: #9a9cab;
  --user-bubble: #5a4be0;
  --danger: #ff6b6f;
  --danger-bg: #3a1d20;
  --shadow: 0 8px 30px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 20px; height: 20px; flex: none; }

.app { display: flex; height: 100dvh; }
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

/* ---------- الشريط العلوي ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border); background: var(--surface);
}
.logo { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.15rem; direction: ltr; }
.logo b { color: var(--accent); }
.logo-mark {
  width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; font-weight: 700;
}
.top-actions { display: flex; gap: 4px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; border: 0; background: transparent;
  display: grid; place-items: center; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---------- تبويبات الأوضاع ---------- */
.modes {
  display: flex; gap: 6px; padding: 10px 16px 4px; overflow-x: auto; scrollbar-width: none;
}
.modes::-webkit-scrollbar { display: none; }
.mode-tab {
  display: flex; align-items: center; gap: 6px; white-space: nowrap;
  padding: 8px 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--muted); font-weight: 500; font-size: .93rem;
  transition: .15s;
}
.mode-tab:hover { color: var(--text); }
.mode-tab[aria-selected="true"] {
  background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
  box-shadow: 0 4px 14px rgba(109, 93, 252, .35);
}
.model-badge {
  padding: 2px 16px 8px; font-size: .8rem; color: var(--muted);
}
.model-badge b { color: var(--text); font-weight: 600; direction: ltr; unicode-bidi: isolate; }

/* ---------- الرسائل ---------- */
.messages {
  flex: 1; overflow-y: auto; padding: 12px 16px 24px; scroll-behavior: smooth;
}
.messages > * { max-width: 820px; margin-inline: auto; }
.msg { display: flex; gap: 10px; margin: 14px auto; }
.msg.user { justify-content: flex-start; flex-direction: row-reverse; }
.msg.user .bubble {
  background: var(--user-bubble); color: var(--user-ink);
  padding: 10px 14px; border-radius: 18px 18px 18px 6px;
  max-width: 85%; white-space: pre-wrap; word-wrap: break-word;
}
.msg .tag { display: inline-block; font-size: .75rem; opacity: .8; margin-top: 4px; }
.avatar {
  width: 32px; height: 32px; border-radius: 10px; flex: none; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; font-weight: 700; font-size: .9rem;
}
.msg.assistant .body { flex: 1; min-width: 0; }
.msg.error .body .err {
  background: var(--danger-bg); color: var(--danger); border-radius: 12px; padding: 10px 14px;
}
.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 6px; font-size: .78rem; color: var(--muted); }
.meta .model { direction: ltr; unicode-bidi: isolate; }
.meta .note { color: var(--accent); }
.chip-btn {
  display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--border);
  background: var(--surface); border-radius: 999px; padding: 3px 10px; font-size: .78rem; color: var(--muted);
  text-decoration: none;
}
.chip-btn:hover { color: var(--text); border-color: var(--muted); }
.chip-btn svg { width: 14px; height: 14px; }
.chip-btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Markdown */
.markdown { word-wrap: break-word; }
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }
.markdown p { margin: .5em 0; }
.markdown h1, .markdown h2, .markdown h3 { line-height: 1.4; margin: 1em 0 .4em; }
.markdown h3 { font-size: 1.05rem; }
.markdown a { color: var(--accent); }
.markdown ul, .markdown ol { padding-inline-start: 1.4em; }
.markdown :not(pre) > code {
  background: var(--surface-2); padding: .1em .4em; border-radius: 6px; font-family: var(--mono); font-size: .88em;
  direction: ltr; unicode-bidi: isolate;
}
.markdown table { border-collapse: collapse; display: block; overflow-x: auto; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 6px 10px; }
.markdown blockquote { margin: .6em 0; padding-inline-start: 12px; border-inline-start: 3px solid var(--accent); color: var(--muted); }

.code-block { margin: .8em 0; border-radius: 12px; overflow: hidden; background: #0d1117; border: 1px solid #262b36; }
.code-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 6px 10px; background: #161b22; color: #9aa4b2; font-size: .78rem; direction: ltr;
}
.code-head .actions { display: flex; gap: 6px; }
.code-head button {
  display: inline-flex; align-items: center; gap: 4px; border: 0; background: #222935; color: #d0d7de;
  border-radius: 8px; padding: 3px 9px; font-size: .76rem;
}
.code-head button:hover { background: #2d3542; }
.code-head button svg { width: 14px; height: 14px; }
.code-block pre { margin: 0; overflow-x: auto; }
.code-block pre code.hljs, .code-block pre code {
  display: block; padding: 12px 14px; font-family: var(--mono); font-size: .85rem; line-height: 1.6;
  background: transparent; color: #e6edf3;
}

.typing { display: inline-flex; gap: 4px; padding: 8px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* الصور */
.gen-image { display: block; max-width: 100%; max-height: 70vh; border-radius: 14px; box-shadow: var(--shadow); background: var(--surface-2); }
.image-loading {
  display: grid; place-items: center; gap: 10px; width: min(100%, 360px); aspect-ratio: 1;
  border-radius: 14px; background: var(--surface-2); color: var(--muted); font-size: .9rem; text-align: center; padding: 16px;
}
.spinner { width: 34px; height: 34px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* الحالة الفارغة */
.empty { text-align: center; padding: 8vh 8px 16px; }
.empty .big-mark {
  width: 64px; height: 64px; margin: 0 auto 14px; border-radius: 20px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff;
  box-shadow: 0 10px 30px rgba(109, 93, 252, .35);
}
.empty .big-mark svg { width: 30px; height: 30px; }
.empty h1 { margin: 0 0 4px; font-size: 1.5rem; }
.empty p { margin: 0 0 20px; color: var(--muted); }
.suggestions { display: grid; gap: 8px; max-width: 520px; margin: 0 auto; }
.suggestion {
  text-align: start; border: 1px solid var(--border); background: var(--surface);
  padding: 12px 14px; border-radius: 14px; color: var(--text); white-space: pre-line;
}
.suggestion:hover { border-color: var(--accent); }

/* ---------- صندوق الكتابة ---------- */
.composer { padding: 8px 16px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.composer > * { max-width: 820px; margin-inline: auto; }
.composer-box {
  display: flex; align-items: flex-end; gap: 8px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 22px; padding: 8px 8px 8px 8px; box-shadow: var(--shadow);
}
.composer-box:focus-within { border-color: var(--accent); }
#input {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent; color: var(--text);
  font: inherit; max-height: 200px; padding: 6px 8px; line-height: 1.6;
}
.send-btn {
  width: 42px; height: 42px; border-radius: 14px; border: 0; flex: none; display: grid; place-items: center;
  background: var(--accent); color: #fff;
}
.send-btn:disabled { opacity: .45; cursor: default; }
.send-btn.stop { background: var(--text); color: var(--bg); }
.send-btn svg { transform: scaleX(-1); }
.send-btn.stop svg { transform: none; }
.hint { text-align: center; color: var(--muted); font-size: .72rem; margin: 6px auto 0; }
.ratios[hidden] { display: none; }
.ratios { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 8px; scrollbar-width: none; }
.ratio {
  white-space: nowrap; border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  border-radius: 999px; padding: 4px 12px; font-size: .82rem;
}
.ratio[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); font-weight: 600; }

/* ---------- الشريط الجانبي ---------- */
.sidebar {
  position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 30; width: var(--sidebar-w);
  background: var(--surface); border-inline-end: 1px solid var(--border);
  display: flex; flex-direction: column; transform: translateX(100%); transition: transform .25s ease;
}
.sidebar.open { transform: none; }
.overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 20; display: none; }
.overlay.show { display: block; }
.sidebar-head { padding: 14px; padding-top: max(14px, env(safe-area-inset-top)); }
.btn-primary {
  width: 100%; display: flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--accent); color: #fff; border: 0; border-radius: 12px; padding: 10px; font-weight: 600;
}
.chat-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.chat-list .group { font-size: .74rem; color: var(--muted); padding: 10px 8px 4px; }
.chat-item {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 10px; cursor: pointer; color: var(--text);
}
.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--surface-2); font-weight: 600; }
.chat-item svg { width: 16px; height: 16px; color: var(--muted); }
.chat-item .title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .9rem; }
.chat-item .del { border: 0; background: transparent; color: var(--muted); padding: 2px; border-radius: 6px; opacity: .6; display: grid; }
.chat-item .del:hover { color: var(--danger); opacity: 1; }
.chat-list .none { color: var(--muted); font-size: .85rem; text-align: center; padding: 20px 8px; }
.sidebar-foot { padding: 10px 14px 14px; border-top: 1px solid var(--border); }
.btn-ghost { width: 100%; border: 0; background: transparent; padding: 8px; border-radius: 10px; color: var(--muted); font-size: .85rem; }
.btn-ghost.danger:hover { color: var(--danger); background: var(--danger-bg); }

@media (min-width: 900px) {
  .sidebar { position: static; transform: none; }
  .overlay, #menuBtn { display: none !important; }
  .topbar .logo { margin-inline-end: auto; }
}

/* ---------- المعاينة ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .55);
  display: grid; place-items: center; padding: 12px;
}
.modal[hidden] { display: none; }
.modal-card {
  width: min(1100px, 100%); height: min(90dvh, 900px); background: var(--surface);
  border-radius: 18px; overflow: hidden; display: flex; flex-direction: column; box-shadow: var(--shadow);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 8px 6px 14px; border-bottom: 1px solid var(--border); }
#previewFrame { flex: 1; width: 100%; border: 0; background: #fff; }

/* ---------- تنبيه ---------- */
.toast {
  position: fixed; bottom: 100px; left: 50%; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 999px; font-size: .88rem;
  transition: .2s; z-index: 60; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
