:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #eeece7;
  --text: #1f1e1c;
  --muted: #6d6a64;
  --border: #e1ded7;
  --accent: #c15f3c;
  --accent-ink: #ffffff;
  --accent-soft: #f6e4dc;
  --ok: #2e8b57;
  --warn: #a86b12;
  --warn-soft: #fbf0dc;
  --err: #c0392b;
  --err-soft: #fbe5e2;
  --code-bg: #f0eee9;
  --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 16px rgb(0 0 0 / .06);
  --radius: 14px;
  --font: "Vazirmatn", "Vazir", Tahoma, "Segoe UI", system-ui, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, "Courier New", monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191817;
    --surface: #242321;
    --surface-2: #2e2c29;
    --text: #ecebe7;
    --muted: #a29e97;
    --border: #3a3834;
    --accent: #d9774f;
    --accent-ink: #1a1310;
    --accent-soft: #3b2a22;
    --ok: #4cbb7f;
    --warn: #e0a64a;
    --warn-soft: #3a2f1d;
    --err: #ef6b5e;
    --err-soft: #3d2320;
    --code-bg: #1f1e1c;
    --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 6px 20px rgb(0 0 0 / .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15.5px/1.75 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }

/* ---------- buttons */
.btn {
  border: 1px solid transparent;
  border-radius: 10px;
  padding: .55em 1.1em;
  cursor: pointer;
  background: var(--surface-2);
  transition: filter .15s, background .15s, opacity .15s;
  touch-action: manipulation;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.danger { background: var(--err); color: #fff; }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.block { display: block; width: 100%; }
.btn:not(:disabled):active { filter: brightness(.92); }
.icon-btn {
  display: grid; place-items: center;
  width: 42px; height: 42px;
  border: 0; border-radius: 10px;
  background: transparent; cursor: pointer; color: var(--text);
}
.icon-btn:active { background: var(--surface-2); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 28px 22px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px;
}
.login-icon { width: 48px; height: 48px; }
.login-card h1 { margin: 0; font-size: 1.35rem; }
.login-card p { margin: 0; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: .92rem; color: var(--muted); }
.login-card input {
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg); padding: .7em .85em; font-size: 16px; color: var(--text);
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.form-error { color: var(--err); min-height: 1.5em; font-size: .92rem; }

/* ---------- app shell */
.app { height: 100dvh; display: flex; flex-direction: column; }
.top {
  display: flex; align-items: center; gap: 6px;
  padding: max(8px, env(safe-area-inset-top)) 8px 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.top-mid { flex: 1; min-width: 0; text-align: center; }
.top-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.agent { font-size: .78rem; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.agent .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.agent.on .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.agent.off .dot { background: var(--err); }

/* ---------- drawer */
.scrim { position: fixed; inset: 0; background: rgb(0 0 0 / .35); z-index: 20; }
.drawer {
  position: fixed; inset-block: 0; inset-inline-start: 0;
  width: min(320px, 86vw);
  background: var(--surface);
  border-inline-end: 1px solid var(--border);
  z-index: 30;
  display: flex; flex-direction: column; gap: 10px;
  padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
  transform: translateX(100%);
  transition: transform .22s ease;
  visibility: hidden;
}
.drawer.open { transform: none; visibility: visible; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; font-weight: 600; padding-inline-start: 6px; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv-list li {
  display: flex; align-items: center; gap: 4px;
  border-radius: 10px; padding: 2px 2px 2px 0;
}
.conv-list li.active { background: var(--surface-2); }
.conv-open {
  flex: 1; min-width: 0; text-align: start;
  background: none; border: 0; cursor: pointer; padding: 8px 10px;
}
.conv-open .t { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-open .s { display: block; font-size: .75rem; color: var(--muted); }
.conv-open .busy { color: var(--accent); }
.conv-del { width: 34px; height: 34px; color: var(--muted); }
.conv-empty { color: var(--muted); padding: 12px; font-size: .9rem; }

/* ---------- thread */
.scroller { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 16px 16px 8px; }
.thread { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 12px; }
.empty-state { text-align: center; color: var(--muted); padding: 12vh 8px 0; }
.empty-state h2 { color: var(--text); font-size: 1.15rem; margin: 0 0 6px; }
.empty-state p { margin: 0 0 18px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: .4em .95em; cursor: pointer; font-size: .9rem;
}

.msg.user { align-self: flex-start; max-width: 88%; }
.msg.user .bubble {
  background: var(--accent-soft);
  border-radius: var(--radius); border-start-start-radius: 4px;
  padding: .55em .9em; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant { overflow-wrap: anywhere; }
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant p { margin: 0 0 .6em; }
.msg.assistant h3, .msg.assistant h4, .msg.assistant h5, .msg.assistant h6 { margin: .9em 0 .35em; line-height: 1.4; }
.msg.assistant ul, .msg.assistant ol { margin: 0 0 .6em; padding-inline-start: 1.4em; }
.msg.assistant blockquote { margin: 0 0 .6em; padding-inline-start: .8em; border-inline-start: 3px solid var(--border); color: var(--muted); }
.msg.assistant hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.msg.assistant a { color: var(--accent); }
code {
  font-family: var(--mono); font-size: .86em;
  background: var(--code-bg); border: 1px solid var(--border);
  border-radius: 5px; padding: .05em .35em; direction: ltr; unicode-bidi: isolate;
}
pre {
  font-family: var(--mono); font-size: .82rem; line-height: 1.55;
  background: var(--code-bg); border: 1px solid var(--border);
  border-radius: 10px; padding: .75em .9em; margin: 0 0 .6em;
  overflow-x: auto; white-space: pre; direction: ltr; text-align: left;
}
pre code { background: none; border: 0; padding: 0; font-size: inherit; }
.table-wrap { overflow-x: auto; margin: 0 0 .6em; }
table { border-collapse: collapse; font-size: .9rem; min-width: 60%; }
th, td { border: 1px solid var(--border); padding: .35em .6em; text-align: start; vertical-align: top; }
th { background: var(--surface-2); }

/* tool activity */
.tool { font-size: .84rem; color: var(--muted); }
.tool summary {
  display: flex; align-items: center; gap: 8px; cursor: pointer; list-style: none;
  padding: 2px 0;
}
.tool summary::-webkit-details-marker { display: none; }
.tool .tname {
  flex: none; font-weight: 600; color: var(--text);
  background: var(--surface-2); border-radius: 6px; padding: 0 .5em; font-size: .78rem;
}
.tool .targ {
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-family: var(--mono); font-size: .78rem; direction: ltr; unicode-bidi: plaintext;
}
.tool .tstate { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.tool.done .tstate { background: var(--ok); }
.tool.err .tstate { background: var(--err); }
.tool pre { margin: 6px 0 2px; max-height: 320px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; font-size: .76rem; }

/* permission card */
.perm {
  background: var(--warn-soft);
  border: 1px solid color-mix(in srgb, var(--warn) 45%, transparent);
  border-radius: var(--radius); padding: 12px;
  display: flex; flex-direction: column; gap: 8px;
}
.perm-title { font-weight: 600; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.badge { font-size: .78rem; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 0 .5em; }
.perm pre { margin: 0; max-height: 260px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; background: var(--surface); }
.perm-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.perm-btns .btn { flex: 1 1 auto; }
.perm-state { font-size: .86rem; color: var(--muted); }
.perm.yes { background: var(--surface); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.perm.no, .perm.expired { background: var(--surface); border-color: var(--border); }

.result { font-size: .78rem; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.result::before, .result::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.result.bad { color: var(--err); }
.error-box {
  background: var(--err-soft); color: var(--err);
  border-radius: 10px; padding: .55em .85em; font-size: .9rem;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.note { font-size: .8rem; color: var(--muted); text-align: center; }

.working {
  max-width: 760px; margin: 6px auto 0; color: var(--muted); font-size: .88rem;
  display: flex; align-items: center; gap: 10px;
}
.working.waiting { color: var(--warn); }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .3; animation: blink 1.2s infinite; }
.dots i:nth-child(2) { animation-delay: .2s; }
.dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 40% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .dots i { animation: none; opacity: .7; } .drawer { transition: none; } }

/* ---------- composer */
.composer {
  padding: 8px 12px max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.conn-banner {
  max-width: 760px; margin: 0 auto 8px; font-size: .82rem; text-align: center;
  background: var(--warn-soft); color: var(--warn); border-radius: 8px; padding: 4px 8px;
}
.composer-box {
  max-width: 760px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 4px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px; padding: 6px;
  box-shadow: var(--shadow);
}
.composer-box.drag { border-color: var(--accent); border-style: dashed; }
.attach { flex: none; color: var(--muted); }

/* pending attachments */
.tray {
  max-width: 760px; margin: 0 auto 8px;
  display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px;
}
.chip-file {
  position: relative; flex: none;
  display: flex; align-items: center; gap: 8px;
  max-width: 220px; padding: 6px 8px; padding-inline-end: 30px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  font-size: .8rem; overflow: hidden;
}
.chip-file.error { border-color: var(--err); }
.chip-file .thumb, .file-card .ficon {
  flex: none; width: 36px; height: 36px; border-radius: 8px;
  background: var(--surface-2); display: grid; place-items: center;
  object-fit: cover; color: var(--muted); font-size: .62rem; font-weight: 700; text-transform: uppercase;
  overflow: hidden;
}
.chip-file .meta, .file-card .meta { min-width: 0; display: flex; flex-direction: column; line-height: 1.4; }
.chip-file .n, .file-card .n { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; direction: ltr; unicode-bidi: plaintext; text-align: start; }
.chip-file .s, .file-card .s { color: var(--muted); font-size: .74rem; }
.chip-file.error .s { color: var(--err); }
.chip-file .x {
  position: absolute; inset-inline-end: 2px; top: 50%; transform: translateY(-50%);
  width: 26px; height: 26px; border: 0; border-radius: 50%; background: transparent; color: var(--muted); cursor: pointer;
}
.chip-file .bar { position: absolute; inset-inline: 0; bottom: 0; height: 3px; background: var(--accent); transform-origin: left; transition: transform .2s; }
[dir="rtl"] .chip-file .bar { transform-origin: right; }

/* files in the thread */
.files { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.msg.user .files { align-items: flex-start; }
.file-card {
  display: flex; align-items: center; gap: 10px;
  max-width: min(100%, 360px); padding: 8px 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  font-size: .86rem;
}
.file-card .meta { flex: 1; }
.file-card .dl { flex: none; padding: .35em .8em; font-size: .82rem; text-decoration: none; color: var(--accent-ink); }
.file-img { display: block; max-width: min(100%, 360px); border-radius: 12px; border: 1px solid var(--border); overflow: hidden; }
.file-img img { display: block; width: 100%; height: auto; max-height: 420px; object-fit: contain; background: var(--surface-2); }
.file-caption { font-size: .86rem; color: var(--muted); }
.file-from { font-size: .8rem; color: var(--muted); margin-bottom: 2px; }
.composer-box:focus-within { border-color: color-mix(in srgb, var(--accent) 60%, var(--border)); }
#input {
  flex: 1; min-width: 0; border: 0; outline: none; resize: none; background: transparent;
  max-height: 40vh; padding: 7px 4px; font-size: 16px; line-height: 1.6;
}
.send { width: 42px; height: 42px; padding: 0; border-radius: 12px; display: grid; place-items: center; flex: none; }
.auto-row {
  max-width: 760px; margin: 8px auto 0; display: flex; align-items: center; gap: 8px;
  font-size: .82rem; color: var(--muted); cursor: pointer; user-select: none;
}
.auto-row input { width: 16px; height: 16px; accent-color: var(--err); margin: 0; }
.auto-row:has(input:checked) { color: var(--err); }

.toast {
  position: fixed; inset-inline: 16px; bottom: calc(110px + env(safe-area-inset-bottom));
  margin: 0 auto; max-width: 420px; z-index: 50;
  background: var(--text); color: var(--bg);
  border-radius: 10px; padding: .6em 1em; font-size: .9rem; text-align: center;
  box-shadow: var(--shadow);
}
