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

:root {
  --bg: #f5f5f5;
  --surface: #ffffff;
  --border: #e0e0e0;
  --primary: #1a56a0;
  --primary-hover: #1445882;
  --user-bubble: #1a56a0;
  --asst-bubble: #ffffff;
  --text: #1a1a1a;
  --text-muted: #6b7280;
  --danger: #dc2626;
  --radius: 12px;
  --sidebar-w: 260px;
}

body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); height: 100dvh; overflow: hidden; }

/* ── Layout ── */
.layout { display: flex; height: 100dvh; }

.sidebar {
  width: var(--sidebar-w); min-width: var(--sidebar-w);
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
  transition: transform 0.25s ease;
}
.sidebar-header { padding: 16px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; }
.sidebar-header h2 { font-size: 16px; font-weight: 700; flex: 1; }
.btn-icon { background: none; border: none; cursor: pointer; padding: 6px; border-radius: 8px; color: var(--text-muted); display: flex; align-items: center; }
.btn-icon:hover { background: var(--bg); }
.conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv-item { padding: 10px 12px; border-radius: 8px; cursor: pointer; display: flex; align-items: center; gap: 8px; font-size: 14px; position: relative; }
.conv-item:hover, .conv-item.active { background: var(--bg); }
.conv-item span { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.conv-delete { opacity: 0; background: none; border: none; cursor: pointer; color: var(--danger); padding: 2px 4px; font-size: 16px; border-radius: 4px; }
.conv-item:hover .conv-delete { opacity: 1; }
.sidebar-footer { padding: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.user-info { font-size: 13px; color: var(--text-muted); display: flex; align-items: center; gap: 8px; }
.user-info strong { color: var(--text); }
.btn-sm { font-size: 13px; padding: 6px 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; }
.btn-sm:hover { background: var(--bg); }

/* ── Main chat area ── */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--surface); display: flex; align-items: center; gap: 10px; }
.topbar h3 { font-size: 15px; font-weight: 600; flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#menu-toggle { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 20px 16px; display: flex; flex-direction: column; gap: 16px; }
.msg { display: flex; flex-direction: column; max-width: 80%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }
.bubble { padding: 12px 16px; border-radius: var(--radius); line-height: 1.6; font-size: 15px; white-space: pre-wrap; word-break: break-word; }
.msg.user .bubble { background: var(--user-bubble); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--asst-bubble); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg-time { font-size: 11px; color: var(--text-muted); margin-top: 4px; }

.typing { display: flex; gap: 5px; align-items: center; padding: 14px 16px; }
.dot { width: 8px; height: 8px; background: var(--text-muted); border-radius: 50%; animation: bounce 1.2s infinite; }
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce { 0%, 80%, 100% { transform: translateY(0); } 40% { transform: translateY(-8px); } }

.input-area { padding: 12px 16px; border-top: 1px solid var(--border); background: var(--surface); display: flex; gap: 10px; align-items: flex-end; }
.input-area textarea { flex: 1; resize: none; border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; font-size: 15px; font-family: inherit; max-height: 140px; line-height: 1.5; outline: none; }
.input-area textarea:focus { border-color: var(--primary); }
.btn-send { background: var(--primary); color: #fff; border: none; border-radius: var(--radius); padding: 10px 18px; cursor: pointer; font-size: 15px; font-weight: 600; white-space: nowrap; }
.btn-send:hover { background: #1445882; }
.btn-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Files panel ── */
.file-panel { padding: 8px 16px 12px; border-top: 1px solid var(--border); background: var(--surface); }
.file-row { display: flex; align-items: center; gap: 8px; }
.file-row label { font-size: 13px; color: var(--primary); cursor: pointer; }
.file-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip { background: var(--bg); border: 1px solid var(--border); border-radius: 20px; padding: 3px 10px; font-size: 12px; display: flex; align-items: center; gap: 6px; }
.chip a { color: var(--primary); text-decoration: none; font-size: 12px; }
.chip button { background: none; border: none; cursor: pointer; color: var(--text-muted); font-size: 14px; line-height: 1; }

/* ── Login page ── */
.login-wrap { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 40px 36px; width: 100%; max-width: 380px; }
.login-card h1 { font-size: 22px; font-weight: 700; margin-bottom: 6px; }
.login-card p { color: var(--text-muted); font-size: 14px; margin-bottom: 28px; }
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; }
.field input { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; font-size: 15px; outline: none; }
.field input:focus { border-color: var(--primary); }
.btn-primary { width: 100%; background: var(--primary); color: #fff; border: none; border-radius: 8px; padding: 12px; font-size: 15px; font-weight: 600; cursor: pointer; margin-top: 8px; }
.btn-primary:hover { background: #154488; }
.error-msg { color: var(--danger); font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ── Admin page ── */
.admin-wrap { max-width: 760px; margin: 0 auto; padding: 32px 16px; }
.admin-wrap h1 { font-size: 22px; font-weight: 700; margin-bottom: 24px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; margin-bottom: 24px; }
.card h2 { font-size: 16px; font-weight: 600; margin-bottom: 16px; }
.form-row { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.form-row input, .form-row select { flex: 1; min-width: 140px; border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; font-size: 14px; outline: none; }
.form-row input:focus { border-color: var(--primary); }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; padding: 8px 10px; border-bottom: 2px solid var(--border); color: var(--text-muted); font-weight: 500; }
td { padding: 10px; border-bottom: 1px solid var(--border); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 12px; font-size: 12px; font-weight: 500; }
.badge.admin { background: #dbeafe; color: #1e40af; }
.badge.user { background: #f3f4f6; color: #374151; }
.btn-danger { background: none; border: 1px solid var(--danger); color: var(--danger); border-radius: 6px; padding: 4px 10px; font-size: 13px; cursor: pointer; }
.btn-danger:hover { background: #fee2e2; }
.back-link { color: var(--primary); text-decoration: none; font-size: 14px; display: inline-block; margin-bottom: 20px; }

/* ── Mobile ── */
@media (max-width: 640px) {
  .sidebar { position: fixed; top: 0; left: 0; height: 100%; z-index: 100; transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); box-shadow: 4px 0 20px rgba(0,0,0,.15); }
  #menu-toggle { display: flex; }
  .msg { max-width: 92%; }
}
