:root {
  --bg: #f4f6fa; --card: #fff; --ink: #1c2333; --muted: #6b7489; --line: #e3e7ef;
  --brand: #2f6fed; --brand-ink: #fff; --ok: #1f9d55; --bad: #d64545; --warn: #b7791f;
  --in: #fff; --out: #dcebff; --radius: 12px;
  --font: Vazirmatn, "Vazir", Tahoma, "Segoe UI", system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151c; --card: #1b2030; --ink: #e8ecf5; --muted: #98a2b8; --line: #2a3144;
    --brand: #5b93ff; --brand-ink: #0b1220; --in: #232a3d; --out: #25406b;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 14px/1.7 var(--font); }
a { color: var(--brand); text-decoration: none; }
h1 { font-size: 22px; margin: 6px 0 18px; }
h2 { font-size: 16px; margin: 0 0 12px; }
code, .code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; }
.code { background: var(--bg); border: 1px dashed var(--line); padding: 8px 10px; border-radius: 8px; word-break: break-all; margin: 8px 0 12px; }
.muted { color: var(--muted); } .small { font-size: 12px; } .center { text-align: center; }

/* ---- Top bar ---- */
.topbar { display: flex; align-items: center; gap: 18px; padding: 0 18px; height: 56px; background: var(--card);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.brand { font-weight: 700; white-space: nowrap; }
.topbar nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.topbar nav a { color: var(--ink); padding: 6px 12px; border-radius: 8px; white-space: nowrap; }
.topbar nav a:hover { background: var(--bg); }
.topbar nav a.on { background: var(--brand); color: var(--brand-ink); }
.logout { display: flex; align-items: center; gap: 8px; margin: 0; }
.who { color: var(--muted); font-size: 12px; }
.wrap { max-width: 1100px; margin: 0 auto; padding: 22px 16px 60px; }
main.full { padding: 0; }

/* ---- Components ---- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 18px; }
.card.warn { border-color: var(--warn); }
.card.warn li.done { text-decoration: line-through; color: var(--muted); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.card-head h2 { margin: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
.grid2 > .card { margin-bottom: 0; }
.grid2 { margin-bottom: 18px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr) auto; gap: 12px; align-items: end; }
.stack { display: grid; gap: 12px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr auto; gap: 12px; align-items: end; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; }
.btn-row form { margin: 0; }
@media (max-width: 800px) { .grid2, .grid4 { grid-template-columns: 1fr; } }

label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
label.chk { display: flex; align-items: center; gap: 8px; color: var(--ink); }
input, select, textarea { font: inherit; color: var(--ink); background: var(--card); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; width: 100%; }
input[type=checkbox] { width: auto; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--brand) 40%, transparent); border-color: var(--brand); }

.btn { display: inline-block; font: inherit; cursor: pointer; border: 1px solid var(--brand); background: var(--brand);
  color: var(--brand-ink); border-radius: 8px; padding: 8px 16px; text-align: center; }
.btn:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn.ghost:hover { background: var(--bg); filter: none; }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 3px 10px; font-size: 12px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

.flash { padding: 10px 14px; border-radius: 10px; margin-bottom: 16px; background: #e6f6ec; color: #14532d; border: 1px solid #b7e4c7; }
.flash.err { background: #fdeaea; color: #7f1d1d; border-color: #f5b5b5; }
@media (prefers-color-scheme: dark) {
  .flash { background: #12301f; color: #b7f0cb; border-color: #1f5a37; }
  .flash.err { background: #3a1616; color: #ffc9c9; border-color: #6b2a2a; }
}
.note { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; font-size: 12px; color: var(--muted); }

.badge { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--bad); color: #fff;
  font-size: 11px; line-height: 20px; text-align: center; }
.badge[hidden] { display: none; }
.tag { display: inline-block; padding: 0 8px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line);
  font-size: 11px; line-height: 20px; color: var(--muted); }
.tag.ok { background: #e6f6ec; color: var(--ok); border-color: #b7e4c7; }
.tag.bad { background: #fdeaea; color: var(--bad); border-color: #f5b5b5; margin-inline-start: 6px; }
.btn-tag { cursor: pointer; font-family: inherit; }

.stats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 14px; margin-bottom: 18px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; text-align: center; }
.stat b { display: block; font-size: 26px; }
.stat span { color: var(--muted); font-size: 12px; }
.stat.hot b { color: var(--bad); }
@media (max-width: 800px) { .stats { grid-template-columns: repeat(2, 1fr); } }

.table-scroll { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; }
.tbl th, .tbl td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: start; vertical-align: middle; }
.tbl th { font-size: 12px; color: var(--muted); font-weight: 600; }
.tbl tr.click { cursor: pointer; } .tbl tr.click:hover { background: var(--bg); }
.tbl tr.off { opacity: .55; }
.tbl .actions { white-space: nowrap; text-align: end; }
.tbl .dest { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
form.inline { display: inline; margin: 0; }
.inline-form { display: flex; gap: 6px; margin: 0; } .inline-form input { min-width: 110px; padding: 3px 8px; }

.crumbs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; color: var(--muted); }
.crumbs a { color: var(--ink); padding: 2px 8px; border-radius: 6px; } .crumbs a.on { background: var(--brand); color: var(--brand-ink); }

/* پیش‌نمایش گوشی */
.phone { background: var(--bg); border: 1px solid var(--line); border-radius: 18px; padding: 14px; max-width: 380px; margin: 0 auto 10px; }
.phone-msg { background: var(--in); border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px; white-space: normal; margin-bottom: 8px; }
.kb-preview { display: grid; gap: 6px; }
.kb-row { display: flex; gap: 6px; }
.kb-btn { flex: 1; text-align: center; font: inherit; cursor: pointer; padding: 7px 8px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--card); color: var(--ink); }
.kb-preview.reply .kb-btn { border-radius: 6px; background: color-mix(in srgb, var(--ink) 8%, var(--card)); }
.kb-btn.off { opacity: .45; text-decoration: line-through; }
.kb-row.fixed .kb-btn { cursor: default; opacity: .6; }
button.kb-btn:hover { border-color: var(--brand); }

dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); color: var(--ink); padding: 0;
  width: min(560px, 94vw); max-height: 92vh; }
dialog::backdrop { background: rgba(10, 14, 24, .5); }
.dlg { padding: 18px; display: grid; gap: 12px; }
.dlg .f[hidden] { display: none; }
.dlg-actions { display: flex; gap: 10px; }

/* ---- Auth ---- */
body.auth { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.auth-card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 26px; width: min(380px, 100%); display: grid; gap: 12px; }
.auth-card .logo { font-size: 40px; text-align: center; }
.auth-card h1 { text-align: center; margin: 0; font-size: 18px; }
.auth-card p { text-align: center; margin: 0; }

/* ---- Chat ---- */
body.chat-page { overflow: hidden; }
.chat-app { display: grid; grid-template-columns: 340px 1fr; height: calc(100vh - 56px); height: calc(100dvh - 56px); }
.conv-pane { border-inline-end: 1px solid var(--line); background: var(--card); display: flex; flex-direction: column; min-height: 0; }
.conv-tools { padding: 10px; border-bottom: 1px solid var(--line); display: grid; gap: 8px; }
.tabs { display: flex; gap: 4px; }
.tabs button { flex: 1; font: inherit; font-size: 12px; padding: 4px 0; border-radius: 8px; border: 1px solid var(--line); background: transparent; color: var(--muted); cursor: pointer; }
.tabs button.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.conv-list { overflow-y: auto; flex: 1; }
.conv-list .empty { padding: 24px; text-align: center; color: var(--muted); }
.conv { display: flex; gap: 10px; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--line); }
.conv:hover { background: var(--bg); } .conv.active { background: color-mix(in srgb, var(--brand) 14%, var(--card)); }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--brand); color: var(--brand-ink); display: grid; place-items: center; font-weight: 700; flex: none; }
.conv-body { flex: 1; min-width: 0; }
.conv-top, .conv-bot { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.conv-top small { color: var(--muted); font-size: 11px; flex: none; }
.conv-top b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-bot .last { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.thread-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--bg); }
.thread-empty { margin: auto; text-align: center; color: var(--muted); font-size: 15px; } .thread-empty div { font-size: 54px; }
.thread { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.thread[hidden], .thread-empty[hidden] { display: none; }
.thread-head { display: flex; align-items: center; gap: 12px; padding: 10px 14px; background: var(--card); border-bottom: 1px solid var(--line); }
.who-box { flex: 1; min-width: 0; display: grid; } .who-box small { color: var(--muted); }
.head-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.back-btn { display: none; }

.msgs { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 6px; }
.msgs .more { align-self: center; }
.day { align-self: center; font-size: 11px; color: var(--muted); background: var(--card); border: 1px solid var(--line); padding: 0 10px; border-radius: 10px; margin: 8px 0; }
.sys { align-self: center; font-size: 12px; color: var(--muted); background: color-mix(in srgb, var(--warn) 12%, transparent); padding: 2px 12px; border-radius: 10px; }
.msg { display: flex; align-items: flex-end; gap: 6px; max-width: 82%; }
.msg.in { align-self: flex-start; }      /* RTL: ابتدای خط = راست */
.msg.out { align-self: flex-end; flex-direction: row-reverse; }
.msg.flash .bubble { outline: 2px solid var(--brand); }
.bubble { background: var(--in); border: 1px solid var(--line); border-radius: 14px; padding: 7px 11px; min-width: 70px; max-width: 100%; }
.msg.out .bubble { background: var(--out); }
.bubble .agent { font-size: 11px; color: var(--brand); font-weight: 700; }
.bubble .txt { white-space: pre-wrap; word-break: break-word; }
.bubble .meta { display: flex; gap: 8px; justify-content: flex-end; font-size: 10px; color: var(--muted); margin-top: 2px; }
.bubble .meta .tick { color: var(--ok); } .bubble .meta .fail { color: var(--bad); }
.bubble .err { font-size: 11px; color: var(--bad); }
.bubble .quote { border-inline-start: 3px solid var(--brand); background: color-mix(in srgb, var(--brand) 10%, transparent);
  padding: 2px 8px; border-radius: 6px; margin-bottom: 4px; font-size: 12px; display: grid; }
.bubble .quote.jump { cursor: pointer; }
.bubble .quote b { color: var(--brand); font-size: 11px; }
.bubble .quote span { color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.bubble img.pic { max-width: 260px; max-height: 260px; border-radius: 8px; display: block; margin-bottom: 4px; }
.bubble audio { max-width: 240px; display: block; }
.bubble a.file { display: inline-block; margin-bottom: 4px; }
.reply-btn { opacity: 0; border: 0; background: var(--card); border: 1px solid var(--line); border-radius: 50%; width: 26px; height: 26px; cursor: pointer; color: var(--muted); flex: none; }
.msg:hover .reply-btn, .reply-btn:focus { opacity: 1; }
@media (hover: none) { .reply-btn { opacity: .7; } }

.composer { background: var(--card); border-top: 1px solid var(--line); padding: 10px 12px; display: grid; gap: 8px; }
.composer .row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; max-height: 140px; }
.attach { cursor: pointer; font-size: 20px; padding: 4px 6px; display: block; }
.replybar, .filebar { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 4px 10px;
  border-inline-start: 3px solid var(--brand); background: var(--bg); border-radius: 6px; font-size: 12px; }
.replybar[hidden], .filebar[hidden] { display: none; }
.replybar span { color: var(--muted); }
.x { border: 0; background: transparent; cursor: pointer; color: var(--muted); font-size: 14px; }

@media (max-width: 800px) {
  .chat-app { grid-template-columns: 1fr; }
  .chat-app .thread-pane { display: none; }
  .chat-app.in-thread .conv-pane { display: none; }
  .chat-app.in-thread .thread-pane { display: flex; }
  .back-btn { display: inline-block; }
  .topbar { gap: 8px; padding: 0 10px; } .who { display: none; } .brand { display: none; }
  .msg { max-width: 94%; }
}
