:root {
  --brand-primary: #2b4f91;
  --brand-accent:  #e85028;
  --brand-hover:   #1f3d73;
  --bg-page:       #eef1f5;
  --bg-card:       white;
  --text-primary:  #1a1a2e;
  --text-muted:    #6b7280;
  --border:        #d1d5db;
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Vazirmatn', sans-serif;
  font-size: 15px;
  background: var(--bg-page);
  color: var(--text-primary);
  direction: rtl;
}

a { color: var(--brand-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Flash messages */
.flash-container {
  position: fixed;
  top: 1rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  transition: opacity 0.5s;
}
.flash {
  padding: .75rem 1.5rem;
  border-radius: 8px;
  margin-bottom: .5rem;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
}
.flash-success { background: #d1fae5; color: #065f46; }
.flash-error   { background: #fee2e2; color: #991b1b; }
.flash-info    { background: #e8f0fe; color: var(--brand-primary); }

/* ===== AUTH PAGES ===== */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.auth-card {
  background: white;
  border-radius: 16px;
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 8px 32px rgba(0,0,0,.1);
  border-top: 4px solid var(--brand-primary);
}
.auth-logo {
  text-align: center;
  margin-bottom: 2rem;
}
.auth-logo h2 { font-size: 1.3rem; margin: .75rem 0 .25rem; color: var(--brand-primary); }
.auth-logo p  { color: var(--text-muted); margin: 0; font-size: .9rem; }

.auth-company-logo {
  max-width: 220px;
  max-height: 80px;
  object-fit: contain;
  margin-bottom: .5rem;
}

.auth-form { display: flex; flex-direction: column; gap: 1rem; }
.form-row { display: flex; gap: .75rem; }
.form-row .form-group { flex: 1; }
.req { color: var(--brand-accent); font-size: .8rem; }
.form-group { display: flex; flex-direction: column; gap: .4rem; }
.form-group label { font-weight: 500; font-size: .9rem; }
.form-group input, .form-group textarea, .settings-form textarea {
  padding: .75rem 1rem;
  border: 1.5px solid #d1d5db;
  border-radius: 8px;
  font-family: inherit;
  font-size: .95rem;
  outline: none;
  transition: border-color .2s;
}
.form-group input:focus, .form-group textarea:focus { border-color: var(--brand-primary); }

.btn-primary {
  background: var(--brand-primary);
  color: white;
  border: none;
  border-radius: 8px;
  padding: .85rem;
  font-size: 1rem;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background .2s;
  width: 100%;
}
.btn-primary:hover { background: var(--brand-hover); }

.btn-secondary {
  background: #e5e7eb;
  color: #374151;
  border: none;
  border-radius: 8px;
  padding: .75rem 1.5rem;
  font-size: .95rem;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
}

.auth-footer { text-align: center; margin-top: 1.5rem; font-size: .9rem; color: #666; }

/* OTP inputs */
.otp-inputs {
  display: flex;
  gap: .5rem;
  justify-content: center;
  direction: ltr;
}
.otp-digit {
  width: 48px;
  height: 56px;
  text-align: center;
  font-size: 1.4rem;
  font-weight: 700;
  border: 2px solid #d1d5db;
  border-radius: 8px;
  outline: none;
  font-family: monospace;
}
.otp-digit:focus { border-color: var(--brand-primary); }

.resend-row {
  text-align: center;
  font-size: .85rem;
  color: #666;
  margin-top: .5rem;
}

/* ===== CHAT LAYOUT ===== */
.chat-layout {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  width: 280px;
  min-width: 280px;
  background: white;
  border-left: 1px solid #e5e7eb;
  display: flex;
  flex-direction: column;
  transition: margin-right .3s;
}
.sidebar.hidden { margin-right: -280px; }

.sidebar-header {
  padding: 1rem;
  border-bottom: 1px solid #e5e7eb;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: .5rem;
  margin-bottom: .5rem;
}
.sidebar-brand img {
  height: 32px;
  object-fit: contain;
}
.sidebar-brand span {
  font-size: .95rem;
  font-weight: 700;
  color: var(--brand-primary);
}
.user-info { font-size: .8rem; color: #666; display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; }
.admin-link { color: #7c3aed; }
.logout-link { color: #dc2626; }

.new-chat-btn {
  margin: .75rem;
  padding: .65rem;
  background: var(--brand-primary);
  color: white;
  border: none;
  border-radius: 8px;
  font-family: inherit;
  font-size: .9rem;
  cursor: pointer;
  font-weight: 500;
}
.new-chat-btn:hover { background: var(--brand-hover); }

.conv-list { flex: 1; overflow-y: auto; padding: .5rem; }
.conv-loading { text-align: center; color: #999; padding: 1rem; font-size: .9rem; }

.conv-item {
  padding: .65rem .75rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: .88rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  color: #374151;
  transition: background .15s;
}
.conv-item:hover { background: #f3f4f6; }
.conv-item.active { background: #e8f0fe; color: var(--brand-primary); }
.conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-del {
  background: none;
  border: none;
  color: #9ca3af;
  cursor: pointer;
  font-size: .9rem;
  padding: 2px 4px;
  border-radius: 4px;
  display: none;
}
.conv-item:hover .conv-del { display: block; }
.conv-del:hover { color: #dc2626; background: #fee2e2; }

/* Chat main */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-topbar {
  padding: .75rem 1rem;
  background: white;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  align-items: center;
  gap: .75rem;
}
.sidebar-toggle {
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  color: #666;
  padding: .25rem .5rem;
  border-radius: 6px;
}
.sidebar-toggle:hover { background: #f3f4f6; }
.chat-title { flex: 1; font-weight: 600; font-size: 1rem; color: #1a1a2e; }
.model-picker {
  font-family: inherit;
  font-size: .82rem;
  padding: .3rem .6rem;
  border: 1.5px solid #d1d5db;
  border-radius: 20px;
  background: #f9fafb;
  color: #374151;
  cursor: pointer;
  outline: none;
  transition: border-color .2s;
}
.model-picker:focus { border-color: var(--brand-primary); }

.ai-badge {
  font-size: .8rem;
  padding: .25rem .6rem;
  border-radius: 20px;
  font-weight: 600;
}
.ai-badge.claude  { background: #e8f0fe; color: var(--brand-primary); }
.ai-badge.offline { background: #fef3c7; color: #92400e; }
.ai-badge.loading { background: #f3f4f6; color: #6b7280; }

.messages-area {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .75rem;
}

.welcome-msg {
  text-align: center;
  padding: 3rem 1rem;
  color: #9ca3af;
}
.welcome-icon { font-size: 3rem; margin-bottom: 1rem; }
.welcome-msg h2 { color: #374151; margin: 0 0 .5rem; }
.welcome-msg p  { margin: 0; font-size: .9rem; }

.msg-row { display: flex; flex-direction: row; direction: ltr; }
.msg-row.user    { justify-content: flex-end; }
.msg-row.assistant { justify-content: flex-start; }
.msg-bubble { direction: rtl; text-align: right; }

.msg-bubble {
  max-width: 72%;
  padding: .75rem 1rem;
  border-radius: 16px;
  line-height: 1.6;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.msg-row.user .msg-bubble {
  background: var(--brand-primary);
  color: white;
  border-bottom-right-radius: 4px;
}
.msg-row.assistant .msg-bubble {
  background: white;
  color: #1a1a2e;
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
}

.msg-meta {
  font-size: .72rem;
  color: #9ca3af;
  margin-top: .3rem;
  padding: 0 .5rem;
}
.msg-row.user .msg-meta { text-align: right; }
.msg-row.assistant .msg-meta { text-align: left; }

.msg-image {
  max-width: 260px;
  border-radius: 8px;
  margin-bottom: .5rem;
  display: block;
}

/* Typing indicator */
.typing-bubble { display: flex; gap: 4px; align-items: center; padding: .75rem 1rem; }
.typing-bubble span {
  width: 8px; height: 8px;
  background: #9ca3af;
  border-radius: 50%;
  animation: bounce .8s infinite;
}
.typing-bubble span:nth-child(2) { animation-delay: .15s; }
.typing-bubble span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%,60%,100%{transform:translateY(0)} 30%{transform:translateY(-6px)} }

/* Input area */
.input-area {
  padding: .75rem 1rem 1rem;
  background: white;
  border-top: 1px solid #e5e7eb;
}
.image-preview-wrap { margin-bottom: .5rem; }
.image-preview-inner {
  display: inline-block;
  position: relative;
}
.image-preview-inner img {
  max-height: 100px;
  border-radius: 8px;
  border: 1px solid #e5e7eb;
}
.remove-image {
  position: absolute;
  top: -8px;
  left: -8px;
  background: #dc2626;
  color: white;
  border: none;
  border-radius: 50%;
  width: 22px;
  height: 22px;
  cursor: pointer;
  font-size: .75rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.input-row {
  display: flex;
  align-items: flex-end;
  gap: .5rem;
  background: #f3f4f6;
  border-radius: 12px;
  padding: .5rem;
}
.attach-btn {
  font-size: 1.2rem;
  cursor: pointer;
  padding: .4rem .6rem;
  border-radius: 8px;
  color: #6b7280;
  transition: background .15s;
  flex-shrink: 0;
}
.attach-btn:hover { background: #e5e7eb; }

#message-input {
  flex: 1;
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: .95rem;
  resize: none;
  outline: none;
  max-height: 160px;
  overflow-y: auto;
  line-height: 1.5;
  padding: .3rem 0;
}

.send-btn {
  background: var(--brand-primary);
  color: white;
  border: none;
  border-radius: 8px;
  width: 40px;
  height: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .15s;
}
.send-btn:hover { background: var(--brand-hover); }
.send-btn:disabled { background: #9ca3af; cursor: default; }

/* ===== ADMIN LAYOUT ===== */
.admin-layout {
  display: flex;
  min-height: 100vh;
}
.admin-nav {
  width: 220px;
  min-width: 220px;
  background: #1e293b;
  color: white;
  padding: 1.5rem 0;
  display: flex;
  flex-direction: column;
}
.admin-brand {
  font-size: 1rem;
  font-weight: 700;
  padding: 0 1.25rem 1.5rem;
  border-bottom: 1px solid rgba(255,255,255,.1);
  margin-bottom: .75rem;
}
.admin-nav a {
  padding: .65rem 1.25rem;
  color: rgba(255,255,255,.75);
  font-size: .9rem;
  display: block;
  transition: background .15s;
}
.admin-nav a:hover, .admin-nav a.active {
  background: rgba(255,255,255,.1);
  color: white;
  text-decoration: none;
}

.admin-content { flex: 1; padding: 2rem; max-width: 900px; }
.admin-content h2 { margin-top: 0; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-card {
  background: white;
  border-radius: 12px;
  padding: 1.25rem;
  text-align: center;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.stat-num { font-size: 2rem; font-weight: 700; color: var(--brand-primary); }
.stat-card.pending .stat-num { color: #f59e0b; }
.stat-card.active .stat-num  { color: #10b981; }
.stat-card.msgs .stat-num    { color: #8b5cf6; }
.stat-label { font-size: .85rem; color: #6b7280; margin-top: .25rem; }

.alert-pending {
  background: #fef3c7;
  border: 1px solid #fcd34d;
  border-radius: 8px;
  padding: .75rem 1rem;
  font-size: .9rem;
  color: #92400e;
}
.alert-pending a { color: #92400e; font-weight: 600; }

.filter-tabs { display: flex; gap: .5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.filter-tabs a {
  padding: .4rem .9rem;
  border-radius: 20px;
  font-size: .85rem;
  color: #374151;
  background: #f3f4f6;
}
.filter-tabs a.active { background: var(--brand-primary); color: white; text-decoration: none; }

.users-table {
  width: 100%;
  border-collapse: collapse;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.users-table th {
  background: #f9fafb;
  padding: .75rem 1rem;
  text-align: right;
  font-size: .85rem;
  color: #6b7280;
  font-weight: 600;
  border-bottom: 1px solid #e5e7eb;
}
.users-table td {
  padding: .75rem 1rem;
  border-bottom: 1px solid #f3f4f6;
  font-size: .9rem;
}

.badge {
  padding: .2rem .6rem;
  border-radius: 20px;
  font-size: .8rem;
  font-weight: 600;
}
.badge-pending { background: #fef3c7; color: #92400e; }
.badge-active  { background: #d1fae5; color: #065f46; }
.badge-blocked { background: #fee2e2; color: #991b1b; }

.actions { display: flex; gap: .5rem; }
.btn-approve, .btn-block {
  padding: .3rem .75rem;
  border: none;
  border-radius: 6px;
  font-size: .82rem;
  cursor: pointer;
  font-family: inherit;
  font-weight: 500;
}
.btn-approve { background: #d1fae5; color: #065f46; }
.btn-approve:hover { background: #a7f3d0; }
.btn-block   { background: #fee2e2; color: #991b1b; }
.btn-block:hover { background: #fecaca; }

.model-select {
  padding: .25rem .5rem;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-family: inherit;
  font-size: .85rem;
  background: #f9fafb;
  cursor: pointer;
}
.model-select:focus { outline: none; border-color: var(--brand-primary); }

.settings-form { max-width: 700px; display: flex; flex-direction: column; gap: 1.25rem; }
.settings-form textarea {
  border: 1.5px solid #d1d5db;
  border-radius: 8px;
  padding: .75rem;
  font-family: monospace;
  font-size: .88rem;
  resize: vertical;
  width: 100%;
  outline: none;
}
.settings-form textarea:focus { border-color: var(--brand-primary); }
.settings-form .btn-primary { width: auto; padding: .75rem 2rem; }

/* Overlay for mobile sidebar */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 99;
}
.sidebar-overlay.visible { display: block; }

/* Responsive */
@media (max-width: 640px) {
  .sidebar {
    position: fixed;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 100;
    box-shadow: -4px 0 16px rgba(0,0,0,.2);
    margin-right: -280px;
    transition: margin-right .25s;
  }
  .sidebar.open { margin-right: 0; }
  .msg-bubble { max-width: 88%; }
  .admin-nav { width: 160px; min-width: 160px; }
  .admin-content { padding: 1rem; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .form-row { flex-direction: column; gap: 0; }
  .input-area {
    padding-bottom: calc(.75rem + env(safe-area-inset-bottom));
  }
  .model-select { font-size: .8rem; }
  .users-table th, .users-table td { padding: .5rem .6rem; font-size: .8rem; }
  .auth-page { padding: .5rem; align-items: flex-start; padding-top: 1.5rem; }
  .auth-card { padding: 1.5rem 1.25rem; border-radius: 12px; }
  .auth-company-logo { max-width: 160px; max-height: 60px; }
  .otp-digit { width: 42px; height: 50px; font-size: 1.2rem; }
}
@media (min-width: 641px) {
  .sidebar { transition: margin-right .25s; }
  .sidebar.hidden { margin-right: -280px; }
}
