:root {
  --bg: #f4f7f6;
  --bg-grad-1: #eaf5f1;
  --bg-grad-2: #f7faf9;
  --surface: #ffffff;
  --surface-2: #f7faf9;
  --sidebar: #ffffff;
  --ink: #0c1f1b;
  --ink-2: #34473f;
  --muted: #6a7b75;
  --line: #e3ebe8;
  --line-strong: #d3ded9;
  --accent: #0e7c66;
  --accent-2: #16a085;
  --accent-soft: #d6f2e9;
  --accent-ink: #0a5c4c;
  --user-bubble: #0e7c66;
  --user-ink: #ffffff;
  --danger: #b3261e;
  --shadow-sm: 0 1px 2px rgba(9, 30, 25, .05);
  --shadow-md: 0 10px 30px -12px rgba(9, 40, 33, .22);
  --shadow-lg: 0 30px 70px -30px rgba(9, 40, 33, .32);
  --radius: 18px;
  --sidebar-w: 300px;
}

[data-theme="dark"] {
  --bg: #08110f;
  --bg-grad-1: #0b1a16;
  --bg-grad-2: #060d0b;
  --surface: #0f1c19;
  --surface-2: #132420;
  --sidebar: #0b1714;
  --ink: #e8f2ee;
  --ink-2: #c3d4ce;
  --muted: #8ea7a0;
  --line: #1c2f2a;
  --line-strong: #26403a;
  --accent: #2bb894;
  --accent-2: #35d0a8;
  --accent-soft: #123b31;
  --accent-ink: #a8ecd8;
  --user-bubble: #17735f;
  --user-ink: #f2fffa;
  --shadow-md: 0 12px 34px -16px rgba(0, 0, 0, .8);
  --shadow-lg: 0 30px 70px -30px rgba(0, 0, 0, .85);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "IBM Plex Sans Arabic", "Segoe UI", system-ui, -apple-system, sans-serif;
  background:
    radial-gradient(1100px 600px at 90% -10%, var(--bg-grad-1), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, var(--bg-grad-1), transparent 55%),
    var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  gap: 0;
  overflow: hidden;
}

/* ---------------------------------------------------------------- sidebar */
.sidebar {
  background: var(--sidebar);
  border-inline-start: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 18px 14px 14px;
  min-height: 0;
  position: relative;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 11px; padding: 4px 6px 18px; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center;
  background: linear-gradient(150deg, var(--accent), var(--accent-2));
  color: #fff;
  box-shadow: 0 8px 18px -10px rgba(14, 124, 102, .9);
}
.brand-mark svg { width: 24px; height: 24px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.25; }
.brand-text strong { font-size: 17px; letter-spacing: .2px; font-weight: 700; }
.brand-text span { font-size: 11.5px; color: var(--muted); }

.new-chat {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 11px 14px; margin-bottom: 20px;
  border: 1px solid var(--line-strong); border-radius: 13px;
  background: var(--surface); color: var(--ink);
  font: inherit; font-size: 14px; font-weight: 600;
  cursor: pointer; transition: .18s ease;
  box-shadow: var(--shadow-sm);
}
.new-chat:hover { border-color: var(--accent); color: var(--accent-ink); background: var(--accent-soft); }
.new-chat svg { width: 17px; height: 17px; }

.side-label {
  font-size: 11px; font-weight: 600; letter-spacing: .08em;
  color: var(--muted); padding: 0 8px 8px;
}
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; padding-bottom: 8px; }
.conv-list::-webkit-scrollbar { width: 6px; }
.conv-list::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 99px; }
.conv-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px; border-radius: 10px; cursor: pointer;
  color: var(--ink-2); font-size: 13.5px; line-height: 1.35;
  border: 1px solid transparent;
  transition: background .15s ease, color .15s ease;
}
.conv-item:hover { background: var(--surface-2); }
.conv-item.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.conv-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .del {
  opacity: 0; border: 0; background: transparent; color: var(--muted);
  cursor: pointer; padding: 2px; border-radius: 6px; display: grid; place-items: center;
}
.conv-item:hover .del { opacity: 1; }
.conv-item .del:hover { color: var(--danger); background: rgba(179, 38, 30, .08); }
.conv-item .del svg { width: 14px; height: 14px; }

.side-foot { padding-top: 10px; border-top: 1px solid var(--line); display: grid; gap: 10px; }
.note-card {
  display: flex; gap: 8px; align-items: flex-start;
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 11px;
}
.note-card svg { width: 15px; height: 15px; color: var(--accent); flex: none; margin-top: 1px; }
.note-card p { margin: 0; font-size: 11.5px; line-height: 1.55; color: var(--muted); }
.side-links { display: flex; align-items: center; gap: 7px; font-size: 11.5px; color: var(--muted); padding-inline: 4px; }

.account {
  display: flex; align-items: center; gap: 9px;
  border: 1px solid var(--line); border-radius: 13px;
  padding: 8px 9px; background: var(--surface);
}
.acct-avatar {
  width: 34px; height: 34px; border-radius: 10px; flex: none;
  display: grid; place-items: center; overflow: hidden;
  background: var(--accent-soft); color: var(--accent-ink);
  font-size: 14px; font-weight: 700;
  background-size: cover; background-position: center;
}
.acct-info { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.3; }
.acct-info strong { font-size: 12.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.acct-info span { font-size: 10.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.acct-btn {
  border: 1px solid var(--line-strong); background: var(--surface-2); color: var(--ink-2);
  border-radius: 9px; padding: 5px 9px; font: inherit; font-size: 11px; font-weight: 600;
  cursor: pointer; white-space: nowrap; transition: .15s ease;
}
.acct-btn:hover { border-color: var(--accent); color: var(--accent-ink); background: var(--accent-soft); }

/* modal */
.modal {
  position: fixed; inset: 0; z-index: 60; display: grid; place-items: center;
  padding: 20px; background: rgba(5, 18, 14, .5); backdrop-filter: blur(6px);
}
.modal-card {
  position: relative; width: min(420px, 100%);
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 22px; padding: 28px 26px 22px; text-align: center;
  box-shadow: var(--shadow-lg); animation: fadeIn .28s ease both;
}
.modal-x {
  position: absolute; top: 12px; inset-inline-end: 12px;
  width: 32px; height: 32px; border-radius: 9px; border: 1px solid transparent;
  background: transparent; color: var(--muted); cursor: pointer; display: grid; place-items: center;
}
.modal-x:hover { background: var(--surface-2); color: var(--ink); }
.modal-x svg { width: 16px; height: 16px; }
.modal-mark {
  width: 50px; height: 50px; margin: 0 auto 14px; border-radius: 15px;
  display: grid; place-items: center; color: #fff;
  background: linear-gradient(150deg, var(--accent), var(--accent-2));
}
.modal-mark svg { width: 30px; height: 30px; }
.modal-card h3 { margin: 0 0 8px; font-size: 19px; font-weight: 600; }
.modal-sub { margin: 0 auto 20px; max-width: 320px; font-size: 12.5px; line-height: 1.75; color: var(--muted); }
.tg-slot { display: flex; justify-content: center; min-height: 48px; }
.modal-note { font-size: 10.5px; color: var(--muted); line-height: 1.6; margin: 14px 0 6px; }
.link-muted {
  border: 0; background: transparent; color: var(--muted); font: inherit; font-size: 12px;
  cursor: pointer; text-decoration: underline; text-underline-offset: 3px; padding: 6px;
}
.link-muted:hover { color: var(--accent-ink); }
.dot-live { width: 7px; height: 7px; border-radius: 50%; background: #16a34a; box-shadow: 0 0 0 3px rgba(22, 163, 74, .16); }

/* ---------------------------------------------------------------- main */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 22px; border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 78%, transparent);
  backdrop-filter: blur(14px);
  z-index: 10;
}
.icon-btn {
  width: 38px; height: 38px; border-radius: 11px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink-2);
  display: grid; place-items: center; cursor: pointer; transition: .16s ease;
}
.icon-btn:hover { color: var(--accent-ink); border-color: var(--accent); background: var(--accent-soft); }
.icon-btn svg { width: 18px; height: 18px; }
.topbar-title { flex: 1; display: flex; align-items: center; gap: 11px; min-width: 0; }
.agent-avatar {
  width: 36px; height: 36px; border-radius: 11px; flex: none;
  display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent-ink);
}
.agent-avatar svg { width: 20px; height: 20px; }
.topbar-title h1 { margin: 0; font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-title p { margin: 1px 0 0; font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; gap: 8px; }

/* ---------------------------------------------------------------- thread */
.thread { flex: 1; overflow-y: auto; scroll-behavior: smooth; padding: 26px 22px 8px; display: flex; flex-direction: column; }
.thread::-webkit-scrollbar { width: 9px; }
.thread::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 99px; border: 3px solid transparent; background-clip: content-box; }
.thread-inner { max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 22px; }

.welcome { max-width: 720px; margin: auto; text-align: center; padding: 8px 4px 24px; width: 100%; }
.hero-mark { width: 64px; height: 64px; margin: 0 auto 18px; color: var(--accent); }
.hero-mark svg { width: 100%; height: 100%; }
.welcome h2 {
  margin: 0 0 10px; font-size: clamp(24px, 3.4vw, 33px); font-weight: 600; letter-spacing: -.4px;
}
.welcome p { margin: 0 auto; max-width: 540px; color: var(--muted); font-size: 14.5px; line-height: 1.8; }
.chips { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-top: 28px; text-align: start; }
.chip {
  border: 1px solid var(--line); background: var(--surface);
  border-radius: 14px; padding: 13px 14px; cursor: pointer;
  font: inherit; font-size: 13px; color: var(--ink-2); line-height: 1.5;
  transition: .18s ease; box-shadow: var(--shadow-sm);
}
.chip span {
  display: block; font-size: 10.5px; font-weight: 700; letter-spacing: .06em;
  color: var(--accent); margin-bottom: 4px;
}
.chip:hover { border-color: var(--accent); transform: translateY(-1px); box-shadow: var(--shadow-md); }

.msg { display: flex; gap: 12px; align-items: flex-start; }
.msg .body { min-width: 0; max-width: 100%; }
.msg-avatar {
  width: 32px; height: 32px; border-radius: 10px; flex: none;
  display: grid; place-items: center; margin-top: 2px;
  background: var(--accent-soft); color: var(--accent-ink);
}
.msg-avatar svg { width: 17px; height: 17px; }

.msg.assistant .body { flex: 1; }
.msg.assistant .bubble {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px 16px 16px 6px;
  padding: 15px 17px; box-shadow: var(--shadow-sm);
  font-size: 15px; line-height: 1.9; color: var(--ink);
}
.msg.user { flex-direction: row-reverse; justify-content: flex-start; }
.msg.user .bubble {
  background: var(--user-bubble); color: var(--user-ink);
  border-radius: 16px 16px 6px 16px;
  padding: 12px 16px; font-size: 14.5px; line-height: 1.8;
  max-width: min(78%, 620px); box-shadow: 0 10px 24px -16px rgba(14, 124, 102, .9);
  white-space: pre-wrap; word-break: break-word;
}

.msg-meta { display: flex; align-items: center; gap: 8px; margin-top: 7px; padding-inline: 4px; min-height: 18px; }
.msg-meta .time { font-size: 10.5px; color: var(--muted); }
.msg-meta button {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  font: inherit; font-size: 11px; display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 6px; border-radius: 7px; opacity: .85;
}
.msg-meta button:hover { background: var(--surface-2); color: var(--accent-ink); opacity: 1; }
.msg-meta svg { width: 13px; height: 13px; }

/* markdown inside assistant bubble */
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble p { margin: 0 0 10px; }
.bubble h1, .bubble h2, .bubble h3 { margin: 16px 0 8px; line-height: 1.5; font-weight: 600; }
.bubble h1 { font-size: 19px; } .bubble h2 { font-size: 17px; } .bubble h3 { font-size: 15.5px; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-inline-start: 20px; }
.bubble li { margin: 4px 0; }
.bubble strong { font-weight: 700; }
.bubble a { color: var(--accent-ink); text-decoration: underline; text-underline-offset: 3px; }
.bubble code {
  background: var(--surface-2); border: 1px solid var(--line);
  padding: 1px 5px; border-radius: 6px; font-size: .88em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; direction: ltr; display: inline-block;
}
.bubble pre {
  background: var(--surface-2); border: 1px solid var(--line);
  padding: 12px 14px; border-radius: 12px; overflow-x: auto; direction: ltr; text-align: left;
}
.bubble pre code { background: none; border: 0; padding: 0; font-size: 12.5px; line-height: 1.6; }
.bubble blockquote {
  margin: 10px 0; padding: 8px 14px; border-inline-start: 3px solid var(--accent);
  background: var(--surface-2); border-radius: 0 10px 10px 0; color: var(--ink-2);
}
.bubble table { width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 13.5px; }
.bubble th, .bubble td { border: 1px solid var(--line); padding: 7px 10px; text-align: start; }
.bubble th { background: var(--surface-2); font-weight: 600; }
.bubble hr { border: 0; border-top: 1px solid var(--line); margin: 14px 0; }

.caret {
  display: inline-block; width: 7px; height: 17px; vertical-align: -3px;
  background: var(--accent); border-radius: 2px; margin-inline-start: 3px;
  animation: blink 1.05s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.thinking {
  display: inline-flex; align-items: center; gap: 8px;
  color: var(--muted); font-size: 12.5px;
}
.thinking .dots { display: inline-flex; gap: 3px; }
.thinking .dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--accent); opacity: .45; animation: pulse 1.2s infinite; }
.thinking .dots i:nth-child(2) { animation-delay: .15s; }
.thinking .dots i:nth-child(3) { animation-delay: .3s; }
@keyframes pulse { 0%, 60%, 100% { opacity: .35; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-2px); } }

.tool-pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11.5px; color: var(--accent-ink); background: var(--accent-soft);
  border-radius: 999px; padding: 4px 10px; margin: 2px 0 8px;
}
.tool-pill svg { width: 12px; height: 12px; }

.think-block { border: 1px dashed var(--line-strong); border-radius: 11px; padding: 8px 12px; margin-bottom: 10px; }
.think-block summary { cursor: pointer; font-size: 11.5px; color: var(--muted); list-style: none; }
.think-block summary::-webkit-details-marker { display: none; }
.think-block .tx { font-size: 12px; color: var(--muted); line-height: 1.75; white-space: pre-wrap; margin-top: 8px; max-height: 220px; overflow: auto; direction: auto; }

.error-note {
  color: var(--danger); background: rgba(179, 38, 30, .07);
  border: 1px solid rgba(179, 38, 30, .2); border-radius: 10px;
  padding: 9px 12px; font-size: 13px;
}

/* ---------------------------------------------------------------- composer */
.composer-wrap { padding: 10px 22px max(16px, env(safe-area-inset-bottom)); background: linear-gradient(to top, var(--bg) 55%, transparent); }
.composer {
  max-width: 820px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 10px;
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: 20px; padding: 10px 10px 10px 14px;
  box-shadow: var(--shadow-md);
  transition: border-color .18s ease, box-shadow .18s ease;
}
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 12%, transparent), var(--shadow-md); }
.composer textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent;
  font: inherit; font-size: 14.5px; line-height: 1.7; color: var(--ink);
  max-height: 190px; padding: 7px 4px;
}
.composer textarea::placeholder { color: var(--muted); }
.composer-actions { display: flex; gap: 6px; align-items: center; }
.send {
  width: 40px; height: 40px; border-radius: 12px; border: 0; cursor: pointer;
  display: grid; place-items: center; transition: .16s ease;
  background: linear-gradient(150deg, var(--accent), var(--accent-2)); color: #fff;
  box-shadow: 0 10px 20px -12px rgba(14, 124, 102, .95);
}
.send:hover { transform: translateY(-1px); filter: brightness(1.05); }
.send:disabled { opacity: .45; cursor: not-allowed; transform: none; box-shadow: none; }
.send svg { width: 19px; height: 19px; }
.send.stop { background: var(--surface-2); color: var(--ink); border: 1px solid var(--line-strong); box-shadow: none; }
.send.stop svg { width: 16px; height: 16px; }
.hint { text-align: center; font-size: 10.5px; color: var(--muted); margin: 9px 0 0; }

.scroll-down {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: 104px; width: 36px; height: 36px; border-radius: 50%;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink-2);
  display: grid; place-items: center; cursor: pointer; box-shadow: var(--shadow-md); z-index: 8;
}
.scroll-down svg { width: 17px; height: 17px; }

/* attachments */
.files { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.attach-file {
  display: flex; align-items: center; gap: 11px; padding: 10px 12px; max-width: 380px;
  border: 1px solid var(--line-strong); background: var(--surface); border-radius: 14px;
  text-decoration: none; color: var(--ink); transition: .15s ease;
}
.attach-file:hover { border-color: var(--accent); box-shadow: var(--shadow-sm); }
.attach-ext {
  width: 40px; height: 40px; flex: 0 0 40px; border-radius: 11px; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent-ink); font-size: 10.5px; font-weight: 800; letter-spacing: .4px;
}
.attach-info { display: flex; flex-direction: column; min-width: 0; flex: 1; gap: 2px; }
.attach-info b { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attach-info small { font-size: 11px; color: var(--muted); }
.attach-dl svg { width: 18px; height: 18px; color: var(--accent); }
.attach-img { display: inline-block; }
.attach-img img { display: block; max-width: min(330px, 100%); border-radius: 14px; border: 1px solid var(--line); }
.attach-audio { border: 1px solid var(--line-strong); background: var(--surface); border-radius: 14px; padding: 9px 11px; max-width: 380px; }
.attach-audio audio { width: 100%; height: 36px; }
.attach-cap { font-size: 11px; color: var(--muted); margin-bottom: 5px; }
.attach-video { display: block; max-width: min(360px, 100%); border-radius: 14px; border: 1px solid var(--line); }

/* bidi + rich text inside bubbles */
.bubble p, .bubble li, .bubble blockquote { unicode-bidi: plaintext; }
.bubble blockquote {
  margin: 10px 0; padding: 9px 12px; border-radius: 10px;
  border-inline-start: 3px solid var(--accent);
  background: var(--surface-2); color: var(--ink-2); font-size: 12.5px;
}
.bubble h2, .bubble h3, .bubble h4 { margin: 16px 0 8px; line-height: 1.55; }
.bubble h2 { font-size: 17px; }
.bubble h3 { font-size: 15px; }
.bubble h4 { font-size: 13.5px; }
.bubble li { margin: 3px 0; }
.bubble bdi { unicode-bidi: isolate; }

/* ---------------------------------------------------------------- misc */
.only-mobile { display: none; }
.modal[hidden] { display: none; }
.scrim { position: fixed; inset: 0; background: rgba(6, 20, 16, .45); backdrop-filter: blur(2px); z-index: 15; }
.skeleton { height: 12px; border-radius: 6px; background: linear-gradient(90deg, var(--line) 25%, var(--surface-2) 37%, var(--line) 63%); background-size: 400% 100%; animation: shimmer 1.3s infinite; }
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: -100% 0; } }
.fade-in { animation: fadeIn .35s ease both; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; top: 0; bottom: 0; right: 0;
    width: min(86vw, 320px); transform: translateX(101%);
    transition: transform .26s cubic-bezier(.4, 0, .2, 1);
    box-shadow: var(--shadow-lg); border: 0;
  }
  .sidebar.open { transform: none; }
  .only-mobile { display: grid; }
  .topbar { padding: 11px 14px; }
  .thread { padding: 18px 14px 4px; }
  .composer-wrap { padding: 8px 12px 12px; }
  .chips { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 560px) {
  .chips { grid-template-columns: 1fr; }
  .msg.user .bubble { max-width: 88%; }
  .thread-inner { gap: 18px; }
}
