:root {
  --bg: #f6f1e8;
  --surface: #ffffff;
  --surface-2: #fbf8f2;
  --sidebar-bg: #f1eadd;
  --line: #e8e0d2;
  --line-2: #ded4c2;
  --text: #2f2a24;
  --muted: #8c8276;
  --accent: #c2632f;
  --accent-d: #a85120;
  --accent-soft: #f7e9e0;
  --think: #b07a4a;
  --tool: #9a7b53;
  --error: #c0432e;
  --ok: #4a8a4a;
  --side-w: 286px;
  --radius: 14px;
  --shadow: 0 18px 50px rgba(80, 50, 20, 0.12);
  --shadow-sm: 0 2px 10px rgba(80, 50, 20, 0.05);
  font-size: 16px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overscroll-behavior: none; }
body {
  background: var(--bg); color: var(--text);
  font-family: "Vazirmatn", "IRANSans", "Segoe UI", system-ui, -apple-system, "Tahoma", sans-serif;
  line-height: 1.8; -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.ic { width: 1.15em; height: 1.15em; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; display: block; }

button { font-family: inherit; cursor: pointer; border: none; border-radius: 10px; transition: background .15s, color .15s, border-color .15s, opacity .15s; }
button.primary { background: var(--accent); color: #fff; padding: 0.7rem 1.1rem; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 0.45rem; }
button.primary:hover { background: var(--accent-d); }
button.ghost { background: transparent; color: var(--accent); padding: 0.55rem 1rem; border: 1px solid var(--line-2); }
button.ghost:hover { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
.icon-btn { background: transparent; color: var(--muted); width: 38px; height: 38px; display: inline-flex; align-items: center; justify-content: center; border-radius: 10px; font-size: 1.15rem; }
.icon-btn:hover { background: rgba(0,0,0,0.05); color: var(--text); }

/* Logo / wordmark */
.logo-row { display: flex; align-items: center; gap: 0.55rem; justify-content: center; }
.logo { width: 44px; height: 44px; display: block; }
.logo.sm { width: 28px; height: 28px; }
.logo.lg { width: 60px; height: 60px; opacity: 0.9; }
.wordmark { font-size: 1.7rem; font-weight: 800; letter-spacing: -0.5px; color: var(--text); }
.wordmark.sm { font-size: 1.15rem; }

/* ===== Auth ===== */
.auth-screen { min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1.4rem; padding: 1rem; }
.auth-card { width: min(92vw, 380px); background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 2.2rem 1.8rem; display: flex; flex-direction: column; gap: 0.7rem; box-shadow: var(--shadow); }
.auth-tagline { text-align: center; color: var(--muted); margin: 0.2rem 0 1rem; font-size: 0.98rem; }
.auth-card input { background: var(--surface-2); border: 1px solid var(--line-2); color: var(--text); padding: 0.78rem 0.95rem; border-radius: 11px; font-size: 1rem; font-family: inherit; }
.auth-card input:focus { outline: none; border-color: var(--accent); background: #fff; }
.auth-card .primary, .auth-card .ghost { margin-top: 0.2rem; }
.auth-error { color: var(--error); font-size: 0.9rem; min-height: 1.1rem; text-align: center; }
.signup-note { color: var(--muted); font-size: 0.85rem; text-align: center; margin: 0.2rem 0 0; }
.auth-foot { color: var(--muted); font-size: 0.8rem; }

/* ===== App shell ===== */
.app { display: flex; height: 100dvh; overflow: hidden; }
.backdrop { position: fixed; inset: 0; background: rgba(40,30,15,0.4); opacity: 0; pointer-events: none; transition: opacity .25s; z-index: 25; }

/* Sidebar collapses by animating width; inner keeps a fixed width so content doesn't squish. */
.sidebar { flex: 0 0 var(--side-w); width: var(--side-w); background: var(--sidebar-bg); border-inline-start: 1px solid var(--line); overflow: hidden; transition: width .25s ease, flex-basis .25s ease; }
.side-inner { width: var(--side-w); height: 100%; display: flex; flex-direction: column; gap: 0.8rem; padding: 0.9rem 0.85rem; }
.app.collapsed .sidebar { flex-basis: 0; width: 0; }

.side-head { display: flex; align-items: center; justify-content: space-between; padding: 0.2rem 0.3rem; }
.only-mobile { display: none; }
.new-chat { width: 100%; }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 0.2rem; margin-top: 0.2rem; }
.chat-item { display: flex; align-items: center; gap: 0.5rem; padding: 0.55rem 0.65rem; border-radius: 10px; cursor: pointer; color: var(--text); border: 1px solid transparent; }
.chat-item:hover { background: #fff; border-color: var(--line); }
.chat-item.active { background: var(--accent-soft); border-color: #eccdbb; }
.chat-ic { color: var(--muted); flex: 0 0 auto; width: 1rem; height: 1rem; }
.chat-item.active .chat-ic { color: var(--accent); }
.chat-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 0.92rem; flex: 1; }
.chat-del { width: 26px; height: 26px; font-size: 0.9rem; opacity: 0; flex: 0 0 auto; }
.chat-item:hover .chat-del { opacity: 1; }
.chat-del:hover { color: var(--error); }
.sidebar-foot { display: flex; align-items: center; gap: 0.55rem; padding-top: 0.6rem; border-top: 1px solid var(--line); }
.avatar-mini { width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: #fff; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.85rem; flex: 0 0 auto; }
.who { color: var(--text); font-size: 0.9rem; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ===== Main ===== */
.main { position: relative; display: flex; flex-direction: column; flex: 1; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 0.5rem; padding: 0.55rem 0.7rem; border-bottom: 1px solid var(--line); background: rgba(246,241,232,0.85); backdrop-filter: blur(8px); z-index: 5; }
.active-title { flex: 1; text-align: center; font-weight: 600; font-size: 0.98rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text); }

/* Empty state */
.empty { position: absolute; inset: 56px 0 120px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.4rem; text-align: center; color: var(--muted); pointer-events: none; padding: 2rem; }
.empty h2 { color: var(--text); font-weight: 800; margin: 0.6rem 0 0; font-size: clamp(1.4rem, 3.5vw, 2rem); }
.empty p { margin: 0; }

/* Thread */
.thread { flex: 1; overflow-y: auto; padding: 1.4rem clamp(0.8rem, 5vw, 3rem); display: flex; flex-direction: column; gap: 1.3rem; scroll-behavior: smooth; }
.turn { display: flex; flex-direction: column; gap: 0.7rem; max-width: 820px; width: 100%; margin: 0 auto; }
/* RTL chat: user's own messages hug the RIGHT, assistant hugs the LEFT.
   `justify-content: right` is physical (dir-independent), so it stays correct in RTL.
   The assistant row is laid out LTR (avatar on the far left, content to its right),
   while the content itself is switched back to RTL for correct Persian text. */
.user-row { display: flex; justify-content: right; }
.bubble.user-msg { background: var(--accent); color: #fff; padding: 0.65rem 1rem; border-radius: 18px 18px 4px 18px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; box-shadow: var(--shadow-sm); }

.assistant-row { display: flex; gap: 0.6rem; align-items: flex-start; direction: ltr; }
.avatar { width: 30px; height: 30px; flex: 0 0 auto; border-radius: 8px; background: var(--surface); border: 1px solid var(--line); padding: 3px; }
.assistant { direction: rtl; display: flex; flex-direction: column; gap: 0.5rem; min-width: 0; max-width: 86%; }
.assistant-name { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.elapsed { font-weight: 400; font-size: 0.76rem; color: var(--muted); margin-inline-start: 0.45rem; }
.elapsed:not(:empty)::before { content: "⏱ "; opacity: 0.7; }
.elapsed.running { color: var(--think); }

.thinking { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 0.35rem 0.85rem; }
.thinking summary { cursor: pointer; color: var(--think); font-size: 0.88rem; user-select: none; display: flex; align-items: center; gap: 0.45rem; list-style: none; }
.thinking summary::-webkit-details-marker { display: none; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--think); display: inline-block; animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100% { opacity: 0.35; transform: scale(0.8);} 50% { opacity: 1; transform: scale(1.1);} }
.thinking-text { white-space: pre-wrap; overflow-wrap: anywhere; color: var(--muted); font-size: 0.86rem; margin: 0.5rem 0 0.2rem; max-height: 240px; overflow-y: auto; font-family: inherit; }

.tools { display: flex; flex-direction: column; gap: 0.3rem; }
.tool { background: var(--surface); border: 1px solid var(--line); border-inline-start: 3px solid var(--tool); border-radius: 8px; padding: 0.38rem 0.7rem; font-size: 0.84rem; color: var(--muted); }
.tool .tool-name { color: var(--accent); font-weight: 600; }
.tool.err { border-inline-start-color: var(--error); }

.answer { background: var(--surface); border: 1px solid var(--line); border-radius: 4px 16px 16px 16px; padding: 0.85rem 1.1rem; overflow-wrap: anywhere; box-shadow: var(--shadow-sm); }
.answer:empty { display: none; }
.answer.error { color: var(--error); border-color: #e8c4bb; background: var(--accent-soft); white-space: pre-wrap; }

/* Rendered Markdown inside answers */
.answer > :first-child { margin-top: 0; }
.answer > :last-child { margin-bottom: 0; }
.answer p { margin: 0.5rem 0; }
.answer h3 { font-size: 1.12rem; font-weight: 800; margin: 0.85rem 0 0.4rem; }
.answer h4, .answer h5, .answer h6 { font-size: 1rem; font-weight: 700; margin: 0.7rem 0 0.3rem; }
.answer ul, .answer ol { margin: 0.5rem 0; padding-inline-start: 1.5rem; }
.answer li { margin: 0.2rem 0; }
.answer blockquote { margin: 0.6rem 0; padding: 0.25rem 0.9rem; border-inline-start: 3px solid var(--line-2); color: var(--muted); }
.answer a { color: var(--accent); text-decoration: underline; word-break: break-all; }
.answer code { background: var(--surface-2); border: 1px solid var(--line); border-radius: 5px; padding: 0.03rem 0.32rem; font-size: 0.86em; font-family: "SF Mono", ui-monospace, "Courier New", monospace; }
.answer pre { background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 0.7rem 0.9rem; overflow-x: auto; direction: ltr; text-align: left; }
.answer pre code { background: none; border: none; padding: 0; font-size: 0.85em; }
.answer table { border-collapse: collapse; margin: 0.6rem 0; font-size: 0.9em; display: block; overflow-x: auto; max-width: 100%; }
.answer th, .answer td { border: 1px solid var(--line-2); padding: 0.4rem 0.65rem; text-align: start; }
.answer thead th { background: var(--surface-2); font-weight: 700; }
.answer tbody tr:nth-child(even) { background: var(--surface-2); }

/* Copy buttons on bubbles (hover-revealed) */
.copy-btn { background: transparent; color: var(--muted); width: 28px; height: 26px; border-radius: 8px; display: inline-flex; align-items: center; justify-content: center; opacity: 0; transition: opacity .15s, color .15s, background .15s; flex: 0 0 auto; }
.copy-btn .ic { width: 1rem; height: 1rem; }
.copy-btn:hover { background: rgba(0,0,0,0.05); color: var(--accent); }
.copy-btn.copied { color: var(--ok); opacity: 1; }
.ans-copy { align-self: flex-start; margin-top: -0.15rem; }
.user-row .copy-btn { align-self: center; }
.user-row:hover .copy-btn, .assistant-row:hover .copy-btn { opacity: 1; }
@media (hover: none) { .copy-btn { opacity: 1; } } /* touch: always visible */

/* Composer */
.composer { padding: 0.6rem clamp(0.8rem, 5vw, 3rem) 0.8rem; background: linear-gradient(to top, var(--bg) 70%, transparent); }
.composer-box { max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: 0.4rem; background: var(--surface); border: 1px solid var(--line-2); border-radius: 26px; padding: 0.35rem 0.4rem 0.35rem 0.4rem; box-shadow: var(--shadow-sm); transition: border-color .15s; }
.composer-box:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; resize: none; background: transparent; border: none; color: var(--text); padding: 0.55rem 0.8rem; font-size: 1rem; font-family: inherit; max-height: 180px; line-height: 1.6; }
.composer textarea:focus { outline: none; }
.send { background: var(--accent); color: #fff; width: 40px; height: 40px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto; }
.send:hover { background: var(--accent-d); }
.send:disabled { opacity: 0.45; }
.disclaimer { max-width: 820px; margin: 0.4rem auto 0; text-align: center; color: var(--muted); font-size: 0.72rem; }

/* ===== Mobile ===== */
@media (max-width: 760px) {
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 30; width: min(84vw, 300px); flex-basis: min(84vw,300px); box-shadow: var(--shadow); transform: translateX(100%); transition: transform .25s ease; }
  [dir="rtl"] .sidebar { transform: translateX(100%); }
  .app.menu-open .sidebar { transform: translateX(0); }
  .app.menu-open .backdrop { opacity: 1; pointer-events: auto; }
  .only-mobile { display: inline-flex; }
  .app.collapsed .sidebar { width: min(84vw,300px); flex-basis: min(84vw,300px); } /* ignore desktop collapse on mobile */
  .turn { max-width: 100%; }
}
@media (min-width: 761px) {
  .backdrop { display: none; }
}
