/* =====================================================================
   توکن‌های طراحی — نسخه‌ی مینیمال و حرفه‌ای.
   دو تم (تیره/روشن) با متغیرهای CSS؛ سوییچ با data-theme روی <html>.
   ===================================================================== */
:root {
  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 9px;
  --font-ui: 'Vazirmatn', system-ui, -apple-system, sans-serif;
  --font-mono: ui-monospace, 'JetBrains Mono', 'Courier New', monospace;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --app-height: 100vh;
}

html[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #16181f;
  --surface-2: #1d2029;
  --surface-3: #262a35;
  --border: #2a2e39;
  --text: #eceef2;
  --text-dim: #8b909d;
  --text-faint: #5c6070;
  --accent: #5b8def;
  --accent-ink: #ffffff;
  --accent-soft: #1a2740;
  --success: #34d399;
  --success-soft: #12271f;
  --danger: #f2685c;
  --danger-soft: #2a1717;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

html[data-theme="light"] {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-2: #f0f1f3;
  --surface-3: #e7e8ec;
  --border: #e2e3e8;
  --text: #16181f;
  --text-dim: #676c7a;
  --text-faint: #9a9fac;
  --accent: #3b6fe0;
  --accent-ink: #ffffff;
  --accent-soft: #e8effd;
  --success: #16a672;
  --success-soft: #e4f7ef;
  --danger: #d9463a;
  --danger-soft: #fbe9e7;
  --shadow: 0 10px 30px rgba(20, 20, 30, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  direction: rtl;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

body { font-size: 15px; line-height: 1.65; }
button, input, textarea { font-family: inherit; }
svg { display: block; }

#app {
  max-width: 520px;
  margin: 0 auto;
  height: var(--app-height);
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--bg);
  overflow: hidden;
  transition: opacity 0.16s ease;
}
#app.view-fade-out { opacity: 0.35; }

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.topbar h1 {
  font-size: 16px;
  font-weight: 700;
  margin: 0;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar .topbar-sub { font-size: 11.5px; color: var(--text-dim); font-weight: 400; }

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:active { background: var(--surface-2); }

.content {
  flex: 1;
  overflow-y: auto;
  padding: 14px 14px calc(20px + var(--safe-bottom));
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  margin-bottom: 10px;
}
.card-title { font-weight: 700; font-size: 14.5px; margin: 0 0 3px; }
.card-sub { color: var(--text-dim); font-size: 12.5px; margin: 0; }

.project-card { display: flex; align-items: center; gap: 12px; cursor: pointer; }
.project-card:active { background: var(--surface-2); }

.avatar {
  width: 38px; height: 38px;
  border-radius: var(--radius-sm);
  background: var(--surface-3);
  color: var(--text);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 15px; flex-shrink: 0;
  border: 1px solid var(--border);
}

.status-chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11.5px; font-family: var(--font-mono);
  padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border);
}
.status-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-dim); flex-shrink: 0; }
.status-chip.running .status-dot { background: var(--accent); animation: pulse 1.4s ease-in-out infinite; }
.status-chip.running { color: var(--accent); }
.status-chip.completed { color: var(--success); }
.status-chip.completed .status-dot { background: var(--success); }
.status-chip.failed .status-dot, .status-chip.stopped_budget .status-dot, .status-chip.cancelled .status-dot { background: var(--danger); }
.status-chip.failed, .status-chip.stopped_budget, .status-chip.cancelled { color: var(--danger); }

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(91, 141, 239, 0.5); }
  50% { box-shadow: 0 0 0 5px rgba(91, 141, 239, 0); }
}

.fab {
  position: absolute;
  bottom: calc(20px + var(--safe-bottom));
  right: 18px;
  width: 52px; height: 52px;
  border-radius: 50%;
  background: var(--accent); color: var(--accent-ink);
  border: none; display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow); cursor: pointer; z-index: 15;
}
.fab svg { width: 24px; height: 24px; }
.fab:active { transform: scale(0.94); }

/* درِیوِر (منوی همبرگری) — از سمت راست باز می‌شود (RTL) */
.drawer-overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45);
  z-index: 60; opacity: 0; transition: opacity 0.18s ease;
}
.drawer-overlay.open { opacity: 1; }

.drawer {
  position: fixed; top: 0; right: 0; bottom: 0;
  width: min(84vw, 340px);
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.2s ease;
  z-index: 61;
  padding-top: var(--safe-top);
}
.drawer-overlay.open .drawer { transform: translateX(0); }

.drawer-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 10px; border-bottom: 1px solid var(--border);
}
.drawer-header .brand { font-weight: 800; font-size: 15px; }

.drawer-section { padding: 10px 8px; overflow-y: auto; }
.drawer-section + .drawer-section { border-top: 1px solid var(--border); }
.drawer-label { font-size: 11px; color: var(--text-faint); padding: 6px 10px 4px; font-weight: 700; letter-spacing: 0.02em; }

.drawer-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 10px; border-radius: var(--radius-sm);
  color: var(--text); cursor: pointer; font-size: 13.5px;
}
.drawer-item svg { width: 18px; height: 18px; color: var(--text-dim); flex-shrink: 0; }
.drawer-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drawer-item:active { background: var(--surface-2); }
.drawer-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.drawer-item.active svg { color: var(--accent); }

.drawer-footer { padding: 10px 8px calc(12px + var(--safe-bottom)); border-top: 1px solid var(--border); }

.theme-switch-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px; font-size: 13px; color: var(--text-dim);
}
.switch {
  width: 40px; height: 24px; border-radius: 999px;
  background: var(--surface-3); border: 1px solid var(--border);
  position: relative; cursor: pointer; flex-shrink: 0;
}
.switch .knob {
  position: absolute; top: 2px; right: 2px;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--text-dim);
  transition: transform 0.18s ease, background 0.18s ease;
}
.switch.on { background: var(--accent-soft); border-color: var(--accent); }
.switch.on .knob { transform: translateX(-16px); background: var(--accent); }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12.5px; color: var(--text-dim); margin-bottom: 6px; }
.field input, .field textarea {
  width: 100%; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 11px 13px; color: var(--text);
  font-size: 14.5px; outline: none;
}
.field input:focus, .field textarea:focus { border-color: var(--accent); }
.field textarea { resize: vertical; min-height: 70px; }

.btn {
  display: block; width: 100%; padding: 12px; border-radius: var(--radius-sm);
  border: none; font-size: 14.5px; font-weight: 700; cursor: pointer; text-align: center;
}
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:active { opacity: 0.85; }
.btn-ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger-soft); }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { flex: 1; }
.btn-icon { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn-icon svg { width: 16px; height: 16px; }

.error-box {
  background: var(--danger-soft); border: 1px solid var(--danger); color: var(--danger);
  border-radius: var(--radius-sm); padding: 10px 13px; font-size: 12.5px; margin-bottom: 12px;
}

.auth-screen { min-height: var(--app-height); display: flex; flex-direction: column; justify-content: center; padding: 24px; }
.auth-logo {
  width: 56px; height: 56px; border-radius: var(--radius-md); background: var(--accent);
  display: flex; align-items: center; justify-content: center; margin: 0 auto 18px;
}
.auth-logo svg { width: 28px; height: 28px; color: #fff; }
.auth-title { text-align: center; font-size: 19px; font-weight: 800; margin: 0 0 4px; }
.auth-sub { text-align: center; color: var(--text-dim); font-size: 12.5px; margin: 0 0 24px; }
.auth-toggle { text-align: center; margin-top: 14px; color: var(--text-dim); font-size: 12.5px; }
.auth-toggle a { color: var(--accent); text-decoration: none; }

.chat-tab { display: flex; flex-direction: column; flex: 1; min-height: 0; overflow: hidden; }
.msg-list { display: flex; flex-direction: column; gap: 8px; }
.chat-messages { flex: 1; overflow-y: auto; padding: 14px; min-height: 0; }
.msg {
  max-width: 86%; padding: 10px 13px; border-radius: var(--radius-md);
  font-size: 13.8px; line-height: 1.7; white-space: pre-wrap; word-break: break-word;
  direction: rtl; text-align: right; unicode-bidi: isolate;
}
/* پیام‌های خودت همیشه سمت راست، پیام‌های مدل همیشه سمت چپ — بدون توجه
   به اینکه متن با کلمه‌ی فارسی شروع بشه یا انگلیسی. */
.msg.user { align-self: flex-start; background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-end; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg-meta { font-size: 10px; opacity: 0.75; margin-top: 5px; font-family: var(--font-mono); direction: ltr; text-align: right; }

.typing-indicator {
  align-self: flex-end;
  display: flex; align-items: center; gap: 4px;
  padding: 12px 15px; border-radius: var(--radius-md); border-bottom-left-radius: 4px;
  background: var(--surface); border: 1px solid var(--border);
}
.typing-indicator span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint);
  animation: typingBounce 1.2s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.15s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.plan-card { border-color: var(--accent); }
.plan-files { font-family: var(--font-mono); font-size: 11.5px; color: var(--text-dim); margin: 9px 0; }
.plan-files div { padding: 3px 0; }

.attach-chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--surface-3); border: 1px solid var(--border); border-radius: 999px;
  padding: 5px 6px 5px 10px; font-size: 12px; color: var(--text-dim); margin: 6px 6px 0 0;
}
.attach-chip svg { width: 14px; height: 14px; }
.attach-chip button { background: none; border: none; color: var(--text-faint); cursor: pointer; display: flex; padding: 2px; }
.attach-row { padding: 0 12px; display: flex; flex-wrap: wrap; }

.composer { flex-shrink: 0; border-top: 1px solid var(--border); background: var(--bg); padding: 8px 10px calc(8px + var(--safe-bottom)); }
.composer-row {
  display: flex; align-items: flex-end; gap: 6px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 22px; padding: 5px 6px;
}
.composer-row textarea {
  flex: 1; background: transparent; border: none; padding: 8px 6px; color: var(--text);
  font-size: 14.3px; resize: none; max-height: 120px; outline: none; line-height: 1.5;
}
.composer-btn {
  width: 34px; height: 34px; border-radius: 50%; border: none; background: transparent;
  color: var(--text-dim); flex-shrink: 0; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.composer-btn svg { width: 19px; height: 19px; }
.composer-btn:active { background: var(--surface-3); }
.send-btn { background: var(--accent); color: var(--accent-ink); }
.send-btn:disabled { opacity: 0.4; }
.send-btn svg { width: 18px; height: 18px; }

.model-pill {
  display: flex; align-items: center; gap: 4px; padding: 0 8px 0 4px; height: 34px;
  border-radius: 999px; background: transparent; border: none; color: var(--text-dim);
  font-size: 11.5px; cursor: pointer; flex-shrink: 0; white-space: nowrap;
}
.model-pill svg { width: 14px; height: 14px; }

.composer-toolbar { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px 2px; }
.composer-toolbar-left { display: flex; align-items: center; gap: 2px; }

.popover-overlay { position: fixed; inset: 0; z-index: 70; background: transparent; }
.popover {
  position: fixed; min-width: 230px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-md); box-shadow: var(--shadow); padding: 6px; z-index: 71;
  opacity: 0; transform: translateY(-4px); transition: opacity 0.14s ease, transform 0.14s ease;
}
.popover.shown { opacity: 1; transform: translateY(0); }
.popover-label { font-size: 10.5px; color: var(--text-faint); padding: 8px 10px 4px; font-weight: 700; }
.popover-item {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 9px 10px; border-radius: var(--radius-sm); cursor: pointer; font-size: 13px;
}
.popover-item:active { background: var(--surface-2); }
.popover-item .popover-item-main { display: flex; flex-direction: column; gap: 1px; }
.popover-item .popover-item-desc { font-size: 11px; color: var(--text-dim); }
.popover-item svg.check { width: 16px; height: 16px; color: var(--accent); flex-shrink: 0; }
.popover-divider { height: 1px; background: var(--border); margin: 6px 2px; }
.popover-link { font-size: 12px; color: var(--accent); padding: 8px 10px; cursor: pointer; }

.file-row {
  display: flex; align-items: center; gap: 10px; padding: 11px 4px;
  border-bottom: 1px solid var(--border); font-family: var(--font-mono); font-size: 12.5px;
}
.file-row .file-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.file-row svg.file-icon { width: 16px; height: 16px; color: var(--text-dim); flex-shrink: 0; }
.file-path-dir { color: var(--text-dim); }
.file-actions { display: flex; gap: 2px; flex-shrink: 0; }
.file-actions .icon-btn { width: 30px; height: 30px; }
.file-actions .icon-btn svg { width: 16px; height: 16px; }

.code-viewer { position: fixed; inset: 0; background: var(--bg); z-index: 55; display: flex; flex-direction: column; }
.code-viewer pre {
  flex: 1; margin: 0; padding: 0; overflow: auto; font-family: var(--font-mono);
  font-size: 12px; line-height: 1.7; direction: ltr; text-align: left;
}
.code-viewer pre code.hljs {
  display: block; padding: 14px; white-space: pre-wrap; word-break: break-word; background: transparent !important;
}
.code-viewer .topbar h1 { font-family: var(--font-mono); font-size: 12.5px; direction: ltr; text-align: right; }

.stat-row { display: flex; justify-content: space-between; align-items: center; padding: 9px 0; border-bottom: 1px solid var(--border); }
.stat-row:last-child { border-bottom: none; }
.stat-label { color: var(--text-dim); font-size: 12.5px; }
.stat-value { font-family: var(--font-mono); font-weight: 700; font-size: 13.5px; }

.budget-bar-track { height: 7px; background: var(--surface-3); border-radius: 999px; overflow: hidden; margin-top: 8px; }
.budget-bar-fill { height: 100%; background: var(--accent); border-radius: 999px; transition: width 0.4s ease; }

.empty-state { text-align: center; padding: 50px 20px; color: var(--text-dim); }
.empty-state svg { width: 36px; height: 36px; margin-bottom: 10px; opacity: 0.5; }

.loading-spin {
  width: 20px; height: 20px; border: 3px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.7s linear infinite; margin: 30px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; bottom: calc(88px + var(--safe-bottom)); left: 50%; transform: translateX(-50%);
  background: var(--surface-3); border: 1px solid var(--border); color: var(--text);
  padding: 10px 16px; border-radius: 999px; font-size: 12.5px; z-index: 80;
  box-shadow: var(--shadow); max-width: 88vw; text-align: center;
}
