:root {
  --bg: #1c1b1a;
  --bg-2: #232220;
  --panel: #2a2826;
  --panel-2: #33312e;
  --border: #3d3a36;
  --text: #ece8e1;
  --muted: #a49d93;
  --accent: #e2a76f;
  --accent-ink: #1c1b1a;
  --danger: #e5675f;
  --ok: #7fc28b;
  --user-bubble: #34312d;
  --code-bg: #161514;
  --radius: 14px;
  --font: "Segoe UI", "Noto Sans Arabic", "Noto Sans", Tahoma, system-ui, -apple-system, sans-serif;
  /* Arabic glyphs fall through to a proportional Arabic font so letters stay connected inside code */
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Consolas, "Liberation Mono", "Segoe UI", "Noto Sans Arabic", Tahoma, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15.5px/1.75 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.error-text { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }

.btn {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 14px; cursor: pointer; transition: background .15s, border-color .15s;
}
.btn:hover { background: #3c3935; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px; flex: none;
  background: transparent; border: 0; border-radius: 10px; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

/* ---------- Login ---------- */
.login-view { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: min(380px, 100%); background: var(--bg-2); border: 1px solid var(--border);
  border-radius: 20px; padding: 32px 28px; display: grid; gap: 14px; text-align: center;
}
.login-card h1 { margin: 0; font-size: 24px; }
.login-card p { margin: 0; }
.login-card label { display: grid; gap: 6px; text-align: start; font-size: 14px; color: var(--muted); }
.login-card input {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.logo-mark { font-size: 34px; color: var(--accent); line-height: 1; }
.logo-mark.big { font-size: 52px; }

/* ---------- Layout ---------- */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar {
  width: 280px; flex: none; background: var(--bg-2); border-inline-start: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-top { padding: 12px; display: grid; gap: 10px; }
.new-chat { display: flex; align-items: center; justify-content: center; gap: 8px; font-weight: 600; }
.search {
  width: 100%; background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 7px 12px; outline: none; font-size: 14px;
}
.search:focus { border-color: var(--accent); }
.conv-list { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }
.conv-group { font-size: 12px; color: var(--muted); padding: 12px 8px 4px; }
.conv-item {
  display: flex; align-items: center; gap: 4px; border-radius: 10px; padding: 0 4px 0 0;
  cursor: pointer; color: var(--text);
}
.conv-item .title { flex: 1; min-width: 0; padding: 8px 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.conv-item:hover, .conv-item.active { background: var(--panel); }
.conv-item.active { background: var(--panel-2); }
.conv-item .icon-btn { width: 28px; height: 28px; opacity: 0; }
.conv-item:hover .icon-btn, .conv-item.active .icon-btn { opacity: 1; }
.sidebar-bottom { border-top: 1px solid var(--border); padding: 10px 12px 12px; display: grid; gap: 10px; }
.usage { font-size: 13px; display: grid; gap: 6px; }
.usage-row { display: flex; justify-content: space-between; gap: 8px; }
#usage-user, #usage-total { direction: ltr; unicode-bidi: isolate; }
.bar { height: 6px; background: var(--panel-2); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .4s; }
.bar i.warn { background: var(--danger); }
.user-row { display: flex; align-items: center; gap: 8px; }
.avatar {
  width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-weight: 700; flex: none; text-transform: uppercase;
}
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 30; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--border);
  background: var(--bg); min-height: 58px;
}
.conv-title { flex: 1; min-width: 0; margin: 0; font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.controls select {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 6px 10px;
  font-size: 13.5px; outline: none; cursor: pointer; max-width: 210px;
}
#model-select { direction: ltr; text-align: left; }
.chip-toggle {
  display: inline-flex; align-items: center; gap: 6px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 999px; padding: 5px 12px; font-size: 13px; cursor: pointer; color: var(--muted);
}
.chip-toggle[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); background: rgba(226,167,111,.1); }
.only-mobile { display: none; }
.mock-banner { background: #3b2f1c; color: #f3d19c; text-align: center; font-size: 13px; padding: 6px; }

/* ---------- Messages ---------- */
.messages { flex: 1; overflow-y: auto; padding: 24px 16px 8px; scroll-behavior: auto; }
.msg { max-width: 820px; margin: 0 auto 22px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-start; }
.msg.user .bubble {
  background: var(--user-bubble); border-radius: 18px; padding: 10px 16px; max-width: 85%;
  white-space: pre-wrap; word-wrap: break-word; unicode-bidi: plaintext; text-align: start;
}
.msg.user .att-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; max-width: 85%; }
.msg .actions { display: flex; gap: 2px; margin-top: 4px; opacity: .0; transition: opacity .15s; align-items: center; flex-wrap: wrap; }
.msg:hover .actions, .msg:focus-within .actions, .msg.last .actions { opacity: 1; }
.msg .actions .icon-btn { width: 30px; height: 30px; }
.msg .meta { font-size: 12px; color: var(--muted); margin-inline-start: 6px; direction: ltr; unicode-bidi: isolate; }

.att {
  display: inline-flex; align-items: center; gap: 8px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 10px; padding: 6px 10px; font-size: 13px; max-width: 260px; position: relative;
}
.att img { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; flex: none; }
.att.image-only { padding: 0; border: 0; background: none; }
.att.image-only img { width: auto; height: auto; max-width: 220px; max-height: 220px; border-radius: 12px; cursor: zoom-in; }
.att .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: ltr; unicode-bidi: isolate; }
.att .sub { font-size: 11px; color: var(--muted); }
.file-ico {
  width: 36px; height: 36px; border-radius: 8px; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-size: 11px; font-weight: 700; flex: none; text-transform: uppercase;
}

.assistant .body { word-wrap: break-word; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md li, .md h1, .md h2, .md h3, .md h4, .md h5, .md h6, .md blockquote, .md td, .md th {
  unicode-bidi: plaintext; text-align: start;
}
.md h1 { font-size: 1.45em; } .md h2 { font-size: 1.3em; } .md h3 { font-size: 1.15em; }
.md h1, .md h2, .md h3, .md h4 { margin: 1.1em 0 .5em; line-height: 1.4; }
.md p { margin: .6em 0; }
.md ul, .md ol { padding-inline-start: 1.6em; margin: .5em 0; }
.md li { margin: .25em 0; }
.md blockquote { margin: .8em 0; padding: .2em 1em; border-inline-start: 3px solid var(--accent); color: #d6d0c6; background: var(--bg-2); border-radius: 6px; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1.2em 0; }
.md code { font-family: var(--mono); font-size: .88em; background: var(--panel-2); padding: .12em .4em; border-radius: 6px; direction: ltr; unicode-bidi: isolate; }
.md .code-block { margin: .9em 0; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; direction: ltr; }
.md .code-head { display: flex; justify-content: space-between; align-items: center; padding: 5px 10px 5px 14px; background: var(--panel); font-size: 12px; color: var(--muted); font-family: var(--mono); }
.md pre { margin: 0; padding: 14px 16px; overflow-x: auto; background: var(--code-bg); text-align: left; line-height: 1.55; }
.md pre code { background: none; padding: 0; font-size: 13.5px; }
.md pre code.hljs { background: none; padding: 0; }
.copy-code { display: inline-flex; gap: 5px; align-items: center; background: none; border: 0; color: var(--muted); cursor: pointer; font-family: var(--font); font-size: 12px; padding: 3px 6px; border-radius: 6px; }
.copy-code:hover { color: var(--text); background: var(--panel-2); }
.md .table-wrap { overflow-x: auto; margin: .8em 0; border: 1px solid var(--border); border-radius: 10px; }
.md table { border-collapse: collapse; width: 100%; font-size: 14px; }
.md th, .md td { border-bottom: 1px solid var(--border); padding: 8px 12px; }
.md th { background: var(--panel); font-weight: 600; }
.md tr:last-child td { border-bottom: 0; }
.md img { max-width: 100%; border-radius: 8px; }
.md a { word-break: break-word; }

.thinking { margin: 0 0 10px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg-2); }
.thinking summary { cursor: pointer; padding: 7px 12px; font-size: 13px; color: var(--muted); list-style: none; display: flex; align-items: center; gap: 8px; }
.thinking summary::-webkit-details-marker { display: none; }
.thinking summary::before { content: "◂"; transition: transform .15s; font-size: 11px; }
.thinking[open] summary::before { transform: rotate(-90deg); }
.thinking .think-body { padding: 0 14px 10px; font-size: 13.5px; color: #c9c2b8; white-space: pre-wrap; unicode-bidi: plaintext; max-height: 360px; overflow-y: auto; }
.tool-chip { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); background: var(--bg-2); border: 1px solid var(--border); border-radius: 999px; padding: 3px 12px; margin: 2px 0 8px; }
.tool-chip bdi { color: var(--text); }
.notice { font-size: 13px; padding: 8px 12px; border-radius: 10px; background: #3a2e22; color: #f0cfa0; margin: 8px 0; }
.notice.error { background: #3d2222; color: #f3b1ab; }
.sources { margin-top: 12px; font-size: 13px; }
.sources summary { cursor: pointer; color: var(--muted); }
.sources ol { margin: 6px 0; padding-inline-start: 1.5em; }
.sources a { direction: ltr; unicode-bidi: isolate; }
.cursor::after { content: "▍"; color: var(--accent); animation: blink 1s steps(2) infinite; margin-inline-start: 2px; }
@keyframes blink { 50% { opacity: 0; } }
.dots { display: inline-flex; gap: 4px; padding: 8px 0; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: bounce 1.2s infinite; }
.dots i:nth-child(2) { animation-delay: .15s; } .dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 80%, 100% { opacity: .3; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-4px); } }

.edit-box { width: min(100%, 680px); display: grid; gap: 8px; }
.edit-box textarea { width: 100%; min-height: 90px; background: var(--panel); border: 1px solid var(--accent); border-radius: 12px; padding: 10px 14px; resize: vertical; outline: none; unicode-bidi: plaintext; }
.edit-box .row { display: flex; gap: 8px; }

/* ---------- Empty state ---------- */
.empty { position: absolute; inset: 58px 0 150px; display: grid; place-content: center; text-align: center; pointer-events: none; padding: 16px; }
.empty h1 { margin: 10px 0 4px; font-size: 28px; font-weight: 600; }

/* ---------- Composer ---------- */
.composer { padding: 8px 16px calc(10px + env(safe-area-inset-bottom)); max-width: 852px; width: 100%; margin: 0 auto; }
.composer-row {
  display: flex; align-items: flex-end; gap: 6px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 20px; padding: 8px;
}
.composer-row:focus-within { border-color: #5a554e; }
#input {
  flex: 1; min-width: 0; resize: none; border: 0; outline: none; background: transparent; padding: 7px 6px;
  max-height: 260px; line-height: 1.6; unicode-bidi: plaintext;
}
.send-btn {
  width: 38px; height: 38px; border-radius: 12px; border: 0; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; cursor: pointer; flex: none;
}
.send-btn:disabled { opacity: .4; cursor: not-allowed; }
.send-btn .i-stop { display: none; }
.send-btn.stop .i-send { display: none; }
.send-btn.stop .i-stop { display: block; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.attachments:empty { display: none; }
.att .remove {
  position: absolute; top: -7px; inset-inline-end: -7px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text); cursor: pointer; font-size: 12px; line-height: 1;
  display: grid; place-items: center; padding: 0;
}
.att .progress { position: absolute; bottom: 0; inset-inline-start: 0; height: 3px; background: var(--accent); border-radius: 3px; transition: width .2s; }
.att.failed { border-color: var(--danger); }
.hint { text-align: center; font-size: 11.5px; margin: 6px 0 0; }
.drop-overlay {
  position: absolute; inset: 12px; border: 2px dashed var(--accent); border-radius: 20px; background: rgba(28,27,26,.85);
  display: grid; place-items: center; font-size: 20px; color: var(--accent); z-index: 20; pointer-events: none;
}

/* ---------- Modal / toast ---------- */
.modal { border: 1px solid var(--border); border-radius: 18px; background: var(--bg-2); color: var(--text); padding: 0; width: min(560px, 94vw); max-height: 90dvh; overflow-y: auto; }
.modal::backdrop { background: rgba(0,0,0,.6); }
.modal-body { padding: 22px; display: grid; gap: 14px; }
.modal h3 { margin: 0; }
.modal label { display: grid; gap: 6px; font-size: 14px; }
.modal textarea, .modal select { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; outline: none; }
.modal textarea { resize: vertical; unicode-bidi: plaintext; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.box { border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; display: grid; gap: 6px; }
.box p { margin: 0; }
.danger-box { border-color: #5a3330; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row.end { justify-content: flex-start; }
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 10px 18px; z-index: 100; box-shadow: 0 8px 30px rgba(0,0,0,.4); max-width: 90vw;
}
.toast.error { border-color: var(--danger); color: #f3b1ab; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 200; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 94vh; border-radius: 10px; }

/* ---------- Mobile ---------- */
@media (max-width: 860px) {
  .only-mobile { display: inline-grid; }
  .sidebar {
    position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 40; transform: translateX(100%);
    transition: transform .2s ease; width: min(300px, 86vw); box-shadow: 0 0 40px rgba(0,0,0,.5);
  }
  .sidebar.open { transform: translateX(0); }
  .topbar { flex-wrap: wrap; padding: 8px 10px; gap: 6px; }
  .conv-title { font-size: 14px; }
  .controls { width: 100%; }
  .controls select { flex: 1; max-width: none; min-width: 0; }
  .messages { padding: 16px 12px 8px; }
  .msg.user .bubble, .msg.user .att-row { max-width: 92%; }
  .empty { inset: 100px 0 150px; }
  .composer { padding-inline: 10px; }
  .msg .actions { opacity: 1; }
  .conv-item .icon-btn { opacity: 1; }
  .grid2 { grid-template-columns: 1fr; }
}
