:root {
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-soft: #eef2ff;
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --red: #ef4444;
  --red-soft: #fef2f2;
  --green: #16a34a;
  --green-soft: #f0fdf4;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  font-family: Vazirmatn, Tahoma, sans-serif;
  color: var(--text);
  height: 100%;
}

#app { height: 100%; display: flex; flex-direction: column; max-width: 640px; margin: 0 auto; }

button { font-family: inherit; cursor: pointer; }
input, textarea { font-family: inherit; }

/* ───────── هدر ───────── */
.topbar {
  background: var(--primary);
  color: #fff;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar h1 { font-size: 17px; font-weight: 700; margin: 0; flex: 1; }
.topbar .icon-btn {
  background: rgba(255,255,255,.12);
  border: 0;
  color: #fff;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.topbar .icon-btn:active { background: rgba(255,255,255,.25); }

/* ───────── صفحه ورود ───────── */
.login-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-logo {
  width: 96px;
  height: 96px;
  border-radius: 28px;
  margin-bottom: 20px;
}
.login-wrap h2 { font-size: 22px; margin: 0 0 6px; color: var(--text); }
.login-wrap .sub { color: var(--muted); font-size: 14px; margin-bottom: 28px; }
.login-form { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; }
.field {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  font-size: 15px;
  width: 100%;
  outline: none;
  transition: border-color .15s;
}
.field:focus { border-color: var(--primary); }
.field.ltr { direction: ltr; text-align: left; }
.btn-primary {
  background: var(--primary);
  color: #fff;
  border: 0;
  border-radius: var(--radius);
  padding: 15px;
  font-size: 16px;
  font-weight: 600;
}
.btn-primary:active { background: var(--primary-dark); }
.btn-primary:disabled { opacity: .6; }

.error-box {
  background: var(--red-soft);
  color: #b91c1c;
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 13px;
  text-align: center;
}

/* ───────── جستجو و فیلترها ───────── */
.list-tools { padding: 12px 16px 4px; background: var(--bg); position: sticky; top: 62px; z-index: 10; }
.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 14px;
}
.search-box input { border: 0; outline: none; flex: 1; font-size: 14px; background: transparent; }
.search-box span { color: var(--muted); }

.filter-tabs { display: flex; gap: 8px; margin-top: 10px; }
.tab {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--muted);
  border-radius: 999px;
  padding: 7px 18px;
  font-size: 13px;
  font-weight: 500;
}
.tab.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.tab .cnt { font-size: 11px; opacity: .8; margin-right: 4px; }

/* ───────── لیست تیکت‌ها ───────── */
.ticket-list { flex: 1; overflow-y: auto; padding: 8px 16px calc(24px + var(--safe-bottom)); }
.ticket-item {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  border: 1px solid transparent;
  cursor: pointer;
  transition: border-color .15s;
}
.ticket-item:active { border-color: var(--primary); }
.ticket-item.unread { border-inline-start: 4px solid var(--primary); }

.avatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  flex-shrink: 0;
}
.ticket-item .info { flex: 1; min-width: 0; }
.ticket-item .name { font-weight: 700; font-size: 14px; }
.ticket-item .preview {
  color: var(--muted);
  font-size: 13px;
  margin-top: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ticket-item .meta { text-align: left; flex-shrink: 0; display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.ticket-item .time { color: var(--muted); font-size: 11px; }

.chip { border-radius: 999px; padding: 3px 12px; font-size: 11px; font-weight: 600; }
.chip.open { background: var(--red-soft); color: var(--red); }
.chip.closed { background: var(--green-soft); color: var(--green); }

.empty-state { text-align: center; color: var(--muted); padding: 60px 20px; font-size: 14px; }
.empty-state .big { font-size: 44px; margin-bottom: 12px; }

/* ───────── صفحه تیکت (چت) ───────── */
.ticket-head {
  background: var(--card);
  margin: 12px 16px 0;
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  gap: 12px;
  align-items: center;
}
.ticket-head .info { flex: 1; min-width: 0; }
.ticket-head .subject { font-weight: 700; font-size: 14px; }
.ticket-head .sub { color: var(--muted); font-size: 12px; margin-top: 4px; }
.status-toggle {
  border: 0;
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 12px;
  font-weight: 700;
  flex-shrink: 0;
}
.status-toggle.open { background: var(--red-soft); color: var(--red); }
.status-toggle.closed { background: var(--green-soft); color: var(--green); }

.chat-area { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.bubble-row { display: flex; flex-direction: column; max-width: 82%; }
.bubble-row.user { align-self: flex-start; align-items: flex-start; }
.bubble-row.admin { align-self: flex-end; align-items: flex-end; }
.bubble-row .who { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.9;
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble-row.user .bubble { background: var(--card); border: 1px solid var(--border); border-start-start-radius: 4px; }
.bubble-row.admin .bubble { background: var(--primary); color: #fff; border-start-end-radius: 4px; }
.bubble img { max-width: 220px; border-radius: 10px; display: block; }
.bubble a { color: inherit; }
.bubble-row .t { font-size: 10px; color: var(--muted); margin-top: 4px; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 16px calc(10px + var(--safe-bottom));
  background: var(--card);
  border-top: 1px solid var(--border);
  position: sticky;
  bottom: 0;
}
.composer textarea {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 11px 14px;
  font-size: 14px;
  resize: none;
  outline: none;
  max-height: 120px;
  background: var(--bg);
}
.composer .round-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 0;
  font-size: 17px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: var(--bg);
  color: var(--muted);
}
.composer .round-btn.send { background: var(--primary); color: #fff; }
.composer .round-btn:disabled { opacity: .5; }

.ai-bar { padding: 6px 16px; background: var(--card); border-top: 1px solid var(--border); }
.ai-btn {
  width: 100%;
  border: 1px dashed var(--primary);
  color: var(--primary);
  background: var(--primary-soft);
  border-radius: 12px;
  padding: 9px;
  font-size: 13px;
  font-weight: 600;
}
.attach-name { font-size: 11px; color: var(--primary); padding: 4px 16px 0; background: var(--card); }

/* ───────── اطلاعات کاربر ───────── */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 40;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet {
  background: var(--bg);
  border-radius: 22px 22px 0 0;
  width: 100%;
  max-width: 640px;
  max-height: 80vh;
  overflow-y: auto;
  padding: 20px 20px calc(28px + var(--safe-bottom));
  animation: slideUp .22s ease;
}
@keyframes slideUp { from { transform: translateY(40%); opacity: .4; } to { transform: none; opacity: 1; } }
.sheet .grab { width: 44px; height: 5px; background: var(--border); border-radius: 999px; margin: 0 auto 18px; }
.user-card { background: var(--card); border-radius: var(--radius); padding: 18px; }
.user-card .center { text-align: center; margin-bottom: 14px; }
.user-card .center .avatar { width: 64px; height: 64px; font-size: 26px; margin: 0 auto 10px; }
.user-card .center .nm { font-weight: 700; font-size: 16px; }
.kv { display: flex; justify-content: space-between; padding: 11px 0; border-bottom: 1px solid var(--bg); font-size: 13px; }
.kv:last-child { border-bottom: 0; }
.kv .k { color: var(--muted); }
.kv .v { font-weight: 600; direction: ltr; }

/* ───────── تنظیمات ───────── */
.settings-wrap { flex: 1; overflow-y: auto; padding: 16px 16px calc(24px + var(--safe-bottom)); }
.settings-group { background: var(--card); border-radius: var(--radius); margin-bottom: 16px; overflow: hidden; }
.settings-title { font-size: 12px; color: var(--muted); margin: 4px 6px 8px; }
.settings-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 15px 16px;
  border-bottom: 1px solid var(--bg);
  font-size: 14px;
  width: 100%;
  background: none;
  border-inline: 0;
  border-top: 0;
  text-align: right;
  color: var(--text);
}
.settings-row:last-child { border-bottom: 0; }
.settings-row .ic { font-size: 18px; width: 26px; text-align: center; }
.settings-row .grow { flex: 1; }
.settings-row.danger { color: var(--red); }
.settings-form { padding: 16px; display: flex; flex-direction: column; gap: 12px; }

.toast {
  position: fixed;
  bottom: calc(90px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: #111827;
  color: #fff;
  border-radius: 999px;
  padding: 10px 22px;
  font-size: 13px;
  z-index: 60;
  animation: fadeIn .2s ease;
  white-space: nowrap;
  max-width: 90vw;
  overflow: hidden;
  text-overflow: ellipsis;
}
@keyframes fadeIn { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

.spinner {
  width: 26px;
  height: 26px;
  border: 3px solid var(--primary-soft);
  border-top-color: var(--primary);
  border-radius: 50%;
  margin: 40px auto;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
