:root {
  --bg: #f3ead6;
  --bg2: #ece0c3;
  --panel: #fbf6ea;
  --gold: #c29b3f;
  --gold2: #8f6f22;
  --accent: #1c6b4b;
  --accent2: #0f5638;
  --text: #2f2818;
  --muted: #8a7a58;
  --user: #1c6b4b;
  --border: #d8c69c;
  --radius: 16px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: "Noto Kufi Arabic", "Segoe UI", Tahoma, sans-serif;
  background: linear-gradient(180deg, var(--bg), var(--bg2));
  color: var(--text);
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 0;
  padding: 5px 14px;
  background: rgba(251, 246, 234, 0.94);
  border-bottom: 1.5px solid var(--gold);
  position: sticky;
  top: 0;
  z-index: 5;
  backdrop-filter: blur(8px);
  box-shadow: 0 2px 12px rgba(143, 111, 34, 0.12);
}

.brand {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
}
.brand-logo {
  width: 80%;
  height: auto;
  display: block;
}
.brand-tagline {
  font-size: 10.5px;
  line-height: 1.1;
  font-weight: bold;
  color: var(--gold2);
  white-space: nowrap;
}

.icon-btn {
  width: 42px; height: 42px;
  border: 1px solid var(--accent2);
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fbf6ea;
  border-radius: 12px;
  font-size: 19px;
  cursor: pointer;
  position: relative;
  flex-shrink: 0;
}
.icon-btn:hover { filter: brightness(1.12); border-color: var(--gold); }

.menu-count {
  position: absolute;
  top: -7px; left: -7px;
  background: var(--gold);
  color: #2f2818;
  font-size: 10px;
  font-weight: bold;
  border-radius: 999px;
  padding: 1px 6px;
  display: none;
}
.menu-count.on { display: block; }

/* side drawer */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(47, 38, 20, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
  z-index: 9;
}
.overlay.open { opacity: 1; pointer-events: auto; }

.drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(86vw, 360px);
  background: var(--panel);
  border-left: 1px solid var(--border);
  z-index: 10;
  transform: translateX(105%);
  transition: transform 0.25s ease;
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
}
.drawer.open { transform: translateX(0); }

.drawer.left {
  right: auto;
  left: 0;
  transform: translateX(-105%);
  border-left: none;
  border-right: 1px solid var(--border);
}
.drawer.left.open { transform: translateX(0); }

.topbar-side { display: flex; gap: 8px; }

/* conversations */
.conv-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
}
.conv-item:hover { background: #f1e8d2; border-color: var(--border); }
.conv-item.active { background: rgba(28, 107, 75, 0.10); border-color: var(--accent); }
.conv-text { flex: 1; min-width: 0; }
.conv-title {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-meta { font-size: 10px; color: var(--muted); margin-top: 2px; }
.conv-actions { display: flex; gap: 4px; }
.conv-btn {
  width: 28px; height: 28px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  cursor: pointer;
  font-size: 12px;
}
.conv-btn:hover { color: var(--accent); border-color: var(--accent); }
.conv-btn.del:hover { color: #b3453c; border-color: #b3453c; }

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.drawer-head h2 { font-size: 17px; }

.drawer-actions { display: flex; gap: 8px; }
.mini-btn {
  flex: 1;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fbf6ea;
  border: 1px solid var(--accent2);
  border-radius: 10px;
  padding: 8px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
}
.mini-btn:hover { filter: brightness(1.12); border-color: var(--gold); }

.book-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.book-cat {
  font-size: 12.5px;
  font-weight: bold;
  color: var(--gold2);
  background: rgba(194, 155, 63, 0.12);
  border-right: 3px solid var(--gold);
  border-radius: 8px;
  padding: 6px 10px;
  margin-top: 8px;
  flex-shrink: 0;
}
.book-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
}
.book-item:hover { background: #f1e8d2; border-color: var(--border); }
.book-item input {
  width: 18px; height: 18px;
  accent-color: var(--accent);
  flex-shrink: 0;
}
.book-item .title { flex: 1; font-size: 13.5px; line-height: 1.7; }
.book-item .count {
  font-size: 11px;
  color: var(--muted);
  background: #f1e8d2;
  border-radius: 999px;
  padding: 2px 8px;
  white-space: nowrap;
}
.drawer-hint { font-size: 11px; color: var(--muted); line-height: 1.9; }

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
}

.welcome {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  line-height: 1.9;
}
.welcome h2 { margin-bottom: 8px; font-size: 20px; }
.welcome p { color: var(--muted); font-size: 14px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.chip {
  background: rgba(28, 107, 75, 0.10);
  color: var(--accent);
  border: 1px solid rgba(28, 107, 75, 0.32);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}
.chip:hover { background: rgba(28, 107, 75, 0.20); }

.msg { display: flex; flex-direction: column; max-width: 92%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.bot  { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 2;
  font-size: 15px;
  white-space: normal;
  overflow-wrap: break-word;
}
.msg.user .bubble {
  background: linear-gradient(135deg, var(--user), var(--accent2));
  color: #fbf6ea;
  border-bottom-right-radius: 4px;
}
.msg.bot .bubble {
  background: var(--panel);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.bubble b, .bubble strong { color: var(--accent); }
.msg.user .bubble b, .msg.user .bubble strong { color: #ffd97a; }
.bubble code {
  background: #eee2c6;
  color: var(--text);
  padding: 1px 6px;
  border-radius: 6px;
  font-size: 13px;
}

.quote {
  border-right: 3px solid var(--gold);
  background: rgba(194, 155, 63, 0.10);
  padding: 8px 12px;
  border-radius: 10px;
  margin: 8px 0;
}

.bubble .md-h,
.md-h {
  color: var(--gold2);
  font-size: 15px;
}

/* sources */
.sources { margin-top: 10px; width: 100%; }
.sources-toggle {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
}
.sources-toggle:hover { color: var(--accent); border-color: var(--accent); }
.sources-list { display: none; margin-top: 10px; gap: 10px; }
.sources.open .sources-list { display: grid; }
.source-card {
  background: #f7efdd;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 13px;
  line-height: 1.9;
}
.source-head {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 6px;
}
.tag {
  background: rgba(194, 155, 63, 0.18);
  color: var(--gold2);
  border-radius: 8px;
  padding: 2px 8px;
  font-size: 11px;
}
.tag.ref { background: rgba(28, 107, 75, 0.12); color: var(--accent); }
a.tag.link {
  background: rgba(28, 107, 75, 0.12);
  color: var(--accent);
  text-decoration: underline;
}
.source-text { color: var(--text); }
.source-score { color: var(--muted); font-size: 11px; margin-top: 6px; }

.typing { display: flex; gap: 5px; padding: 14px 18px; }
.typing span {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

.composer {
  padding: 12px 16px calc(14px + env(safe-area-inset-bottom));
  background: rgba(251, 246, 234, 0.95);
  border-top: 1px solid var(--border);
  backdrop-filter: blur(8px);
}
#form {
  display: flex;
  gap: 10px;
  max-width: 860px;
  margin: 0 auto;
}
#input {
  flex: 1;
  resize: none;
  background: #fffdf7;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 14px;
  padding: 12px 16px;
  font-size: 15px;
  font-family: inherit;
  line-height: 1.7;
  max-height: 140px;
}
#input:focus { outline: none; border-color: var(--accent); }
#send {
  width: 48px; height: 48px;
  border: none;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fbf6ea;
  font-size: 20px;
  cursor: pointer;
}
#send:hover { filter: brightness(1.12); }
#send:disabled { opacity: 0.5; cursor: default; }

/* ===== auth: login page ===== */
.login-body { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-wrap { width: 100%; max-width: 420px; padding: 18px; }
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 16px 44px rgba(47, 40, 24, .14);
  padding: 28px 24px;
  text-align: center;
}
.login-card h1 { font-size: 21px; margin: 10px 0 2px; }
.login-tagline { color: var(--muted); font-size: 13px; margin: 0 0 18px; }
.login-tabs {
  display: flex; gap: 6px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 16px;
}
.login-tab {
  flex: 1;
  border: none;
  border-radius: 9px;
  padding: 9px;
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 600;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.login-tab.active { background: var(--panel); color: var(--accent2); box-shadow: 0 1px 6px rgba(47,40,24,.10); }
.login-form { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.login-form input {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg2);
  font-family: inherit;
  font-size: 13.5px;
}
.login-form input:focus { outline: none; border-color: var(--gold); }
.login-btn {
  padding: 12px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fbf6ea;
  font-family: inherit;
  font-size: 14.5px;
  font-weight: 700;
  cursor: pointer;
}
.login-btn:hover { filter: brightness(1.1); }
.link-btn {
  border: none;
  background: none;
  color: var(--accent2);
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
}
.login-msg { min-height: 20px; font-size: 12.5px; color: var(--muted); line-height: 1.9; }
.login-msg.ok { color: var(--accent2); }

/* ===== account bar + drawer footer ===== */
.user-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 10px 16px 2px;
  padding: 10px 12px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.user-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fbf6ea;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  font-weight: 700;
}
.user-meta { min-width: 0; }
.user-name {
  font-size: 13.5px;
  font-weight: 700;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.user-email {
  font-size: 11px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.drawer-foot {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}
.drawer-foot .mini-btn { width: 100%; text-align: center; }
.mini-btn.admin-btn {
  background: var(--bg2);
  color: var(--text);
}
.mini-btn.logout-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: #9c2f2f;
}
.mini-btn.logout-btn:hover { border-color: #9c2f2f; filter: none; }
.mini-btn.danger {
  background: #9c2f2f;
  color: #fff;
  border: none;
  padding: 5px 12px;
}
.mini-btn.danger:hover { filter: brightness(1.1); }

/* ===== admin panel modal ===== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(47, 40, 24, .42);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 300;
}
.modal[hidden] { display: none; }
.modal-card {
  width: 100%;
  max-width: 640px;
  max-height: 86vh;
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.modal-head h2 { font-size: 17px; }
.modal-tabs {
  display: flex;
  gap: 6px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 14px;
}
.modal-tab {
  flex: 1;
  border: none;
  border-radius: 9px;
  padding: 9px;
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 600;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.modal-tab.active { background: var(--panel); color: var(--accent2); }
.users-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.users-table th, .users-table td {
  padding: 9px 8px;
  border-bottom: 1px solid var(--border);
  text-align: right;
}
.users-table th { color: var(--muted); font-size: 11.5px; font-weight: 600; }
.smtp-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.smtp-form label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  text-align: right;
}
.smtp-form input[type="text"],
.smtp-form input[type="number"],
.smtp-form input[type="password"] {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--bg2);
  font-family: inherit;
  font-size: 13px;
}
.smtp-form input:focus { outline: none; border-color: var(--gold); }
.check-row {
  flex-direction: row !important;
  align-items: center;
  gap: 8px !important;
}
.smtp-actions {
  display: flex;
  gap: 10px;
}
.smtp-actions .mini-btn { flex: 1; text-align: center; }
.hint {
  text-align: center;
  color: var(--muted);
  font-size: 11px;
  margin-top: 8px;
}

@media (max-width: 640px) {
  .msg { max-width: 100%; }
}
