:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --text: #1d2433;
  --muted: #6b7488;
  --border: #e3e7f0;
  --primary: #4f46e5;
  --primary-text: #ffffff;
  --theirs: #eceffa;
  --ok: #16a34a;
  --bad: #dc2626;
  --radius: 16px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1320;
    --panel: #171c2e;
    --text: #e8ebf5;
    --muted: #8d96ad;
    --border: #262d45;
    --theirs: #232a42;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  font-family: Vazirmatn, Tahoma, system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

button, input, textarea { font: inherit; color: inherit; }

button {
  cursor: pointer;
  border: 0;
  border-radius: 12px;
  padding: 10px 16px;
  background: var(--primary);
  color: var(--primary-text);
  transition: filter .15s, transform .05s;
}
button:hover:not(:disabled) { filter: brightness(1.08); }
button:active:not(:disabled) { transform: scale(.97); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.ghost { background: transparent; color: var(--muted); padding: 8px 10px; }
button.ghost:hover { color: var(--text); }

input, textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 12px;
  padding: 10px 14px;
  outline: none;
}
input:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent); }

/* ─── وضعیت اتصال ─── */
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.on { background: var(--ok); }
.dot.off { background: var(--bad); }

/* ─── پنل چت ─── */
.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
}

.chat > header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.chat > header h1, .chat > header strong { margin: 0; font-size: 16px; }
.chat > header small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.chat > header .spacer { flex: 1; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
}

.empty { margin: auto; color: var(--muted); text-align: center; }

/* در RTL: flex-start = سمت راست */
.bubble {
  max-width: min(75%, 520px);
  padding: 9px 14px 6px;
  border-radius: var(--radius);
  background: var(--theirs);
  align-self: flex-end;
  border-end-end-radius: 4px;
  animation: pop .15s ease-out;
}
.bubble.mine {
  background: var(--primary);
  color: var(--primary-text);
  align-self: flex-start;
  border-end-end-radius: var(--radius);
  border-end-start-radius: 4px;
}
.bubble p { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.7; }
.bubble time { display: block; font-size: 11px; opacity: .65; margin-top: 2px; text-align: end; }

@keyframes pop { from { opacity: 0; transform: translateY(6px); } }

.composer {
  display: flex;
  gap: 10px;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  align-items: flex-end;
}
.composer textarea { resize: none; max-height: 140px; min-height: 42px; line-height: 1.6; }
.composer[hidden] { display: none; }

/* ─── دیالوگ‌ها ─── */
dialog {
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--panel);
  color: var(--text);
  padding: 24px;
  width: min(92vw, 380px);
}
dialog::backdrop { background: rgba(10, 14, 28, .55); backdrop-filter: blur(4px); }
dialog h2 { margin: 0 0 6px; font-size: 18px; }
dialog p { margin: 0 0 16px; color: var(--muted); font-size: 13px; }
dialog form { display: flex; flex-direction: column; gap: 12px; }
dialog .row { display: flex; gap: 8px; justify-content: flex-end; }
.error { color: var(--bad); font-size: 13px; min-height: 1.2em; margin: 0; }

/* ─── صفحه‌ی کاربر ─── */
.user-page { height: 100%; display: flex; justify-content: center; padding: 16px; }
.user-page .chat {
  width: min(100%, 720px);
  border: 1px solid var(--border);
  border-radius: 24px;
  overflow: hidden;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .08);
}
@media (max-width: 600px) {
  .user-page { padding: 0; }
  .user-page .chat { border-radius: 0; border: 0; }
}

/* ─── پنل ادمین ─── */
.admin { height: 100%; display: grid; grid-template-columns: 340px 1fr; }

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-inline-end: 1px solid var(--border);
}
.sidebar > header { padding: 16px 18px 8px; display: flex; align-items: center; justify-content: space-between; }
.sidebar > header h1 { margin: 0; font-size: 17px; }
.sidebar .search { padding: 8px 14px 12px; }

#users { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }

.user {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px 18px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.user:hover { background: color-mix(in srgb, var(--primary) 6%, transparent); }
.user.active { background: color-mix(in srgb, var(--primary) 14%, transparent); }

.avatar {
  position: relative;
  flex: none;
  width: 42px; height: 42px;
  border-radius: 50%;
  background: var(--primary);
  color: var(--primary-text);
  display: grid; place-items: center;
  font-weight: 700;
}
.avatar .dot { position: absolute; inset-inline-end: -1px; bottom: -1px; border: 2px solid var(--panel); width: 12px; height: 12px; }

.user .info { flex: 1; min-width: 0; }
.user .top { display: flex; justify-content: space-between; gap: 8px; }
.user .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user .time { font-size: 11px; color: var(--muted); flex: none; }
.user .last { display: flex; justify-content: space-between; gap: 8px; margin-top: 2px; }
.user .last span:first-child { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { background: var(--bad); color: #fff; border-radius: 999px; font-size: 11px; min-width: 20px; height: 20px; padding: 0 6px; display: grid; place-items: center; }

.sidebar > footer { padding: 12px 14px; border-top: 1px solid var(--border); }
.sidebar > footer button { width: 100%; }

.admin .chat { background: var(--bg); }
.admin .chat > header { background: var(--panel); }
.admin .composer { background: var(--panel); }
#back { display: none; }

@media (max-width: 720px) {
  .admin { grid-template-columns: 1fr; }
  .admin .chat { display: none; }
  .admin.chat-open .sidebar { display: none; }
  .admin.chat-open .chat { display: flex; }
  #back { display: inline-block; }
}

/* ─── آپلود فایل ─── */
.attach { font-size: 20px; padding: 8px 10px; align-self: flex-end; }

.upload {
  padding: 10px 16px 12px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}
.upload[hidden], .upload-msg[hidden] { display: none; }
.upload-info { display: flex; align-items: center; gap: 10px; font-size: 13px; margin-bottom: 8px; }
.upload-name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.upload-pct { color: var(--muted); font-variant-numeric: tabular-nums; }
.upload-cancel { padding: 2px 8px; }

.progress { height: 8px; border-radius: 999px; background: var(--theirs); overflow: hidden; }
.progress-fill {
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--primary), color-mix(in srgb, var(--primary) 55%, #22d3ee));
  transition: width .15s linear;
}

.upload-msg {
  margin: 0;
  padding: 10px 16px;
  font-size: 13px;
  color: var(--bad);
  background: color-mix(in srgb, var(--bad) 10%, var(--panel));
  border-top: 1px solid var(--border);
}

.chat.dragging { outline: 3px dashed var(--primary); outline-offset: -10px; }

.attach-img { display: block; margin-bottom: 6px; }
.attach-img img {
  display: block;
  max-width: 100%;
  max-height: 280px;
  border-radius: 10px;
  object-fit: cover;
}

.file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  margin-bottom: 6px;
  border-radius: 12px;
  color: inherit;
  text-decoration: none;
  background: color-mix(in srgb, currentColor 10%, transparent);
}
.file-card:hover { background: color-mix(in srgb, currentColor 16%, transparent); }
.file-icon { font-size: 22px; }
.file-meta { display: flex; flex-direction: column; min-width: 0; }
.file-meta b { font-weight: 600; font-size: 14px; overflow-wrap: anywhere; }
.file-meta small { opacity: .7; font-size: 12px; }

/* ─── نمایش کاربر در پنل ادمین (IP / رزولوشن / صفحه) ─── */
.user .meta {
  color: var(--muted);
  font-size: 12px;
  margin-top: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user .name { direction: ltr; text-align: start; }

/* ─── حالت جاسازی‌شده در iframe سایت ─── */
.embed .user-page { padding: 0; }
.embed .user-page .chat { width: 100%; border: 0; border-radius: 0; box-shadow: none; }
