:root {
  --bg: #1e1f22;
  --bg-alt: #26272b;
  --bg-elevated: #2d2e33;
  --border: #3a3b40;
  --text: #e7e7ea;
  --text-dim: #9a9ba1;
  --accent: #6a8dff;
  --accent-hover: #7f9dff;
  --danger: #e5636b;
  --radius: 8px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
}

button, input, textarea {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

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

.btn {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
}
.btn:hover { background: #34353b; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #10131e; font-weight: 600; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.danger:hover { background: rgba(229, 99, 107, 0.1); }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn.full { width: 100%; }

/* ---------- Auth pages ---------- */

.auth-page {
  height: 100vh;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.auth-card {
  width: 340px;
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px;
}
.auth-card h1 { margin: 0 0 4px; font-size: 20px; }
.auth-sub { color: var(--text-dim); margin: 0 0 20px; font-size: 13px; }
.auth-foot { text-align: center; margin-top: 14px; font-size: 13px; color: var(--text-dim); }
.field { margin-bottom: 14px; }
.field label { display: block; margin-bottom: 6px; font-size: 13px; color: var(--text-dim); }
.field .optional { font-weight: 400; color: var(--text-dim); }
.field input {
  width: 100%;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 10px;
}
.field input:focus { outline: none; border-color: var(--accent); }
.error { color: var(--danger); font-size: 13px; margin-bottom: 12px; }
.success { color: #67d38a; font-size: 13px; margin-bottom: 12px; }

/* ---------- App shell (chat / content / admin) ---------- */

.app-shell {
  height: 100vh;
  height: 100dvh;
  display: flex;
}

.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--bg-alt);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px;
}

.new-chat-btn { margin-bottom: 16px; }

.sidebar-section-label {
  color: var(--text-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 6px 4px;
}

.recents-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.recent-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  gap: 6px;
}
.recent-item:hover { background: var(--bg-elevated); }
.recent-item.active { background: var(--bg-elevated); }
.recent-item .title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}
.recent-item .del-btn {
  visibility: hidden;
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
}
.recent-item:hover .del-btn { visibility: visible; }
.recent-item .del-btn:hover { color: var(--danger); }

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding-top: 12px;
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.nav-link {
  padding: 8px 10px;
  border-radius: var(--radius);
  color: var(--text);
  display: block;
}
.nav-link:hover { background: var(--bg-elevated); text-decoration: none; }

.user-chip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
  padding: 6px 4px;
}
.user-chip-name { color: var(--text-dim); font-size: 13px; }

.main-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

/* ---------- Chat ---------- */

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 24px 12%;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.messages[hidden] { display: none; }

.msg { display: flex; flex-direction: column; gap: 4px; max-width: 100%; min-width: 0; }
.msg .role { font-size: 12px; color: var(--text-dim); }
.msg .bubble {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  line-height: 1.5;
}
.msg.user .bubble { background: var(--bg-elevated); }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 16px 12%;
  border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1;
  resize: none;
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  max-height: 200px;
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.attach-btn {
  padding: 8px 12px;
  font-size: 16px;
  line-height: 1;
}

.pending-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 12% 0;
}

.attachment-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 2px;
}

.attachment-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 10px 4px 6px;
  max-width: 220px;
}
.attachment-chip img {
  width: 22px;
  height: 22px;
  object-fit: cover;
  border-radius: 5px;
}
.attachment-icon { font-size: 15px; }
.attachment-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  color: var(--text);
}
.attachment-remove {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 0;
  font-size: 12px;
}
.attachment-remove:hover { color: var(--danger); }

/* ---------- Mobile top bar + drawer sidebar ---------- */

.mobile-topbar { display: none; }
.sidebar-backdrop { display: none; }

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 20px;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
}

.user-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: #10131e;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  flex-shrink: 0;
  margin-bottom: 2px;
}

/* ---------- Welcome / empty-state screen ---------- */

.welcome-screen {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px 20px;
  gap: 26px;
}
.welcome-screen[hidden] { display: none; }
.welcome-title {
  font-size: 26px;
  font-weight: 700;
  margin: 8px 0 0;
  text-align: center;
}

@media (max-width: 768px) {
  .mobile-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 6px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-alt);
    flex-shrink: 0;
  }
  .mobile-title { font-weight: 600; font-size: 16px; }

  .app-shell { flex-direction: column; position: relative; overflow: hidden; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 82%;
    max-width: 300px;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .sidebar.open { transform: translateX(0); }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 30;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }
  .sidebar-backdrop.open { opacity: 1; pointer-events: auto; }

  .main-panel { width: 100%; }

  .messages { padding: 18px 5%; }
  .composer { padding: 12px 4%; padding-bottom: max(12px, env(safe-area-inset-bottom)); }
  .pending-attachments { padding: 10px 5% 0; }
  .welcome-screen { padding: 24px 16px 16px; }
  .welcome-title { font-size: 22px; }
}

@media (min-width: 769px) {
  .mobile-topbar, .sidebar-backdrop { display: none !important; }
}

/* ---------- Content panel ---------- */

.content-panel { padding: 0; }
.content-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
.content-title-input {
  flex: 1;
  background: transparent;
  border: none;
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}
.content-title-input:focus { outline: none; }
.content-toolbar-actions { display: flex; gap: 8px; }

#editor {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  background: var(--bg);
  color: var(--text);
}
.ql-toolbar.ql-snow { border: none; border-bottom: 1px solid var(--border); background: var(--bg-alt); }
.ql-container.ql-snow { border: none; font-size: 14px; }
.ql-editor { min-height: 400px; padding: 24px 12%; }
.ql-editor img, .ql-editor video { max-width: 100%; border-radius: 8px; }
.ql-editor audio, .ql-editor video { display: block; margin: 10px 0; }
.ql-snow .ql-stroke { stroke: var(--text-dim); }
.ql-snow .ql-fill { fill: var(--text-dim); }
.ql-snow .ql-picker { color: var(--text-dim); }

/* ---------- Admin ---------- */

.admin-panel { flex: 1; min-height: 0; padding: 24px 32px; overflow-y: auto; }
.hint { color: var(--text-dim); font-size: 13px; margin-top: -8px; }
.user-table { width: 100%; border-collapse: collapse; margin-top: 16px; }
.user-table th {
  text-align: left;
  color: var(--text-dim);
  font-size: 12px;
  text-transform: uppercase;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.user-table td {
  padding: 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.user-table .actions { display: flex; gap: 6px; }
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
}
.badge.admin { color: var(--accent); }
.badge.disabled { color: var(--danger); border-color: var(--danger); margin-left: 4px; }
.settings-form { max-width: 480px; margin-top: 16px; }
