@import url('https://fonts.googleapis.com/css2?family=EB+Garamond:ital,wght@0,400;0,500;1,400&display=swap');

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

:root {
  --blue:   #7297C5;
  --bg:     #ffffff;
  --text:   #000000;
  --dim:    #555555;
  --rule:   #e4e4e4;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'EB Garamond', Georgia, serif;
  font-size: 18px;
  line-height: 1.65;
  min-height: 100vh;
}

/* ── Animations ─────────────────────────────────────────────── */
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes blink {
  0%, 80%, 100% { opacity: 0.2; transform: translateY(0); }
  40%           { opacity: 1;   transform: translateY(-5px); }
}

/* ── Shared elements ────────────────────────────────────────── */
button {
  font-family: inherit;
  font-size: 0.85rem;
  letter-spacing: 0.02em;
  cursor: pointer;
  border: 1px solid var(--blue);
  background: var(--bg);
  color: var(--text);
  padding: 0.4rem 1.1rem;
  transition: background 0.15s, color 0.15s;
}
button:hover  { background: var(--blue); color: #fff; }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button:disabled:hover { background: var(--bg); color: var(--text); }

.btn-ghost {
  border-color: transparent;
  color: var(--dim);
  padding: 0.3rem 0.5rem;
}
.btn-ghost:hover { background: transparent; color: var(--blue); border-color: transparent; }

.btn-danger { border-color: #c0392b; }
.btn-danger:hover { background: #c0392b; color: #fff; }

label {
  display: block;
  font-size: 0.78rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--dim);
  margin-bottom: 0.3rem;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  border: none;
  border-bottom: 1px solid var(--rule);
  background: transparent;
  font-family: inherit;
  font-size: 1rem;
  color: var(--text);
  padding: 0.4rem 0;
  outline: none;
  margin-bottom: 1.5rem;
  transition: border-color 0.15s;
}
input:focus { border-bottom-color: var(--blue); }

select {
  width: 100%;
  border: 1px solid var(--rule);
  background: transparent;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  padding: 0.4rem 0.5rem;
  outline: none;
  margin-bottom: 1.5rem;
}
select:focus { border-color: var(--blue); }

.error-msg {
  font-size: 0.82rem;
  color: #c0392b;
  min-height: 1.2em;
  margin-top: 0.5rem;
}

/* ── Login ──────────────────────────────────────────────────── */
.login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.login-card {
  width: 100%;
  max-width: 340px;
  padding: 0 1rem;
}

.login-card h1 {
  font-size: 2.2rem;
  font-weight: 400;
  letter-spacing: -0.01em;
  margin-bottom: 0.15rem;
}

.login-card .subtitle {
  font-size: 1rem;
  font-style: italic;
  color: var(--dim);
  margin-bottom: 2.5rem;
}

.login-card button[type="submit"] {
  width: 100%;
  padding: 0.55rem;
  font-size: 0.9rem;
  margin-top: 0.5rem;
}

/* ── App shell ──────────────────────────────────────────────── */
.app {
  display: grid;
  grid-template-columns: 190px 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ────────────────────────────────────────────────── */
.sidebar {
  position: relative;
  display: flex;
  flex-direction: column;
  padding: 1.75rem 1.25rem 1.25rem;
  gap: 0;
}

.sidebar::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 1px;
  height: 100%;
  background: linear-gradient(to bottom, transparent, var(--blue) 18%, var(--blue) 82%, transparent);
}

.sidebar-logo {
  font-size: 1.1rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  margin-bottom: 0.2rem;
}

.sidebar-user {
  font-size: 0.82rem;
  font-style: italic;
  color: var(--dim);
  margin-bottom: 1.5rem;
}

.sidebar-rule {
  border: none;
  border-top: 1px solid var(--rule);
  margin-bottom: 1rem;
}

.sidebar-action {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  font-family: inherit;
  font-size: 0.88rem;
  color: var(--text);
  padding: 0.3rem 0;
  cursor: pointer;
  transition: color 0.12s;
}
.sidebar-action:hover { color: var(--blue); background: none; }

/* ── Conversation list ──────────────────────────────────────── */
.sidebar-new-chat {
  display: block;
  width: 100%;
  text-align: left;
  border: 1px solid var(--blue);
  background: none;
  font-family: inherit;
  font-size: 0.82rem;
  color: var(--blue);
  padding: 0.3rem 0.6rem;
  margin-bottom: 0.75rem;
  cursor: pointer;
  border-radius: 2px;
  transition: background 0.12s, color 0.12s;
}
.sidebar-new-chat:hover { background: var(--blue); color: #fff; }

.conv-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0;
  margin-bottom: 0.5rem;
  min-height: 0;
}

.conv-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  font-family: inherit;
  font-size: 0.82rem;
  color: var(--dim);
  padding: 0.28rem 0.4rem;
  cursor: pointer;
  border-radius: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.1s, background 0.1s;
}
.conv-item:hover  { color: var(--text); background: none; }
.conv-item.active { color: var(--text); font-style: italic; }

.sidebar-spacer { flex: 0; }

.sidebar-logout {
  font-size: 0.82rem;
  color: var(--dim);
  border: none;
  background: none;
  padding: 0.3rem 0;
  cursor: pointer;
  font-family: inherit;
  text-align: left;
}
.sidebar-logout:hover { color: #c0392b; background: none; }

/* ── Chat area ──────────────────────────────────────────────── */
.chat-area {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 2rem 2.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.75rem;
}

.message {
  display: flex;
  flex-direction: column;
  max-width: 640px;
  animation: fadeUp 0.2s ease;
}

.message.user     { align-self: flex-end; align-items: flex-end; }
.message.assistant { align-self: flex-start; }

.bubble {
  padding: 0.65rem 1rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.message.user .bubble {
  border: 1px solid var(--blue);
  background: var(--bg);
  color: var(--text);
  border-radius: 20px;
  border-bottom-right-radius: 4px;
}

.message.assistant .bubble {
  padding: 0;
  font-size: 1rem;
}

.sources {
  font-size: 0.78rem;
  font-style: italic;
  color: var(--dim);
  margin-top: 0.5rem;
}

/* ── Loading dots ───────────────────────────────────────────── */
.thinking-dots {
  padding: 0.4rem 0;
  display: flex;
  align-items: center;
  gap: 5px;
}

.thinking-dots span {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--blue);
  animation: blink 1.3s ease-in-out infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

/* ── Input bar ──────────────────────────────────────────────── */
.input-bar {
  position: relative;
  padding: 0.9rem 2.5rem;
  display: flex;
  gap: 0.75rem;
  align-items: flex-end;
}

.input-bar::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 1px;
  background: linear-gradient(to right, transparent, var(--blue) 12%, var(--blue) 88%, transparent);
}

.input-bar textarea {
  flex: 1;
  border: none;
  border-bottom: 1px solid var(--rule);
  background: transparent;
  font-family: inherit;
  font-size: 1rem;
  color: var(--text);
  line-height: 1.5;
  padding: 0.3rem 0;
  resize: none;
  outline: none;
  max-height: 140px;
  overflow-y: auto;
  transition: border-color 0.15s;
}
.input-bar textarea:focus { border-bottom-color: var(--blue); }
.input-bar textarea::placeholder { color: #aaa; }

.input-bar button {
  padding: 0.4rem 1.4rem;
  flex-shrink: 0;
  align-self: flex-end;
  margin-bottom: 1px;
  border-radius: 999px;
}

/* ── Modal ──────────────────────────────────────────────────── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,0.75);
  backdrop-filter: blur(2px);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open { display: flex; }

.modal {
  background: var(--bg);
  border: 1px solid var(--blue);
  padding: 2rem;
  width: 100%;
  max-width: 460px;
  max-height: 85vh;
  overflow-y: auto;
  animation: fadeUp 0.18s ease;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 1.75rem;
}

.modal h2 {
  font-size: 1.3rem;
  font-weight: 400;
}

.modal-close {
  border: none;
  background: none;
  font-size: 1.1rem;
  color: var(--dim);
  padding: 0;
  line-height: 1;
}
.modal-close:hover { color: var(--text); background: none; }

.modal button[type="button"]:not(.modal-close) {
  margin-top: 0.25rem;
}

/* ── User table ─────────────────────────────────────────────── */
.user-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
  margin-bottom: 2rem;
}
.user-table th {
  text-align: left;
  color: var(--dim);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid var(--blue);
  font-weight: 400;
}
.user-table td {
  padding: 0.55rem 0.5rem;
  border-bottom: 1px solid var(--rule);
  vertical-align: middle;
}
.user-table td:last-child { display: flex; gap: 0.4rem; align-items: center; }

.badge {
  font-size: 0.68rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  padding: 0.1rem 0.5rem;
  border: 1px solid var(--rule);
  color: var(--dim);
}
.badge.admin { border-color: var(--blue); color: var(--blue); }

.section-label {
  font-size: 0.78rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--dim);
  margin-bottom: 1.25rem;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--rule);
}
