:root {
  --bg: #0e0b1e;
  --panel: #171233;
  --panel2: #221c47;
  --glass: rgba(34, 28, 71, 0.72);
  --line: rgba(255, 255, 255, 0.08);
  --text: #f1eefc;
  --muted: #9a93c4;
  --acc: #8b5cf6;
  --acc2: #06b6d4;
  --grad: linear-gradient(135deg, #8b5cf6, #06b6d4);
  --danger: #ff5e62;
  --r: 16px;
  --tr: 0.18s ease;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  font-family: "Segoe UI", Tahoma, "Noto Kufi Arabic", system-ui, sans-serif;
  background: var(--bg); color: var(--text);
  overflow: hidden;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, textarea, select { font-family: inherit; color: var(--text); }
.hidden { display: none !important; }

#app { display: flex; height: 100dvh; }

/* sidebar */
#sidebar {
  width: 290px; min-width: 290px; background: var(--panel);
  border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 14px; gap: 12px;
}
.side-head { display: flex; align-items: center; justify-content: space-between; }
.logo { font-size: 22px; font-weight: 800; display: flex; align-items: center; gap: 8px; }
.logo-mark, .welcome-mark, .auth-logo {
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--grad); color: #fff; border-radius: 12px;
  width: 36px; height: 36px; font-weight: 800; font-size: 20px;
  box-shadow: 0 4px 18px rgba(139, 92, 246, 0.45);
}
.new-chat { width: 100%; }
#conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.conv-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border-radius: 12px; cursor: pointer; transition: var(--tr);
}
.conv-row:hover { background: var(--panel2); }
.conv-row.active { background: var(--panel2); box-shadow: inset 2px 0 0 var(--acc); }
[dir="ltr"] .conv-row.active { box-shadow: inset -2px 0 0 var(--acc); }
.conv-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.conv-del { opacity: 0; font-size: 13px; transition: var(--tr); }
.conv-row:hover .conv-del { opacity: 0.7; }
.side-foot { display: flex; flex-direction: column; gap: 8px; border-top: 1px solid var(--line); padding-top: 10px; }
.usage-note { font-size: 12px; color: var(--muted); text-align: center; }
.side-user { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.side-user select { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 4px 6px; font-size: 13px; }
#me-name { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }

/* main */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.conv-title { flex: 1; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
.icon-btn {
  font-size: 18px; width: 38px; height: 38px; border-radius: 10px;
  display: inline-flex; align-items: center; justify-content: center; transition: var(--tr);
}
.icon-btn:hover { background: var(--panel2); }
.icon-btn.active { background: var(--acc); color: #fff; }
.icon-btn.recording { background: var(--danger); color: #fff; animation: pulse 1.1s infinite; }
@keyframes pulse { 50% { opacity: 0.55; } }

#msgs { flex: 1; overflow-y: auto; padding: 20px 0; scroll-behavior: smooth; }
.msg-row { display: flex; padding: 6px 0; }
.msg-row.mine { justify-content: flex-end; }
.bubble {
  max-width: min(760px, 88%); padding: 12px 16px; border-radius: var(--r);
  line-height: 1.75; font-size: 15.5px; overflow-wrap: break-word;
}
.msg-row { padding-inline: max(16px, calc((100% - 900px) / 2)); }
.msg-row.mine .bubble { background: var(--grad); color: #fff; border-end-end-radius: 4px; white-space: pre-wrap; }
.msg-row:not(.mine) .bubble { background: var(--panel); border: 1px solid var(--line); border-end-start-radius: 4px; }
.img-bubble { padding: 8px; }
.img-bubble img { max-width: 100%; max-height: 420px; border-radius: 10px; display: block; }
.open-full { display: inline-block; margin-top: 6px; font-size: 13px; color: var(--muted); text-decoration: none; }
.open-full:hover { color: var(--text); }
.thinking { color: var(--muted); animation: pulse 1.2s infinite; }

/* markdown content */
.md h1, .md h2, .md h3 { margin: 14px 0 6px; line-height: 1.4; }
.md h1 { font-size: 20px; } .md h2 { font-size: 18px; } .md h3 { font-size: 16px; }
.md p { margin: 6px 0; }
.md ul, .md ol { margin: 6px 0; padding-inline-start: 22px; }
.md li { margin: 3px 0; }
.md code { background: rgba(255,255,255,0.09); padding: 2px 6px; border-radius: 6px; font-size: 13.5px; direction: ltr; unicode-bidi: embed; }
.md pre { margin: 0; }
.code-wrap { margin: 10px 0; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); direction: ltr; }
.code-head {
  display: flex; justify-content: space-between; align-items: center;
  background: #0a0817; padding: 6px 12px; font-size: 12px; color: var(--muted);
}
.copy-btn { color: var(--acc2); font-size: 12px; font-weight: 700; }
.code-wrap pre { background: #0d0a1f; padding: 12px 14px; overflow-x: auto; }
.code-wrap code { background: none; padding: 0; font-size: 13.5px; font-family: "Cascadia Code", "Fira Code", Consolas, monospace; line-height: 1.6; }
.tk-kw { color: #c792ea; } .tk-str { color: #7ec8a9; } .tk-com { color: #63668a; font-style: italic; } .tk-num { color: #f78c6c; }
.md blockquote { border-inline-start: 3px solid var(--acc); padding-inline-start: 12px; color: var(--muted); margin: 8px 0; }
.tbl-wrap { overflow-x: auto; margin: 8px 0; }
.md table { border-collapse: collapse; width: 100%; font-size: 14px; }
.md th, .md td { border: 1px solid var(--line); padding: 7px 10px; text-align: start; }
.md th { background: var(--panel2); }
.md a { color: var(--acc2); }

/* welcome */
#welcome {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px; text-align: center; padding: 20px;
  pointer-events: none;
}
#welcome > * { pointer-events: auto; }
#main { position: relative; }
.welcome-mark { width: 84px; height: 84px; font-size: 44px; border-radius: 24px; animation: float 3s ease-in-out infinite; }
@keyframes float { 50% { transform: translateY(-8px); } }
#welcome h1 { font-size: 26px; }
#welcome p { color: var(--muted); max-width: 420px; }
.chips { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; max-width: 520px; margin-top: 8px; }
.chip {
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  padding: 10px 16px; font-size: 14px; transition: var(--tr);
}
.chip:hover { border-color: var(--acc); transform: translateY(-2px); }

/* composer */
#composer { padding: 10px 16px 14px; background: linear-gradient(transparent, var(--bg) 30%); }
.composer-row {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--glass); border: 1px solid var(--line); border-radius: 20px;
  padding: 8px 10px; backdrop-filter: blur(12px);
  max-width: 900px; margin: 0 auto;
}
#composer > .mode-chip, #composer > .composer-note, #attach-preview { max-width: 900px; margin-inline: auto; }
#input {
  flex: 1; background: none; border: none; outline: none; resize: none;
  font-size: 15.5px; line-height: 1.6; max-height: 160px; padding: 8px 4px;
}
.send-btn {
  width: 42px; height: 42px; border-radius: 14px; background: var(--grad);
  color: #fff; font-size: 17px; display: inline-flex; align-items: center; justify-content: center;
  transition: var(--tr); flex-shrink: 0;
}
[dir="rtl"] .send-btn { transform: scaleX(-1); }
.send-btn:hover { filter: brightness(1.15); }
.send-btn.stop { background: var(--danger); transform: none; }
.mode-chip { font-size: 12.5px; color: var(--acc2); padding: 6px 4px 0; }
.composer-note { font-size: 11.5px; color: var(--muted); text-align: center; padding-top: 6px; }
#attach-preview { padding-bottom: 8px; }
.attach-chip {
  display: inline-flex; align-items: center; gap: 8px; background: var(--panel2);
  border-radius: 999px; padding: 6px 12px; font-size: 13px;
}
.attach-chip button { color: var(--muted); }

/* buttons */
.btn { border-radius: 12px; padding: 11px 18px; font-size: 15px; font-weight: 700; transition: var(--tr); }
.btn.primary { background: var(--grad); color: #fff; }
.btn.primary:hover { filter: brightness(1.15); }
.btn.ghost { background: var(--panel2); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.small { font-size: 13px; padding: 8px 12px; }

/* auth */
.auth {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(1000px 600px at 70% -10%, rgba(139, 92, 246, 0.25), transparent),
              radial-gradient(800px 500px at 10% 110%, rgba(6, 182, 212, 0.18), transparent), var(--bg);
  padding: 20px;
}
.glass { background: var(--glass); backdrop-filter: blur(16px); border: 1px solid var(--line); }
.auth-card {
  width: 100%; max-width: 380px; border-radius: 24px; padding: 30px 26px;
  display: flex; flex-direction: column; gap: 12px; text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.auth-logo { width: 64px; height: 64px; font-size: 34px; border-radius: 18px; margin: 0 auto; }
.auth-card h1 { font-size: 28px; }
.tagline { color: var(--muted); font-size: 14px; margin-bottom: 6px; }
.tabs { display: flex; background: var(--panel2); border-radius: 12px; padding: 4px; }
.tab { flex: 1; padding: 9px; border-radius: 9px; font-weight: 700; font-size: 14px; color: var(--muted); }
.tab.active { background: var(--grad); color: #fff; }
.auth-card input {
  background: var(--panel2); border: 1px solid var(--line); border-radius: 12px;
  padding: 13px 14px; font-size: 15px; outline: none; text-align: center;
}
.auth-card input:focus { border-color: var(--acc); }
.auth-err { color: var(--danger); font-size: 13px; min-height: 18px; }
.auth-note { font-size: 12px; color: var(--muted); }

/* modal & toast */
.overlay {
  position: fixed; inset: 0; background: rgba(5, 3, 15, 0.7); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.modal-card { border-radius: 20px; padding: 22px; max-width: 560px; width: 100%; max-height: 85dvh; overflow-y: auto; }
.modal-actions { display: flex; gap: 10px; margin-top: 16px; justify-content: flex-end; }
.about-card h2 { margin-bottom: 8px; }
.about-card h3 { margin: 14px 0 6px; color: var(--acc2); }
.about-card ul { padding-inline-start: 20px; line-height: 1.9; font-size: 14.5px; }
.about-card p { line-height: 1.8; font-size: 14.5px; color: var(--muted); }
.about-card .btn { margin-top: 14px; }
.toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%);
  background: var(--panel2); border: 1px solid var(--line); border-radius: 999px;
  padding: 10px 20px; font-size: 14px; z-index: 100; box-shadow: 0 8px 30px rgba(0,0,0,0.5);
}

#side-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 39; }

/* mobile */
.only-mobile { display: none; }
@media (max-width: 860px) {
  #sidebar {
    position: fixed; inset-inline-start: 0; top: 0; bottom: 0; z-index: 40;
    transform: translateX(100%); transition: transform 0.25s ease;
  }
  [dir="ltr"] #sidebar { transform: translateX(-100%); }
  #sidebar.open { transform: translateX(0); }
  .only-mobile { display: inline-flex; }
  .bubble { max-width: 94%; }
}
