/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-main: #212121;
  --bg-sidebar: #171717;
  --bg-input: #2f2f2f;
  --bg-hover: #2a2a2a;
  --bg-active: #3a3a3a;
  --bg-msg-user: #2f2f2f;
  --text-primary: #ececec;
  --text-secondary: #a0a0a0;
  --text-muted: #6b6b6b;
  --accent: #10a37f;
  --accent-hover: #0d8f6f;
  --danger: #e55353;
  --danger-hover: #c94343;
  --border: #383838;
  --scrollbar-thumb: #4a4a4a;
  --radius: 8px;
  --sidebar-w: 260px;
  --transition: 150ms ease;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg-main);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.5;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 3px; }

/* ===== Login Page ===== */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg-main);
}

.login-card {
  background: #2a2a2a;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 40px 48px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 8px 32px rgba(0,0,0,.4);
}

.login-logo {
  text-align: center;
  margin-bottom: 28px;
}

.login-logo svg { color: var(--accent); }

.login-logo h1 {
  font-size: 1.5rem;
  font-weight: 600;
  margin-top: 12px;
  color: var(--text-primary);
}

.login-logo p { color: var(--text-secondary); font-size: 0.875rem; margin-top: 4px; }

.form-group { margin-bottom: 16px; }

.form-group label {
  display: block;
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-bottom: 6px;
  font-weight: 500;
}

.form-control {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.9375rem;
  outline: none;
  transition: border-color var(--transition);
}

.form-control:focus { border-color: var(--accent); }

.btn-primary {
  width: 100%;
  padding: 10px 14px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 0.9375rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition);
  margin-top: 8px;
}

.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.alert-error {
  background: rgba(229, 83, 83, 0.12);
  border: 1px solid rgba(229, 83, 83, 0.4);
  border-radius: var(--radius);
  color: #f87171;
  padding: 10px 14px;
  font-size: 0.875rem;
  margin-bottom: 16px;
}

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

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  overflow: hidden;
}

.sidebar-top {
  padding: 12px 8px;
  border-bottom: 1px solid var(--border);
}

.btn-new-chat {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.875rem;
  cursor: pointer;
  transition: background var(--transition);
}

.btn-new-chat:hover { background: var(--bg-hover); }

.sidebar-conversations {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.sidebar-section-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  padding: 8px 10px 4px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 0.875rem;
  transition: background var(--transition);
  group: true;
}

.conv-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.conv-item.active { background: var(--bg-active); color: var(--text-primary); }

.conv-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-delete-btn {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
  transition: opacity var(--transition), color var(--transition);
  flex-shrink: 0;
}

.conv-item:hover .conv-delete-btn { opacity: 1; }
.conv-delete-btn:hover { color: var(--danger); }

.sidebar-bottom {
  padding: 12px 8px;
  border-top: 1px solid var(--border);
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius);
}

.user-avatar {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8125rem;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
}

.user-info { flex: 1; overflow: hidden; }
.user-name { font-size: 0.875rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 0.75rem; color: var(--text-muted); }

.user-actions { display: flex; gap: 4px; }

.icon-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: color var(--transition), background var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover { color: var(--text-primary); background: var(--bg-hover); }

/* ===== Main Chat Area ===== */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-main);
}

/* ===== Welcome Screen ===== */
.welcome-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--text-secondary);
}

.welcome-screen svg { color: var(--accent); opacity: 0.7; }
.welcome-screen h2 { font-size: 1.5rem; color: var(--text-primary); font-weight: 600; }
.welcome-screen p { font-size: 0.9375rem; text-align: center; max-width: 420px; }

/* ===== Messages Area ===== */
.messages-area {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
}

.message-row {
  max-width: 780px;
  margin: 0 auto;
  padding: 12px 24px;
  display: flex;
  gap: 14px;
}

.message-row.user { flex-direction: row-reverse; }

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8125rem;
  font-weight: 700;
}

.msg-avatar.user { background: var(--bg-active); color: var(--text-secondary); }
.msg-avatar.assistant { background: var(--accent); color: #fff; }

.msg-body { max-width: 70%; }

.message-row.user .msg-body { align-items: flex-end; display: flex; flex-direction: column; }

.msg-prompt {
  background: var(--bg-msg-user);
  border-radius: 12px 12px 2px 12px;
  padding: 10px 14px;
  font-size: 0.9375rem;
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-word;
}

.message-row.assistant .msg-prompt {
  border-radius: 12px 12px 12px 2px;
}

.msg-upload-preview {
  max-width: 200px;
  max-height: 200px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  margin-bottom: 6px;
  display: block;
}

.msg-images {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 4px;
}

.msg-image-wrap {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
  background: #1a1a1a;
}

.msg-image {
  display: block;
  max-width: 480px;
  max-height: 480px;
  width: 100%;
  height: auto;
  border-radius: var(--radius);
  cursor: zoom-in;
}

.msg-image-download {
  position: absolute;
  bottom: 8px;
  right: 8px;
  background: rgba(0,0,0,.6);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 0.75rem;
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--transition);
  display: flex;
  align-items: center;
  gap: 4px;
  text-decoration: none;
}

.msg-image-wrap:hover .msg-image-download { opacity: 1; }

.msg-time {
  font-size: 0.6875rem;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ===== Loading message ===== */
.msg-loading {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-secondary);
  font-size: 0.875rem;
  padding: 10px 0;
}

.dots-loading span {
  display: inline-block;
  width: 6px; height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.2s infinite ease-in-out;
}

.dots-loading span:nth-child(2) { animation-delay: 0.2s; }
.dots-loading span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* ===== Input Bar ===== */
.input-bar-wrapper {
  padding: 16px 24px 20px;
  background: var(--bg-main);
}

.input-bar {
  max-width: 780px;
  margin: 0 auto;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 14px;
  transition: border-color var(--transition);
}

.input-bar:focus-within { border-color: #555; }

/* Upload preview */
.upload-preview {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 0 6px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 6px;
}

.upload-preview.visible { display: flex; }

.upload-thumb {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.upload-info { flex: 1; overflow: hidden; }
.upload-name { font-size: 0.8125rem; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.upload-size { font-size: 0.75rem; color: var(--text-muted); }

.upload-clear {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  flex-shrink: 0;
}

.upload-clear:hover { color: var(--danger); }

/* Text area row */
.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.input-attach-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius);
  transition: color var(--transition);
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

.input-attach-btn:hover { color: var(--text-primary); }

#prompt-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-size: 0.9375rem;
  line-height: 1.5;
  resize: none;
  max-height: 200px;
  overflow-y: auto;
  font-family: inherit;
}

#prompt-input::placeholder { color: var(--text-muted); }

.input-send-btn {
  background: var(--accent);
  border: none;
  color: #fff;
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  transition: background var(--transition), opacity var(--transition);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.input-send-btn:hover { background: var(--accent-hover); }
.input-send-btn:disabled { opacity: 0.35; cursor: not-allowed; background: var(--accent); }

.input-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
  margin-top: 8px;
  max-width: 780px;
  margin-left: auto;
  margin-right: auto;
}

/* ===== Admin Page ===== */
.admin-layout {
  min-height: 100vh;
  background: var(--bg-main);
}

.admin-header {
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.admin-header h1 { font-size: 1.125rem; font-weight: 600; flex: 1; }

.btn-back {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.875rem;
  padding: 6px 12px;
  border-radius: var(--radius);
  transition: background var(--transition), color var(--transition);
}

.btn-back:hover { background: var(--bg-hover); color: var(--text-primary); }

.admin-content { max-width: 900px; margin: 0 auto; padding: 32px 24px; }

.admin-section { background: #2a2a2a; border: 1px solid var(--border); border-radius: 12px; margin-bottom: 28px; overflow: hidden; }
.admin-section-header { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.admin-section-header h2 { font-size: 1rem; font-weight: 600; }

.btn-sm {
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: background var(--transition);
}

.btn-sm.btn-accent { background: var(--accent); color: #fff; }
.btn-sm.btn-accent:hover { background: var(--accent-hover); }
.btn-sm.btn-danger { background: rgba(229,83,83,.15); color: #f87171; border: 1px solid rgba(229,83,83,.3); }
.btn-sm.btn-danger:hover { background: rgba(229,83,83,.25); }

.users-table { width: 100%; border-collapse: collapse; }
.users-table th { padding: 10px 20px; text-align: left; font-size: 0.8125rem; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; background: #252525; }
.users-table td { padding: 12px 20px; border-top: 1px solid var(--border); font-size: 0.9rem; }
.users-table tr:last-child td { border-bottom: none; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 0.75rem; font-weight: 600; }
.badge-admin { background: rgba(16,163,127,.2); color: #4ade80; }
.badge-user { background: rgba(160,160,160,.12); color: var(--text-muted); }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  opacity: 0; pointer-events: none;
  transition: opacity var(--transition);
}

.modal-overlay.open { opacity: 1; pointer-events: all; }

.modal {
  background: #2a2a2a;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px;
  width: 100%;
  max-width: 420px;
  transform: translateY(8px);
  transition: transform var(--transition);
}

.modal-overlay.open .modal { transform: translateY(0); }
.modal h3 { font-size: 1.0625rem; font-weight: 600; margin-bottom: 20px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
.btn-cancel { background: var(--bg-hover); color: var(--text-secondary); border: 1px solid var(--border); }
.btn-cancel:hover { background: var(--bg-active); color: var(--text-primary); }

/* ===== Lightbox ===== */
.lightbox {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.85);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
  opacity: 0; pointer-events: none;
  transition: opacity var(--transition);
}

.lightbox.open { opacity: 1; pointer-events: all; }
.lightbox img { max-width: 95vw; max-height: 95vh; object-fit: contain; border-radius: var(--radius); }
.lightbox-close {
  position: absolute; top: 16px; right: 16px;
  background: rgba(0,0,0,.5); border: none; color: #fff;
  width: 36px; height: 36px; border-radius: 50%;
  cursor: pointer; font-size: 1.25rem; display: flex; align-items: center; justify-content: center;
}

/* ===== Admin Sidenav ===== */
.admin-sidenav { width: 200px; min-width: 200px; background: #1a1a1a; border-right: 1px solid var(--border); padding: 12px 8px; overflow-y: auto; }
.admin-nav-item { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: var(--radius); color: var(--text-secondary); text-decoration: none; font-size: 0.875rem; margin-bottom: 2px; cursor: pointer; transition: background var(--transition), color var(--transition); border: none; background: none; width: 100%; text-align: left; }
.admin-nav-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.admin-nav-item.active { background: var(--bg-active); color: var(--text-primary); }
.admin-main-content { flex: 1; overflow-y: auto; padding: 28px 32px; }

/* ===== Stats Cards ===== */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fill,minmax(150px,1fr)); gap: 14px; margin-bottom: 28px; }
.stat-card { background: #2a2a2a; border: 1px solid var(--border); border-radius: 10px; padding: 18px 16px; text-align: center; }
.stat-number { font-size: 2rem; font-weight: 700; color: var(--accent); line-height: 1; }
.stat-label { font-size: 0.75rem; color: var(--text-muted); margin-top: 6px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }

/* ===== DB Tables ===== */
.db-table-wrap { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; margin-bottom: 28px; }
.db-table { width: 100%; border-collapse: collapse; }
.db-table th { background: #252525; padding: 9px 16px; text-align: left; font-size: .75rem; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.db-table td { padding: 10px 16px; border-top: 1px solid var(--border); font-size: .875rem; vertical-align: middle; }
.db-table tr.conv-row:hover td { background: rgba(255,255,255,.025); cursor: pointer; }
.db-table tr.conv-row td:first-child { padding-left: 12px; }
.conv-toggle { display: inline-flex; align-items: center; justify-content: center; width: 18px; height: 18px; border-radius: 3px; background: var(--bg-hover); color: var(--text-muted); font-size: .6rem; margin-right: 6px; transition: background var(--transition); }
.db-table tr.conv-row.expanded .conv-toggle { background: var(--accent); color: #fff; }
.conv-messages-row { display: none; }
.conv-messages-row.open { display: table-row; }
.conv-msgs-inner { padding: 10px 20px 14px; background: #1c1c1c; }
.conv-msg-item { display: flex; align-items: flex-start; gap: 10px; padding: 6px 0; border-bottom: 1px solid #242424; font-size: .8125rem; }
.conv-msg-item:last-child { border-bottom: none; }
.conv-msg-role { width: 68px; flex-shrink: 0; font-weight: 600; font-size: .75rem; }
.conv-msg-role.user { color: #a0a0ff; }
.conv-msg-role.assistant { color: var(--accent); }
.conv-msg-text { flex: 1; color: var(--text-secondary); word-break: break-word; }
.conv-msg-thumb { width: 52px; height: 52px; object-fit: cover; border-radius: 5px; border: 1px solid var(--border); cursor: pointer; flex-shrink: 0; }

/* ===== Log Viewer ===== */
.log-toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.log-select { background: var(--bg-input); border: 1px solid var(--border); border-radius: 6px; color: var(--text-primary); padding: 6px 10px; font-size: .875rem; outline: none; cursor: pointer; }
.log-btn { padding: 6px 14px; border-radius: 6px; border: 1px solid var(--border); background: var(--bg-hover); color: var(--text-secondary); cursor: pointer; font-size: .875rem; transition: background var(--transition), color var(--transition); }
.log-btn:hover { background: var(--bg-active); color: var(--text-primary); }
.log-btn.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.log-content { background: #141414; border: 1px solid var(--border); border-radius: 8px; padding: 14px 16px; font-family: "Consolas","Monaco",monospace; font-size: .8125rem; line-height: 1.7; overflow-y: auto; height: calc(100vh - 230px); white-space: pre; overflow-x: auto; }
.log-info { color: #6bcb77; }
.log-warn { color: #ffd166; }
.log-error { color: #ef476f; }
.log-counter { margin-left: auto; font-size: .8125rem; color: var(--text-muted); }

/* ===== Gallery ===== */
.gallery-tabs { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 20px; }
.gallery-tab-btn { padding: 9px 22px; background: none; border: none; border-bottom: 2px solid transparent; color: var(--text-secondary); cursor: pointer; font-size: .9375rem; margin-bottom: -1px; transition: color var(--transition), border-color var(--transition); }
.gallery-tab-btn.active { color: var(--text-primary); border-bottom-color: var(--accent); }
.gallery-panel { display: none; }
.gallery-panel.active { display: block; }
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill,minmax(170px,1fr)); gap: 12px; }
.gallery-card { background: #2a2a2a; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; cursor: pointer; transition: border-color var(--transition), transform var(--transition); }
.gallery-card:hover { border-color: #555; transform: translateY(-2px); }
.gallery-card img { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; background: #1a1a1a; }
.gallery-card-meta { padding: 8px 10px; }
.gallery-card-name { font-size: .6875rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: "Consolas",monospace; }
.gallery-card-info { font-size: .6875rem; color: #4a4a4a; margin-top: 2px; }
.gallery-empty { text-align: center; color: var(--text-muted); padding: 48px 0; font-size: .9375rem; }
