:root {
  color-scheme: light dark;
  --bg: #eef1f8;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --border: #e2e7f1;
  --text: #16203a;
  --muted: #6b7690;
  --primary: #4f6bed;
  --primary-strong: #3a54d6;
  --success: #12a150;
  --danger: #d1293d;
  --warning: #c77400;
  --shadow: 0 10px 30px rgba(24, 39, 75, 0.08);
  --radius: 16px;
  font-family: "Vazirmatn", system-ui, -apple-system, Segoe UI, Tahoma, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1220;
    --surface: #161d30;
    --surface-2: #1c2439;
    --border: #27314c;
    --text: #e8ecf6;
    --muted: #94a0bd;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(1200px 600px at 85% -10%, rgba(79, 107, 237, 0.18), transparent 60%),
    radial-gradient(1000px 500px at 0% 0%, rgba(18, 161, 80, 0.10), transparent 55%),
    var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--muted); margin: 4px 0 0; font-size: 14px; }

/* Brand */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.brand-mark {
  display: grid; place-items: center;
  width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), #7d5cf5);
  box-shadow: 0 6px 16px rgba(79, 107, 237, 0.4);
  font-size: 18px;
}
.brand-name { font-size: 18px; letter-spacing: -0.2px; direction: ltr; }
.brand-name b { color: var(--primary); font-weight: 800; }
.brand--lg { justify-content: center; margin-bottom: 6px; }
.brand--lg .brand-mark { width: 46px; height: 46px; font-size: 24px; border-radius: 14px; }
.brand--lg .brand-name { font-size: 24px; }

/* Login */
.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: 100%; max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 34px 30px;
  box-shadow: var(--shadow);
  animation: rise 0.4s ease;
}
.login-sub { text-align: center; color: var(--muted); font-size: 14px; margin: 0 0 22px; }

/* App shell */
.app { max-width: 1020px; margin: 0 auto; padding: 20px 16px 60px; }
.topbar {
  display: flex; align-items: center; gap: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  box-shadow: var(--shadow);
  position: sticky; top: 12px; z-index: 5;
}
.tabs { display: flex; gap: 4px; margin: 0 auto; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.tab {
  border: 0; background: transparent; color: var(--muted);
  font: inherit; font-weight: 600; font-size: 14px;
  padding: 8px 16px; border-radius: 9px; cursor: pointer;
  transition: all 0.15s ease;
}
.tab:hover { color: var(--text); }
.tab.is-active { background: var(--surface); color: var(--primary); box-shadow: var(--shadow); }

.content { margin-top: 20px; }
.tab-panel { animation: rise 0.3s ease; }

.section-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 16px; margin-bottom: 18px; flex-wrap: wrap;
}
.section-head h2 { margin: 0; font-size: 22px; letter-spacing: -0.3px; }
.section-actions { display: flex; gap: 8px; align-items: center; }

/* Forms & inputs */
label { display: block; font-weight: 600; font-size: 14px; margin: 14px 0 6px; }
.login-card label:first-of-type { margin-top: 0; }
.input, input[type="password"], textarea {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font: inherit; font-size: 15px;
  padding: 11px 13px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.input:focus, input:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 107, 237, 0.18);
}
textarea { resize: vertical; line-height: 1.5; }
.input--search { width: 220px; max-width: 46vw; }

/* Buttons */
.btn {
  border: 1px solid transparent; border-radius: 10px;
  font: inherit; font-weight: 600; font-size: 14px;
  padding: 10px 16px; cursor: pointer;
  transition: transform 0.05s ease, background 0.15s ease, opacity 0.15s ease;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn--primary { background: var(--primary); color: #fff; }
.btn--primary:hover { background: var(--primary-strong); }
.btn--primary:disabled { opacity: 0.6; cursor: progress; }
.btn--soft { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn--soft:hover { border-color: var(--primary); color: var(--primary); }
.btn--ghost { background: transparent; color: var(--muted); border-color: var(--border); }
.btn--ghost:hover { color: var(--danger); border-color: var(--danger); }
.btn--block { width: 100%; margin-top: 20px; padding: 12px; }

/* Avatar */
.avatar {
  flex: 0 0 auto;
  display: grid; place-items: center;
  width: 44px; height: 44px; border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), #7d5cf5);
  color: #fff; font-weight: 700; font-size: 18px;
}

/* Conversation list */
.threads { display: flex; flex-direction: column; gap: 8px; }
.thread {
  display: flex; align-items: center; gap: 14px; text-align: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  cursor: pointer; width: 100%;
  font: inherit; color: var(--text);
  box-shadow: var(--shadow);
  transition: transform 0.08s ease, border-color 0.15s ease;
  animation: rise 0.2s ease;
}
.thread:hover { border-color: var(--primary); transform: translateY(-1px); }
.thread-main { flex: 1; min-width: 0; }
.thread-top { display: flex; align-items: center; gap: 8px; }
.thread-name { font-weight: 700; font-size: 15px; }
.thread-count {
  background: var(--primary); color: #fff;
  font-size: 12px; font-weight: 700;
  min-width: 20px; height: 20px; padding: 0 6px;
  border-radius: 999px; display: grid; place-items: center;
}
.thread-time { margin-inline-start: auto; color: var(--muted); font-size: 12px; white-space: nowrap; }
.thread-preview {
  color: var(--muted); font-size: 14px; margin-top: 4px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* Conversation view */
.thread-header {
  display: flex; align-items: center; gap: 14px; margin-bottom: 16px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 16px; box-shadow: var(--shadow);
}
.thread-peer { display: flex; align-items: center; gap: 12px; }
.thread-peer-name { font-weight: 700; font-size: 16px; }

.chat {
  display: flex; flex-direction: column; gap: 8px;
  max-height: 58vh; overflow-y: auto;
  padding: 6px 4px;
}
.bubble {
  max-width: 78%; padding: 9px 13px; border-radius: 16px;
  box-shadow: var(--shadow); animation: rise 0.2s ease;
}
.bubble--in {
  align-self: flex-start;
  background: var(--surface); border: 1px solid var(--border);
  border-top-right-radius: 4px;
}
.bubble--out {
  align-self: flex-end;
  background: linear-gradient(135deg, var(--primary), #6d5cf0);
  color: #fff; border-top-left-radius: 4px;
}
.bubble-body { white-space: pre-wrap; line-height: 1.5; word-break: break-word; }
.bubble-meta {
  display: flex; align-items: center; gap: 8px; justify-content: flex-end;
  margin-top: 4px; font-size: 11px; opacity: 0.85;
}

.reply-bar { display: flex; gap: 8px; align-items: flex-end; margin-top: 14px; }
.reply-bar textarea { flex: 1; min-height: 44px; max-height: 140px; resize: none; }
.reply-device { width: auto; max-width: 40%; flex: 0 0 auto; }

.thread-device { color: var(--muted); font-size: 12px; margin-top: 4px; }

/* Devices */
select.input { cursor: pointer; }
.device-actions { display: flex; gap: 8px; margin-top: 14px; }
.token-box {
  margin-top: 14px; padding: 14px;
  border: 1px dashed var(--primary); border-radius: 12px;
  background: var(--surface-2);
  display: flex; flex-direction: column; gap: 10px; align-items: flex-start;
}
.token-box code {
  display: block; width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 14px; word-break: break-all;
  background: var(--surface); padding: 10px 12px; border-radius: 8px;
  border: 1px solid var(--border);
}

/* Compose */
.compose {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
  max-width: 640px;
}
.compose-meta { display: flex; justify-content: flex-end; color: var(--muted); font-size: 12px; margin-top: 6px; }
.compose-actions { display: flex; align-items: center; gap: 14px; margin-top: 18px; }
.send-status { font-size: 14px; font-weight: 500; min-height: 20px; margin: 8px 0 0; }
.send-status.ok { color: var(--success); }
.send-status.err { color: var(--danger); }

/* Cards (outbox) */
.cards { display: grid; gap: 14px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  animation: rise 0.25s ease;
}
.card-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.card-address { font-weight: 700; font-size: 15px; }
.card-body { margin: 0 0 12px; white-space: pre-wrap; line-height: 1.5; word-break: break-word; }

.badge {
  font-size: 12px; font-weight: 600;
  padding: 3px 10px; border-radius: 999px;
  border: 1px solid transparent; white-space: nowrap;
}
.badge--sent { background: rgba(18, 161, 80, 0.14); color: var(--success); }
.badge--failed { background: rgba(209, 41, 61, 0.12); color: var(--danger); }
.badge--pending { background: rgba(199, 116, 0, 0.14); color: var(--warning); }
.badge--processing { background: rgba(199, 116, 0, 0.14); color: var(--warning); }
.bubble--out .badge { background: rgba(255, 255, 255, 0.22); color: #fff; }

.meta { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 6px 16px; margin: 0; font-size: 13px; }
.meta div { display: flex; flex-direction: column; }
.meta dt { color: var(--muted); font-size: 11px; }
.meta dd { margin: 0; }

.empty { text-align: center; color: var(--muted); padding: 40px 0; }
.error { color: var(--danger); min-height: 20px; text-align: center; font-size: 14px; margin: 10px 0 0; }

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

@media (max-width: 680px) {
  .topbar { flex-wrap: wrap; }
  .tabs { order: 3; width: 100%; margin: 4px 0 0; }
  .input--search { width: 100%; max-width: none; }
  .section-actions { width: 100%; }
  .section-actions .input--search { flex: 1; }
  .bubble { max-width: 88%; }
}
