/* ================= Design tokens ================= */
:root {
  --bg: #eef2f1;
  --panel: #ffffff;
  --panel-2: #f6f9f8;
  --text: #10201e;
  --muted: #6b7f7c;
  --border: #e0e7e5;
  --brand: #0f766e;
  --brand-2: #0b5e57;
  --brand-soft: #ccfbf1;
  --mine: #d7f8ec;
  --theirs: #ffffff;
  --danger: #dc2626;
  --warn: #d97706;
  --shadow: 0 12px 34px rgba(10, 60, 55, 0.10);
  --radius: 16px;
  --sidebar-w: 344px;
  --header-h: 72px;
  --font: "Vazirmatn", "Segoe UI", Tahoma, "Iranian Sans", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a1413;
    --panel: #10201e;
    --panel-2: #0d1a18;
    --text: #e8f3f0;
    --muted: #8aa5a0;
    --border: #1d3330;
    --brand: #14b8a6;
    --brand-2: #0f766e;
    --brand-soft: #134e4a;
    --mine: #124a44;
    --theirs: #152725;
    --shadow: 0 14px 40px rgba(0, 0, 0, 0.45);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button, input, textarea { font-family: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.svg-sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: rgba(120, 140, 137, .35); border-radius: 8px; }
::-webkit-scrollbar-track { background: transparent; }

/* ================= Common ================= */
.screen { height: 100dvh; }

.avatar {
  width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 700; font-size: 16px; color: #fff;
  background: var(--brand);
  flex: none; user-select: none;
}
.avatar.sm { width: 34px; height: 34px; font-size: 13px; }
.avatar.lg { width: 64px; height: 64px; font-size: 22px; }
.avatar.xl { width: 104px; height: 104px; font-size: 36px; margin-bottom: 14px; }

.btn {
  border: 0; border-radius: 12px; padding: 11px 18px;
  font-size: 15px; font-weight: 600; background: var(--panel-2);
  transition: transform .06s ease, filter .15s ease, background .15s ease;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--brand); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border: 1px solid var(--border); }
.btn.wide { width: 100%; }
.btn.small-btn { padding: 7px 14px; font-size: 13px; border-radius: 10px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.icon-btn {
  width: 42px; height: 42px; border: 0; border-radius: 12px;
  background: transparent; color: var(--muted);
  display: grid; place-items: center;
  transition: background .15s ease, color .15s ease;
}
.icon-btn:hover { background: var(--panel-2); color: var(--brand); }
.icon-btn.send-btn {
  background: var(--brand); color: #fff; border-radius: 50%;
}
.icon-btn.send-btn:hover { filter: brightness(1.08); background: var(--brand); color: #fff; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.tiny { font-size: 12px; }
.center { text-align: center; }

.form-error {
  background: rgba(220, 38, 38, .1); color: var(--danger);
  border: 1px solid rgba(220, 38, 38, .25);
  padding: 10px 12px; border-radius: 10px; font-size: 14px; margin: 10px 0 0;
}

/* ================= Auth ================= */
.auth-screen { display: grid; place-items: center; padding: 20px; position: relative; overflow: hidden; }
.auth-bg {
  position: absolute; inset: -20%;
  background:
    radial-gradient(45% 45% at 20% 20%, rgba(20, 184, 166, .35), transparent 70%),
    radial-gradient(40% 40% at 85% 15%, rgba(13, 148, 136, .25), transparent 70%),
    radial-gradient(50% 50% at 70% 90%, rgba(45, 212, 191, .25), transparent 70%);
  filter: blur(10px);
}
.auth-card {
  position: relative; width: 100%; max-width: 420px;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(16px);
  border: 1px solid var(--border); border-radius: 24px;
  padding: 30px; box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.brand-logo {
  width: 54px; height: 54px; border-radius: 16px;
  background: linear-gradient(135deg, var(--brand), #14b8a6);
  color: #fff; display: grid; place-items: center;
  box-shadow: 0 8px 20px rgba(15, 118, 110, .35);
}
.brand-logo svg { width: 28px; height: 28px; }
.brand h1 { font-size: 19px; margin: 0 0 4px; }
.brand p { margin: 0; font-size: 12.5px; color: var(--muted); line-height: 1.6; }

.tabs { display: flex; background: var(--panel-2); border-radius: 12px; padding: 4px; margin-bottom: 18px; }
.tab {
  flex: 1; border: 0; background: transparent; padding: 9px;
  border-radius: 9px; font-size: 14px; font-weight: 600; color: var(--muted);
}
.tab.active { background: var(--panel); color: var(--brand); box-shadow: 0 2px 8px rgba(0,0,0,.06); }

.auth-form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; color: var(--muted); }
.field input {
  border: 1px solid var(--border); background: var(--panel-2);
  border-radius: 11px; padding: 12px 14px; font-size: 15px; outline: none;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.field input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 18%, transparent);
}
.auth-form .btn { margin-top: 6px; }

/* ================= App layout ================= */
.app-screen { display: flex; height: 100dvh; overflow: hidden; }

.sidebar {
  width: var(--sidebar-w); flex: none;
  background: var(--panel);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head {
  height: var(--header-h); flex: none;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; border-bottom: 1px solid var(--border);
}
.me { display: flex; align-items: center; gap: 11px; min-width: 0; }
.me-info { display: flex; flex-direction: column; min-width: 0; }
.me-info strong { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status { font-size: 11.5px; color: var(--muted); }
.status.online { color: var(--brand); }
.head-actions { display: flex; gap: 2px; }

.search-box {
  margin: 12px 14px 6px; display: flex; align-items: center; gap: 8px;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 0 12px; flex: none;
}
.search-box svg { width: 18px; height: 18px; color: var(--muted); }
.search-box input { flex: 1; border: 0; background: transparent; padding: 11px 0; outline: none; font-size: 14px; }

.conv-list { flex: 1; overflow-y: auto; padding: 8px; min-height: 0; }
.conv-item {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 10px; border-radius: 14px; cursor: pointer;
  transition: background .14s ease; position: relative;
}
.conv-item:hover { background: var(--panel-2); }
.conv-item.active { background: color-mix(in srgb, var(--brand) 12%, transparent); }
.conv-body { flex: 1; min-width: 0; }
.conv-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.conv-name { font-size: 14.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { font-size: 11px; color: var(--muted); flex: none; }
.conv-preview {
  font-size: 12.5px; color: var(--muted); margin-top: 3px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.badge {
  min-width: 20px; height: 20px; border-radius: 10px; padding: 0 6px;
  background: var(--brand); color: #fff; font-size: 11px; font-weight: 700;
  display: grid; place-items: center; flex: none;
}
.empty-list { text-align: center; color: var(--muted); font-size: 13px; padding: 40px 16px; line-height: 2; }

.sidebar-foot {
  flex: none; display: flex; align-items: center; gap: 7px;
  padding: 10px 16px; border-top: 1px solid var(--border);
  font-size: 11.5px; color: var(--muted);
}
.conn-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.conn-dot.on { background: #16a34a; }
.conn-dot.off { background: var(--danger); }
.lock-note { margin-right: auto; display: inline-flex; align-items: center; gap: 4px; font-size: 10.5px; }
.lock-note svg { width: 12px; height: 12px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg); }

/* ================= Empty state ================= */
.empty-state {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 6px; padding: 24px; text-align: center;
}
.empty-logo {
  width: 86px; height: 86px; border-radius: 28px; margin-bottom: 12px;
  display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, var(--brand), #2dd4bf);
  box-shadow: 0 16px 40px rgba(15, 118, 110, .3);
}
.empty-logo svg { width: 42px; height: 42px; }
.empty-state h2 { margin: 0; font-size: 21px; }
.empty-state p { margin: 0; color: var(--muted); font-size: 13.5px; }

/* ================= Chat ================= */
.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-head {
  height: var(--header-h); flex: none;
  display: flex; align-items: center; gap: 12px;
  padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.back-btn { display: none; }
.chat-peer { display: flex; align-items: center; gap: 11px; min-width: 0; flex: 1; }
.peer-text { display: flex; flex-direction: column; min-width: 0; }
.peer-text strong { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-actions { display: flex; gap: 2px; }

.messages {
  flex: 1; overflow-y: auto; min-height: 0;
  padding: 18px 20px 8px;
  display: flex; flex-direction: column; gap: 3px;
  background-image: radial-gradient(circle at 1px 1px, color-mix(in srgb, var(--muted) 14%, transparent) 1px, transparent 0);
  background-size: 22px 22px;
}

.day-divider { align-self: center; margin: 12px 0 8px; padding: 4px 12px; border-radius: 20px; background: color-mix(in srgb, var(--panel) 80%, transparent); border: 1px solid var(--border); font-size: 11px; color: var(--muted); }

.msg-row { display: flex; margin: 2px 0; }
.msg-row.mine { justify-content: flex-start; }
.msg-row.theirs { justify-content: flex-end; }
.bubble {
  max-width: min(72%, 560px); padding: 8px 12px 6px;
  border-radius: 16px; background: var(--theirs);
  border: 1px solid color-mix(in srgb, var(--border) 60%, transparent);
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
  position: relative; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg-row.mine .bubble { background: var(--mine); border-color: transparent; border-bottom-right-radius: 5px; }
.msg-row.theirs .bubble { border-bottom-left-radius: 5px; }
.msg-sender { font-size: 11.5px; font-weight: 700; color: var(--brand); margin-bottom: 3px; }
.msg-text { font-size: 14.5px; line-height: 1.75; white-space: pre-wrap; }
.msg-text a { color: var(--brand); }
.msg-meta {
  display: flex; align-items: center; justify-content: flex-end; gap: 4px;
  font-size: 10.5px; color: var(--muted); margin-top: 3px; user-select: none;
}
.msg-meta svg { width: 14px; height: 14px; stroke-width: 2.4; }
.msg-meta .ticks.read { color: var(--brand); }

.msg-image { display: block; border-radius: 12px; max-width: 100%; max-height: 340px; margin: 2px 0 4px; cursor: pointer; background: var(--panel-2); }
.msg-audio { width: 240px; max-width: 100%; margin: 4px 0; }
.msg-file {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px;
  border-radius: 12px; background: color-mix(in srgb, var(--panel) 55%, transparent);
  border: 1px solid var(--border); margin: 2px 0 4px; min-width: 190px;
}
.msg-file .file-icon { width: 38px; height: 38px; border-radius: 10px; background: color-mix(in srgb, var(--brand) 14%, transparent); color: var(--brand); display: grid; place-items: center; flex: none; }
.msg-file .file-meta { min-width: 0; flex: 1; }
.msg-file .file-name { font-size: 12.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-file .file-size { font-size: 10.5px; color: var(--muted); }
.msg-file .file-dl { flex: none; }
.msg-file .file-dl svg { width: 18px; height: 18px; }

.typing-line { min-height: 20px; padding: 0 24px; font-size: 11.5px; color: var(--muted); }

.composer {
  flex: none; display: flex; align-items: flex-end; gap: 6px;
  padding: 10px 14px 14px; background: var(--panel); border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); background: var(--panel-2);
  border-radius: 20px; padding: 11px 16px; font-size: 14.5px; line-height: 1.6;
  outline: none; max-height: 132px; min-height: 44px;
}
.composer textarea:focus { border-color: var(--brand); }
.composer .icon-btn { margin-bottom: 1px; }

.record-bar {
  flex: none; display: flex; align-items: center; gap: 12px;
  padding: 12px 18px; background: color-mix(in srgb, var(--danger) 9%, var(--panel));
  border-top: 1px solid var(--border); font-size: 13px;
}
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
.rec-hint { color: var(--muted); margin-right: auto; }
@keyframes pulse { 50% { opacity: .25; } }

/* ================= Modal ================= */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(4, 18, 16, .55);
  backdrop-filter: blur(3px); z-index: 60;
  display: grid; place-items: center; padding: 18px;
  animation: fade .14s ease;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  width: 100%; max-width: 460px; max-height: 86dvh; overflow-y: auto;
  background: var(--panel); border-radius: 22px;
  border: 1px solid var(--border); box-shadow: var(--shadow);
  padding: 22px; animation: rise .16s ease;
}
@keyframes rise { from { transform: translateY(12px); opacity: .6; } }
.modal h3 { margin: 0 0 4px; font-size: 17px; }
.modal .sub { margin: 0 0 16px; font-size: 12.5px; color: var(--muted); }
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.modal-close { margin: -6px -6px 0 0; }
.pick-list { display: flex; flex-direction: column; gap: 4px; margin: 12px 0; max-height: 300px; overflow-y: auto; }
.pick-item { display: flex; align-items: center; gap: 11px; padding: 9px 10px; border-radius: 12px; cursor: pointer; }
.pick-item:hover { background: var(--panel-2); }
.pick-item input { width: 18px; height: 18px; accent-color: var(--brand); }
.pick-item .grow { flex: 1; min-width: 0; }
.pick-item .nm { font-size: 14px; font-weight: 600; }
.pick-item .un { font-size: 11px; color: var(--muted); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

.settings-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--border); font-size: 13.5px; }
.settings-row:last-child { border-bottom: 0; }
.settings-row .k { color: var(--muted); font-size: 12.5px; }
.settings-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }

/* ================= Toasts ================= */
.toasts { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 90; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast {
  background: #0f172a; color: #fff; padding: 10px 18px; border-radius: 12px;
  font-size: 13px; box-shadow: 0 10px 26px rgba(0,0,0,.28); max-width: 88vw;
  animation: toastIn .18s ease;
}
.toast.error { background: #7f1d1d; }
.toast.success { background: #14532d; }
@keyframes toastIn { from { transform: translateY(8px); opacity: 0; } }

/* ================= Call ================= */
.call-overlay {
  position: fixed; inset: 0; z-index: 80;
  background: radial-gradient(60% 60% at 50% 35%, #103f3b, #061211 75%);
  display: grid; place-items: center; color: #fff; padding: 20px;
}
.call-card { text-align: center; width: 100%; max-width: 560px; }
.call-card h2 { margin: 0 0 4px; font-size: 22px; }
.call-status { margin: 0 0 20px; font-size: 13px; color: #a7c4c0; }
.call-media { position: relative; border-radius: 20px; overflow: hidden; background: #04100f; margin: 0 auto 22px; aspect-ratio: 3 / 4; max-height: 52dvh; width: min(100%, 380px); }
.call-media video { width: 100%; height: 100%; object-fit: cover; background: #04100f; }
#call-local-video { position: absolute; bottom: 10px; left: 10px; width: 92px; height: 124px; border-radius: 12px; border: 2px solid rgba(255,255,255,.35); object-fit: cover; }
.call-actions { display: flex; justify-content: center; gap: 14px; }
.round-btn {
  width: 58px; height: 58px; border-radius: 50%; border: 0;
  display: grid; place-items: center; color: #fff;
  background: rgba(255,255,255,.16); backdrop-filter: blur(6px);
  transition: transform .08s ease, background .15s ease;
}
.round-btn:hover { background: rgba(255,255,255,.26); }
.round-btn:active { transform: scale(.94); }
.round-btn.accept { background: #16a34a; }
.round-btn.reject { background: #dc2626; }
.round-btn.off { background: rgba(255,255,255,.34); }
.round-btn svg { width: 24px; height: 24px; }

/* ================= Responsive ================= */
@media (max-width: 880px) {
  .sidebar { width: 100%; border-left: 0; }
  .main { display: none; }
  .app-screen.chat-open .sidebar { display: none; }
  .app-screen.chat-open .main { display: flex; }
  .back-btn { display: grid; }
  .bubble { max-width: 82%; }
  .messages { padding: 14px 12px 6px; }
  .composer { padding: 8px 10px calc(10px + env(safe-area-inset-bottom)); }
}

@media (max-width: 420px) {
  .auth-card { padding: 22px; border-radius: 20px; }
  .brand h1 { font-size: 17px; }
  .call-media { max-height: 46dvh; }
  .round-btn { width: 54px; height: 54px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
