/* SOREN_V2_Code — mobile-first app shell (skeleton)
   ریسپانسیو: mobile-first · safe-area-inset · اهداف لمسی ≥ ۴۴px
   RTL کامل برای FA + اسکرول افقی مستقل برای کد/Diff با dir="ltr" */

:root {
  --ink: #131110;
  --panel: #1e1a16;
  --panel-2: #241f1a;
  --line: rgba(238, 222, 192, 0.12);
  --text: #f0ebe0;
  --muted: #a79d8d;
  --brass: #d9a85c;
  --teal: #8fb3bd;
  --ok: #8fc39a;
  --danger: #d98d8d;
  --font-fa: "Vazirmatn", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, monospace;
  --touch: 44px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; overflow: hidden; }
body {
  margin: 0;
  background: var(--ink);
  color: var(--text);
  font-family: var(--font-fa);
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.sr-only:focus { position: fixed; width: auto; height: auto; clip-path: none; background: var(--panel); padding: .5rem 1rem; z-index: 99; }

/* ===== App grid: sidebar | main ===== */
.app-shell {
  display: grid;
  grid-template-areas: "topbar" "chat" "composer";
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
}
.sidebar {
  display: none; /* موبایل: drawer — TODO(P0) */
  grid-area: sidebar;
  background: var(--panel);
  border-inline-end: 1px solid var(--line);
}

@media (min-width: 1024px) {
  .app-shell {
    grid-template-areas: "sidebar topbar" "sidebar chat" "sidebar composer";
    grid-template-columns: 280px 1fr;
  }
  .sidebar { display: flex; flex-direction: column; }
  .menu-btn { display: none; }
}

/* ===== نوار بالا ===== */
.topbar {
  grid-area: topbar;
  display: flex; align-items: center; justify-content: space-between;
  gap: .5rem; padding: .5rem .75rem;
  min-height: var(--touch);
  border-bottom: 1px solid var(--line);
  background: rgba(19, 17, 16, .9); backdrop-filter: blur(8px);
}
.conversation-title { font-size: .95rem; margin: 0; font-weight: 700; }
.model-chip { font-family: var(--font-mono); font-size: .68rem; color: var(--teal); border: 1px solid var(--line); padding: .25rem .6rem; border-radius: 99px; }
.agent-status { font-size: .72rem; color: var(--muted); }

/* ===== محور چت ===== */
.chat-scroll { grid-area: chat; overflow-y: auto; padding: 1rem .75rem; }
.chat-empty { text-align: center; margin-top: 18vh; color: var(--muted); }
.empty-title { font-size: 1.4rem; font-weight: 800; color: var(--text); }

/* بلوک کد و Diff همیشه LTR و مستقل — صفحه هرگز افقی اسکرول نمی‌شود */
pre, code, .diff-preview { direction: ltr; text-align: left; unicode-bidi: embed; }
pre, .diff-preview { overflow-x: auto; }

/* ===== Composer ===== */
.composer { grid-area: composer; border-top: 1px solid var(--line); padding: .5rem .75rem calc(.5rem + env(safe-area-inset-bottom)); }
.composer-row { display: flex; gap: .5rem; align-items: flex-end; max-width: 56rem; margin-inline: auto; }
.composer select, .composer textarea, .composer button { font: inherit; min-height: var(--touch); }
#prompt-input {
  flex: 1; resize: none; max-height: 30vh;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: .9rem; padding: .65rem .9rem;
}
.send-btn, .stop-btn { width: var(--touch); border-radius: .9rem; border: 1px solid var(--line); background: var(--brass); color: #241503; font-weight: 800; cursor: pointer; }
.stop-btn { background: var(--danger); color: #2a0d0d; }

/* ===== دیالوگ‌ها (mobile: bottom sheet · desktop: centered) ===== */
.sheet { border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 1.1rem 1.1rem 0 0; width: 100%; max-width: 100%; margin: auto 0 0; padding: 0; }
.sheet::backdrop { background: rgba(0,0,0,.6); }
.sheet-inner { padding: 1.25rem; }
.sheet-actions { display: flex; justify-content: flex-end; gap: .5rem; padding: 0; margin: 1rem 0 0; list-style: none; }
.sheet-actions button { min-height: var(--touch); padding: 0 1.25rem; border-radius: .75rem; border: 1px solid var(--line); background: transparent; color: var(--text); }
.sheet-actions .primary { background: var(--brass); color: #241503; border-color: transparent; font-weight: 800; }
@media (min-width: 768px) {
  .sheet { border-radius: 1.1rem; width: min(36rem, 92vw); margin: auto; }
}

.hint { color: var(--muted); font-size: .8rem; }
.diff-preview { background: #100e0c; border: 1px solid var(--line); border-radius: .75rem; padding: .75rem; max-height: 40vh; font-family: var(--font-mono); font-size: .78rem; }
button:focus-visible, select:focus-visible, textarea:focus-visible, [tabindex]:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; scroll-behavior: auto !important; } }

/* ============ P0: پیام‌ها و drawer (موبایل) ============ */
.msg {
  max-width: 46rem;
  width: fit-content;
  margin: 0 auto .65rem;
  padding: .65rem .9rem;
  border-radius: 1rem;
  font-size: .92rem;
  line-height: 2;
  overflow-wrap: break-word;
}
.msg-user { margin-inline-start: auto; margin-inline-end: 0; background: #2b251d; border: 1px solid var(--line); border-start-end-radius: .3rem; }
.msg-assistant { margin-inline-start: 0; margin-inline-end: auto; background: var(--panel); border: 1px solid var(--line); border-start-start-radius: .3rem; }
.msg-stopped { border-color: #d9a85c44; }
.msg-error { margin-inline-start: 0; margin-inline-end: auto; background: #2a1518; border: 1px solid #d98d8d55; color: #e7b9b9; border-radius: 1rem; padding: .75rem .9rem; }
.retry-btn { margin-top: .4rem; min-height: 40px; padding: 0 1rem; border-radius: .7rem; border: 1px solid #d98d8d88; background: transparent; color: #f0d3d3; cursor: pointer; }

.msg p { margin: .15rem 0; }
.msg ul { margin: .25rem 1.25rem .25rem 0; padding: 0; }
.msg li { margin: .15rem 0; }
.msg strong { color: #eed49a; }
.msg a { color: var(--teal); }
.msg code { font-family: var(--font-mono); font-size: .85em; background: #100e0c; border: 1px solid var(--line); border-radius: .35rem; padding: .08em .38em; direction: ltr; unicode-bidi: embed; display: inline-block; }
.msg pre { background: #100e0c; border: 1px solid var(--line); border-radius: .75rem; padding: .75rem; margin: .5rem 0; overflow-x: auto; }
.msg pre code { background: transparent; border: none; padding: 0; display: block; }
.msg h3,.msg h4,.msg h5,.msg h6 { margin: .5rem 0 .25rem; }
.caret { color: var(--brass); }

/* آیتم‌های تاریخچه */
.conversation-list { flex: 1; overflow-y: auto; padding: .5rem; }
.conversation-item {
  display: flex; align-items: center; gap: .5rem; width: 100%;
  text-align: start; padding: .5rem .65rem; min-height: var(--touch);
  border-radius: .75rem; border: 1px solid transparent; background: transparent;
  color: var(--text); cursor: pointer; font: inherit;
}
.conversation-item:hover { background: var(--panel-2); }
.conversation-item.active { border-color: #d9a85c66; background: #262019; }
.conv-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .85rem; }
.conversation-item time { font-size: .65rem; color: var(--muted); flex-shrink: 0; }
.delete-btn { margin-inline-start: .15rem; opacity: .55; font-size: .85rem; flex-shrink: 0; border-radius: .5rem; padding: .2rem .3rem; }
.delete-btn:hover { opacity: 1; background: #2a1518; }
.sidebar-header { display: flex; align-items: center; justify-content: space-between; padding: .75rem; border-bottom: 1px solid var(--line); }
.brand { font-family: var(--font-mono); font-weight: 600; font-size: .8rem; color: var(--brass); letter-spacing: .03em; }
.icon-btn { min-width: var(--touch); min-height: var(--touch); border: 1px solid var(--line); background: transparent; color: var(--text); border-radius: .75rem; cursor: pointer; font-size: 1.1rem; }
.settings-btn { margin: .5rem; min-height: var(--touch); border: 1px solid var(--line); background: transparent; color: var(--muted); border-radius: .75rem; cursor: pointer; }
.empty-state { color: var(--muted); text-align: center; font-size: .85rem; padding: 1rem; }

/* drawer زیر ۱۰۲۴px */
@media (max-width: 1023px) {
  .sidebar {
    display: flex; flex-direction: column;
    position: fixed; top: 0; bottom: 0; inset-inline-start: 0; z-index: 40;
    width: min(300px, 86vw);
    border-inline-end: 1px solid var(--line);
    box-shadow: 0 0 60px rgba(0,0,0,.6);
    transform: translateX(105%); /* RTL: از راست وارد می‌شود */
    transition: transform .35s cubic-bezier(.22,1,.36,1);
    visibility: hidden;
  }
  [dir="ltr"] .sidebar { transform: translateX(-105%); }
  .sidebar.open { transform: translateX(0); visibility: visible; }
  .sidebar-overlay { position: fixed; inset: 0; z-index: 35; background: rgba(0,0,0,.6); }
}
[hidden] { display: none !important; }

/* ===== فرم تنظیمات Provider (P1) ===== */
.field { display: flex; flex-direction: column; gap: .35rem; margin-bottom: .75rem; }
.field label { font-size: .8rem; color: var(--muted); font-weight: 600; }
.field input, .field select {
  min-height: var(--touch); padding: 0 .75rem; border-radius: .6rem;
  border: 1px solid var(--line); background: var(--bg); color: var(--text); font-size: 1rem;
}
.field input:focus-visible, .field select:focus-visible { outline: 2px solid var(--brass); outline-offset: 1px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.link-btn { align-self: flex-start; background: none; border: none; color: var(--brass); padding: .25rem 0; text-decoration: underline; cursor: pointer; font-size: .85rem; min-height: 2.75rem; }
#key-state[data-state="set"] { color: var(--brass); }
@media (max-width: 480px) { .field-row { grid-template-columns: 1fr; } }

/* ===== فعالیت Agent (P2) ===== */
.agent-activity { font-size: .8rem; border: 1px solid var(--line); border-radius: .6rem; padding: .5rem .75rem; margin: .35rem 0; background: color-mix(in srgb, var(--panel) 60%, transparent); }
.agent-line { padding: .15rem 0; color: var(--muted); }
.agent-line code { font-size: .75rem; }
.agent-tool { color: var(--brass); font-weight: 700; }
.agent-ok { color: #7dbb7e; }
.agent-denied { color: #d98a8a; }
.agent-line em { color: var(--brass); font-style: normal; }
