:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --ink: #1f2733;
  --muted: #7a8699;
  --line: #e6eaf2;
  --primary: #2f6df6;
  --primary-d: #1f57d6;
  --green: #1ea672;
  --red: #e2483d;
  --amber: #e8a13a;
  --bubble-in: #ffffff;
  --bubble-out: #d8e8ff;
  --shadow: 0 6px 24px rgba(28, 47, 84, .08);
  --radius: 14px;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: Vazirmatn, Tahoma, "Segoe UI", system-ui, sans-serif;
  background: var(--bg); color: var(--ink); direction: rtl;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--primary); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
input, textarea, select {
  font-family: inherit; font-size: 14px; color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%;
  background: #fff; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--primary); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--primary); color: #fff; border: 0; border-radius: 10px;
  padding: 10px 16px; font-size: 14px; font-weight: 600; transition: .15s;
}
.btn:hover { background: var(--primary-d); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.ghost { background: #eef2fb; color: var(--primary); }
.btn.ghost:hover { background: #e2e9f8; }
.btn.danger { background: var(--red); }
.btn.green { background: var(--green); }
.btn.sm { padding: 6px 10px; font-size: 12px; border-radius: 8px; }
.badge { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 11px; font-weight: 700; }
.badge.green { background: #e3f7ee; color: var(--green); }
.badge.red { background: #fde9e7; color: var(--red); }
.badge.gray { background: #eef1f6; color: var(--muted); }
.badge.blue { background: #e7f0ff; color: var(--primary); }

/* ---------- login ---------- */
.login-wrap { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 20px;
  background: linear-gradient(135deg, #2f6df6 0%, #6a3df6 100%); }
.login-card { background: #fff; border-radius: 20px; box-shadow: var(--shadow); width: 100%; max-width: 400px; padding: 32px 26px; }
.brand-title { text-align: center; font-size: 26px; font-weight: 800; margin: 0 0 4px;
  background: linear-gradient(90deg,#2f6df6,#6a3df6); -webkit-background-clip: text; background-clip: text; color: transparent; }
.brand-sub { text-align: center; color: var(--muted); margin: 0 0 24px; font-size: 13px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; margin-bottom: 6px; color: var(--muted); }
.err { color: var(--red); font-size: 13px; min-height: 18px; margin-top: 4px; }

/* ---------- app shell ---------- */
.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }
.sidebar { width: 230px; background: var(--panel); border-left: 1px solid var(--line); padding: 18px 14px;
  display: flex; flex-direction: column; gap: 6px; position: sticky; top: 0; height: 100vh; height: 100dvh; flex: 0 0 auto; }
.sidebar .logo { font-weight: 800; font-size: 18px; margin: 4px 6px 16px; color: var(--primary); }
.nav-item { display: flex; align-items: center; gap: 8px; padding: 11px 12px; border-radius: 10px;
  color: var(--ink); font-size: 14px; font-weight: 600; }
.nav-item:hover { background: #f1f4fb; }
.nav-item.active { background: var(--primary); color: #fff; }
.nav-spacer { flex: 1; }
.main { flex: 1; padding: 18px; min-width: 0; height: 100vh; height: 100dvh;
  display: flex; flex-direction: column; overflow: hidden; }
/* the visible view fills the remaining height; long views scroll internally */
.main > section { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
.main > section#view-chats { overflow: hidden; }
.topbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; gap: 10px; flex-wrap: wrap; flex: 0 0 auto; }
.topbar h2 { margin: 0; font-size: 20px; }
.me-chip { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.avatar { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; background: #dde4f0; display: inline-block; }
.avatar.sm { width: 30px; height: 30px; }
.avatar.lg { width: 54px; height: 54px; }

/* cards / grid */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px,1fr)); gap: 14px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.stat { display: flex; flex-direction: column; gap: 4px; }
.stat .k { color: var(--muted); font-size: 12px; }
.stat .v { font-size: 26px; font-weight: 800; }
.section { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 18px; }
.section h3 { margin: 0 0 14px; font-size: 16px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row > * { flex: 1; min-width: 120px; }
.row .shrink { flex: 0 0 auto; min-width: auto; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: right; padding: 10px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
tr:hover td { background: #fafbff; }
.table-wrap { overflow-x: auto; }

.tabs { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.tab { padding: 8px 14px; border-radius: 20px; background: #eef2fb; color: var(--muted); font-size: 13px; font-weight: 600; }
.tab.active { background: var(--primary); color: #fff; }

/* ---------- chat (telegram-like) ---------- */
.chat-layout { display: grid; grid-template-columns: 320px 1fr; grid-template-rows: 1fr; gap: 0; height: 100%; min-height: 0;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.chat-list { border-left: 1px solid var(--line); overflow-y: auto; min-height: 0; }
.chat-list .search { padding: 12px; position: sticky; top: 0; background: var(--panel); z-index: 2; border-bottom: 1px solid var(--line); }
.chat-row { display: flex; gap: 10px; padding: 12px; cursor: pointer; border-bottom: 1px solid var(--line); align-items: center; }
.chat-row:hover { background: #f5f8ff; }
.chat-row.active { background: #e9f1ff; }
.chat-row .meta { flex: 1; min-width: 0; }
.chat-row .name { font-weight: 700; font-size: 14px; display: flex; align-items: center; gap: 6px; }
.chat-row .last { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unread-dot { background: var(--primary); color: #fff; border-radius: 20px; font-size: 11px; padding: 1px 7px; font-weight: 700; }

.chat-main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-header { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.chat-header .name { font-weight: 700; }
.chat-header .actions { margin-right: auto; display: flex; gap: 8px; }
.pin-bar { background: #fff7e6; border-bottom: 1px solid #f0e2c0; padding: 8px 14px; font-size: 12px; display: none; flex: 0 0 auto; max-height: 116px; overflow-y: auto; }
.pin-bar.show { display: block; }
.pin-bar .pin-item { padding: 5px 8px; border-right: 3px solid var(--amber); margin: 4px 0; background: #fffdf6; border-radius: 6px; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.pin-bar .pin-item:hover { background: #fff3d6; }
.msg.flash { animation: msgflash 1.5s ease; }
@keyframes msgflash { 0%, 60% { box-shadow: 0 0 0 3px rgba(47,109,246,.55); } 100% { box-shadow: 0 1px 2px rgba(0,0,0,.06); } }
.messages { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 16px; background: #eef1f8; display: flex; flex-direction: column; gap: 8px; }
.composer { flex: 0 0 auto; }
.msg { max-width: 72%; padding: 8px 12px; border-radius: 14px; font-size: 14px; position: relative; word-break: break-word; box-shadow: 0 1px 2px rgba(0,0,0,.06); }
.msg.in { background: var(--bubble-in); align-self: flex-start; border-bottom-right-radius: 4px; }
.msg.out { background: var(--bubble-out); align-self: flex-end; border-bottom-left-radius: 4px; }
.msg.system { background: #fff5d6; align-self: center; font-size: 12px; color: #8a6d1e; max-width: 90%; text-align: center; }
.msg .time { font-size: 10px; color: var(--muted); margin-top: 3px; text-align: left; }
.msg .reply-quote { border-right: 3px solid var(--primary); background: rgba(47,109,246,.08); padding: 4px 8px; border-radius: 6px; font-size: 12px; margin-bottom: 5px; color: var(--muted); }
.msg .tools { position: absolute; top: -10px; left: 6px; display: none; gap: 4px; }
.msg:hover .tools { display: flex; }
.msg .tools button { background: #fff; border: 1px solid var(--line); border-radius: 6px; font-size: 11px; padding: 2px 6px; }
.msg img.media { max-width: 240px; border-radius: 10px; display: block; }
.msg video.media { max-width: 260px; border-radius: 10px; display: block; }
.msg .file-chip { display: flex; align-items: center; gap: 6px; }
.pinned-flag { color: var(--amber); font-size: 11px; }
.msg-status { font-size: 11px; margin-right: 2px; }
.msg-status.fail { cursor: pointer; }

.composer { border-top: 1px solid var(--line); padding: 10px; display: flex; gap: 8px; align-items: flex-end; background: var(--panel); }
.composer textarea { resize: none; max-height: 120px; min-height: 42px; }
.composer .icon-btn { background: #eef2fb; border: 0; border-radius: 10px; width: 42px; height: 42px; font-size: 18px; color: var(--primary); flex: 0 0 auto; }
.reply-preview { background: #eef2fb; border-right: 3px solid var(--primary); padding: 6px 10px; font-size: 12px; display: none; align-items: center; justify-content: space-between; }
.reply-preview.show { display: flex; }
.recording-bar { align-items: center; }
.recording-bar .rec-meta { flex: 1; display: flex; align-items: center; gap: 8px; font-size: 13px; }
.rec-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--red); animation: pulse 1s infinite; display: inline-block; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .25; } }
.icon-btn.danger-btn { background: #fde9e7; color: var(--red); }
.icon-btn.send-rec { background: var(--primary); color: #fff; }
.empty-chat { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); }

/* toast / popup */
.toast-wrap { position: fixed; top: 16px; left: 50%; transform: translateX(-50%); z-index: 9999; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: #1f2733; color: #fff; padding: 12px 18px; border-radius: 12px; font-size: 13px; box-shadow: var(--shadow); max-width: 90vw; }
.toast.warn { background: var(--amber); }
.toast.error { background: var(--red); }
.toast.ok { background: var(--green); }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(20,30,50,.45); display: none; align-items: center; justify-content: center; z-index: 1000; padding: 16px; }
.modal-bg.show { display: flex; }
.modal { background: #fff; border-radius: 16px; width: 100%; max-width: 460px; padding: 22px; box-shadow: var(--shadow); max-height: 90vh; overflow-y: auto; }
.modal h3 { margin: 0 0 16px; }
.modal .field { margin-bottom: 12px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-start; margin-top: 16px; }

.upload-bar { position: relative; height: 22px; background: #eef2fb; border-radius: 11px; overflow: hidden; margin: 4px 0 6px; }
.upload-bar .fill { position: absolute; inset: 0 auto 0 0; right: 0; width: 0; background: linear-gradient(90deg,#2f6df6,#6a3df6); transition: width .15s; }
.upload-bar .pct { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; color: #1f2733; }

.mobile-only { display: none; }
.hamburger { display: none; background: #eef2fb; border: 0; border-radius: 10px; width: 40px; height: 40px; font-size: 18px; }

/* ---------- responsive ---------- */
@media (max-width: 820px) {
  .sidebar { position: fixed; right: 0; top: 0; z-index: 500; transform: translateX(100%); transition: .25s; box-shadow: var(--shadow); }
  .sidebar.open { transform: translateX(0); }
  .hamburger { display: inline-flex; align-items: center; justify-content: center; }
  .main { padding: 10px; }
  .chat-layout { grid-template-columns: 1fr; height: 100%; }
  .chat-list { display: block; }
  .chat-list.hide-mobile { display: none; }
  .chat-main.hide-mobile { display: none; }
  .mobile-only { display: inline-flex; }
  .msg { max-width: 86%; }
  .sidebar-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 400; display: none; }
  .sidebar-backdrop.show { display: block; }
}
.hidden { display: none !important; }
.center-load { text-align: center; color: var(--muted); padding: 30px; }
