:root {
  --bg: #f4f5fa; --card: #ffffff; --text: #18202f; --muted: #6a7286; --line: #e6e8f0;
  --side: #ffffff; --side-text: #3b4458; --side-hover: #f2f3f9;
  --brand: #5a48e0; --brand-2: #8b5cf6; --brand-soft: #eeebfe; --on-brand: #ffffff;
  --ok: #0f8a52; --ok-soft: #e2f6ec; --warn: #9a5c00; --warn-soft: #fff2dc; --bad: #c62f3c; --bad-soft: #fde7ea;
  --me: #eeebfe; --them: #f1f3f8; --human: #e2f6ec; --bar: #5a48e0; --grid: #eceef4;
  --shadow: 0 1px 2px rgba(20, 24, 40, .04), 0 4px 16px rgba(20, 24, 40, .05);
  --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117; --card: #181b23; --text: #e9ebf1; --muted: #9aa1b3; --line: #262a35;
    --side: #14161d; --side-text: #c3c8d6; --side-hover: #1e212b;
    --brand: #8b7cff; --brand-2: #a78bfa; --brand-soft: #262150; --on-brand: #ffffff;
    --ok: #4fd08f; --ok-soft: #14301f; --warn: #f0b04a; --warn-soft: #362a12; --bad: #ff6b78; --bad-soft: #3a181d;
    --me: #262150; --them: #222632; --human: #14301f; --bar: #8b7cff; --grid: #232733;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.75 Vazirmatn, Tahoma, sans-serif; }
button, input, select, textarea { font: inherit; color: inherit; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.hidden { display: none !important; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); font-size: 13px; }
h1, h2, h3 { line-height: 1.4; }
code { background: var(--bg); border: 1px solid var(--line); padding: 0 6px; border-radius: 6px; direction: ltr; unicode-bidi: embed; font-size: 12.5px; }
bdi { unicode-bidi: isolate; }

/* ---------- controls ---------- */
input[type=text], input[type=password], input[type=number], select, textarea {
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; min-width: 0;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
button:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
textarea { width: 100%; min-height: 84px; resize: vertical; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 0; border-radius: 10px;
  padding: 9px 18px; cursor: pointer; background: var(--brand); color: var(--on-brand); font-weight: 600; transition: filter .15s, transform .05s; }
.btn:hover { filter: brightness(1.07); }
.btn:active { transform: translateY(1px); }
.btn.ghost { background: var(--brand-soft); color: var(--brand); }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.btn.small { padding: 4px 12px; font-size: 13px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: wait; }
.btn .ico { width: 18px; height: 18px; }
.icon-only { display: inline-grid; place-items: center; width: 38px; height: 38px; border: 0; border-radius: 10px; background: none; color: var(--muted); cursor: pointer; }
.icon-only:hover { background: var(--side-hover); color: var(--text); }
.pill, .badge { display: inline-block; padding: 1px 10px; border-radius: 99px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.pill.ok, .badge.ok { background: var(--ok-soft); color: var(--ok); }
.pill.warn, .badge.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad, .badge.bad { background: var(--bad-soft); color: var(--bad); }
.pill.off { background: var(--them); color: var(--muted); }
.pill.brand { background: var(--brand-soft); color: var(--brand); }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.stack { display: grid; gap: 10px; }
.field { display: grid; gap: 4px; font-size: 14px; }
.toggle { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.error { color: var(--bad); font-size: 14px; min-height: 1.4em; margin-bottom: 0; }
.empty { color: var(--muted); text-align: center; padding: 28px 0; }
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 12px; z-index: 50; max-width: 90vw; box-shadow: var(--shadow); }

/* ---------- brand & auth ---------- */
.brand { display: flex; align-items: center; gap: 10px; }
.brand b { display: block; font-size: 16px; }
.brand small { display: block; color: var(--muted); font-size: 12px; margin-top: -2px; }
.logo { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 11px; color: #fff; font-weight: 700;
  background: linear-gradient(135deg, var(--brand), var(--brand-2)); box-shadow: 0 4px 12px rgba(90, 72, 224, .35); }
.brand.big .logo { width: 46px; height: 46px; font-size: 20px; }
.brand.big b { font-size: 19px; }
.auth-wrap { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.auth { width: 100%; max-width: 410px; padding: 28px; }
.auth .brand { margin-bottom: 16px; }

/* ---------- shell: sidebar on the right (RTL start) ---------- */
.shell { display: grid; grid-template-columns: 264px minmax(0, 1fr); min-height: 100%; }
.sidebar { position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; background: var(--side);
  border-inline-end: 1px solid var(--line); padding: 16px 12px; z-index: 40; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 0 6px 18px; }
#tabs { display: flex; flex-direction: column; gap: 2px; overflow-y: auto; flex: 1; }
#tabs button { display: flex; align-items: center; gap: 12px; width: 100%; border: 0; background: none; text-align: start;
  padding: 10px 12px; border-radius: 11px; cursor: pointer; color: var(--side-text); font-weight: 500; transition: background .15s, color .15s; }
#tabs button span:first-of-type { flex: 1; }
#tabs button:hover { background: var(--side-hover); color: var(--text); }
#tabs button.on { background: var(--brand-soft); color: var(--brand); font-weight: 700; }
#tabs button.on svg { stroke-width: 2.1; }
.nav-sep { font-size: 12px; color: var(--muted); padding: 18px 12px 6px; }
.side-foot { display: flex; align-items: center; gap: 10px; border-top: 1px solid var(--line); padding: 14px 6px 2px; margin-top: 10px; }
.side-foot b { display: block; font-size: 14px; direction: ltr; text-align: right; }
.side-foot small { color: var(--muted); font-size: 12px; }
.avatar { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); font-weight: 700; }
.backdrop { display: none; }
.content { min-width: 0; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 10px; height: 64px; padding: 0 28px;
  background: color-mix(in srgb, var(--bg) 85%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.topbar h1 { font-size: 19px; margin: 0; }
main { padding: 24px 28px 48px; width: 100%; max-width: 1280px; }
.show-mobile { display: none; }

/* ---------- cards & layout ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 18px; box-shadow: var(--shadow); }
.card h2 { font-size: 16px; margin: 0 0 14px; }
.card h3 { font-size: 14px; margin: 18px 0 8px; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.grid2 > .card { margin-bottom: 0; }
.grid2 + .card, .card + .grid2 { margin-top: 18px; }
.page-tools { margin-bottom: 14px; }
ol.steps { margin: 0 0 14px; padding-inline-start: 20px; }
.status-box { margin-bottom: 12px; line-height: 2; }
.hero { display: flex; gap: 18px; align-items: center; flex-wrap: wrap;
  background: linear-gradient(135deg, var(--brand-soft), var(--card) 70%); }
.hero h2 { font-size: 18px; margin-bottom: 4px; }
.hero p { margin: 0; }

/* ---------- dashboard ---------- */
.tiles { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 14px; margin-bottom: 18px; }
.tile { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.tile b { display: block; font-size: 25px; line-height: 1.35; margin: 2px 0; }
.tile span { color: var(--muted); font-size: 13px; }
.chart { position: relative; height: 230px; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; stroke: none; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; font-family: Vazirmatn, Tahoma, sans-serif; }
.chart .bar { fill: var(--bar); }
.chart .hit { fill: transparent; }
.chart .col:hover .bar { opacity: .75; }
.chart-note { margin: 6px 0 0; }
.tip { position: absolute; pointer-events: none; background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 12px; font-size: 13px; box-shadow: 0 8px 24px rgba(0,0,0,.14); white-space: nowrap; z-index: 3; line-height: 1.8; }
.reason { display: grid; grid-template-columns: minmax(0, 1fr) 140px 32px; gap: 10px; align-items: center; padding: 5px 0; }
.reason .track { background: var(--bg); border-radius: 4px; height: 8px; overflow: hidden; }
.reason .fill { background: var(--bar); height: 100%; border-radius: 4px; }

.meter { margin: 6px 0 14px; }
.meter .top { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; color: var(--text); }
.meter .track { height: 10px; border-radius: 99px; background: var(--bg); overflow: hidden; margin-top: 6px; border: 1px solid var(--line); }
.meter .fill { height: 100%; border-radius: 99px; background: var(--bar); }
.meter.warn .fill { background: var(--warn); }
.meter.bad .fill { background: var(--bad); }

/* ---------- chat bubbles (shared) ---------- */
.msgs { display: flex; flex-direction: column; gap: 10px; overflow-y: auto; padding: 4px; }
.msg { max-width: 78%; padding: 9px 14px; border-radius: 16px; word-wrap: break-word; }
.msg .text { white-space: pre-wrap; }
.msg.user { background: var(--them); align-self: flex-start; border-start-start-radius: 4px; }
.msg.assistant { background: var(--me); align-self: flex-end; border-start-end-radius: 4px; }
.msg.support { background: var(--human); align-self: flex-end; border-start-end-radius: 4px; }
.msg .who { display: block; font-size: 12px; font-weight: 700; color: var(--muted); }
.msg .meta { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; font-size: 11.5px; color: var(--muted); margin-top: 6px; }
.msg .fix { margin-top: 6px; background: var(--card); border-radius: 8px; padding: 6px 10px; font-size: 13px; }
.msg.typing { color: var(--muted); }
.icon-btn { border: 1px solid var(--line); background: var(--card); border-radius: 8px; padding: 0 9px; cursor: pointer; font-size: 12.5px; line-height: 24px; }
.icon-btn:hover { border-color: var(--brand); }
.icon-btn.on { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); }

/* ---------- test page ---------- */
.chat-card { display: flex; flex-direction: column; height: calc(100vh - 64px - 24px - 48px); min-height: 480px; margin-bottom: 0; }
.chat-head { align-items: flex-start; border-bottom: 1px solid var(--line); padding-bottom: 12px; margin-bottom: 12px; }
.chat-head h2 { margin-bottom: 2px; }
.chat-head p { margin: 0; }
.test-msgs { flex: 1; min-height: 0; }
.test-msgs:empty::before { content: "سؤال خودتان را بنویسید یا یکی از نمونه‌های پایین را بزنید 👇"; margin: auto; color: var(--muted); text-align: center; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0 2px; }
.chips button { border: 1px solid var(--line); background: var(--card); border-radius: 99px; padding: 5px 14px; cursor: pointer; white-space: nowrap; font-size: 13px; color: var(--side-text); }
.chips button:hover { border-color: var(--brand); color: var(--brand); }
.composer { display: flex; gap: 8px; padding-top: 10px; }
.composer input { flex: 1; padding: 12px 14px; }

/* ---------- chats page ---------- */
.chats { display: grid; grid-template-columns: 330px minmax(0, 1fr); gap: 18px; align-items: start; }
.chats > .card { margin-bottom: 0; }
.chat-list { max-height: calc(100vh - 140px); overflow-y: auto; padding: 10px; }
.chat-list input { width: 100%; margin-bottom: 8px; }
.chat-row { padding: 10px 12px; border-radius: 12px; cursor: pointer; }
.chat-row:hover { background: var(--side-hover); }
.chat-row.on { background: var(--brand-soft); }
.chat-row .last { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chatView .msgs { max-height: calc(100vh - 290px); }

/* ---------- lists ---------- */
.item { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px; margin-bottom: 12px; }
.card .item { box-shadow: none; }
#handoffs .item, #learned .item { box-shadow: var(--shadow); }
.item dl { display: grid; grid-template-columns: 110px minmax(0, 1fr); gap: 3px 12px; margin: 10px 0 0; }
.item dt { color: var(--muted); }
.item dd { margin: 0; white-space: pre-wrap; }
.item.open { border-inline-start: 4px solid var(--bad); }
.item label { display: block; margin-top: 8px; }
.qa { white-space: pre-wrap; }
.qa b { color: var(--muted); font-weight: 500; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: start; padding: 8px; border-top: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 500; border-top: 0; }
td.data { color: var(--muted); font-size: 12px; direction: ltr; text-align: left; word-break: break-all; max-width: 360px; }
.lvl-error { color: var(--bad); font-weight: 700; }
.lvl-warn { color: var(--warn); font-weight: 700; }
.secret { font-size: 16px; letter-spacing: 2px; word-break: break-all; }

/* ---------- responsive ---------- */
@media (max-width: 1200px) { .tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 980px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; width: 280px; max-width: 86vw; transform: translateX(100%);
    transition: transform .22s ease; box-shadow: none; }
  .shell.menu-open .sidebar { transform: none; box-shadow: -12px 0 40px rgba(0,0,0,.18); }
  .shell.menu-open .backdrop { display: block; position: fixed; inset: 0; background: rgba(10, 12, 20, .45); z-index: 35; }
  .show-mobile { display: inline-grid; }
  .topbar { padding: 0 12px; }
  main { padding: 16px 16px 40px; }
  .grid2, .chats { grid-template-columns: minmax(0, 1fr); }
  .grid2 > .card, .chats > .card { margin-bottom: 0; }
  .chat-list { max-height: 40vh; }
  #chatView .msgs { max-height: 60vh; }
  .chat-card { height: calc(100vh - 64px - 16px - 24px); }
}
@media (max-width: 620px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .tile b { font-size: 21px; }
  .card { padding: 16px; }
  .msg { max-width: 92%; }
  .reason { grid-template-columns: minmax(0, 1fr) 70px 28px; }
  .item dl { grid-template-columns: 92px minmax(0, 1fr); }
  .topbar h1 { font-size: 17px; }
  .chat-head select, .chat-head .btn { flex: 1; }
  .chat-head #testContact { width: 100% !important; }
  .hero .row { width: 100%; }
  .hero .btn { flex: 1; }
}
