:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --border: #e3e6eb;
  --text: #1f2328;
  --muted: #6a737d;
  --accent: #2f6feb;
  --accent-hover: #2558c4;
  --user-bubble: #2f6feb;
  --user-text: #ffffff;
  --bot-bubble: #ffffff;
  --bot-text: #1f2328;
  --error: #c92a2a;
}

* { box-sizing: border-box; }

/* The `hidden` HTML attribute is just a UA `display: none`, so any author
   rule that sets `display` (e.g. `#chat-view { display: flex }`) overrides
   it. Force it to win so JS can rely on toggling the attribute. */
[hidden] { display: none !important; }

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

button {
  cursor: pointer;
  font: inherit;
}

/* ---- Login view ---- */
#login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 1rem;
}

#login-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 2rem;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
}

#login-logo {
  display: block;
  margin: 0 auto 1.25rem;
  height: 100px;
  width: auto;
}

#login-card h1 {
  margin: 0 0 0.5rem;
  font-size: 1.25rem;
  text-align: center;
}

#login-card .subtitle {
  margin: 0 0 1.5rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
}

#login-form input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  margin-bottom: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  background: #fff;
}

#login-form input:focus {
  outline: none;
  border-color: var(--accent);
}

#login-form button {
  width: 100%;
  padding: 0.65rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-weight: 500;
}

#login-form button:hover { background: var(--accent-hover); }
#login-form button:disabled { opacity: 0.6; cursor: not-allowed; }

#login-error {
  color: var(--error);
  font-size: 0.85rem;
  min-height: 1.2em;
  margin-top: 0.5rem;
  text-align: center;
}

/* ---- Chat view ---- */
#chat-view {
  display: flex;
  flex-direction: row;
  height: 100%;
}

#chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0; /* allow flex item to shrink below content size */
}

/* ---- Sidebar ---- */
#sidebar {
  width: 280px;
  flex: 0 0 280px;
  display: flex;
  flex-direction: column;
  background: var(--card);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

#sidebar-header {
  padding: 0.75rem;
  border-bottom: 1px solid var(--border);
}

#sidebar-header button {
  width: 100%;
  padding: 0.55rem 0.75rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-weight: 500;
}

#sidebar-header button:hover { background: var(--accent-hover); }

#conversation-list {
  list-style: none;
  margin: 0;
  padding: 0.5rem;
  overflow-y: auto;
  flex: 1;
}

#conversation-list li {
  display: flex;
  align-items: flex-start;
  gap: 0.4rem;
  padding: 0.55rem 0.65rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
  line-height: 1.35;
  color: var(--text);
  border: 1px solid transparent;
  margin-bottom: 0.25rem;
  word-wrap: break-word;
}

#conversation-list li:hover { background: var(--bg); }

#conversation-list li.active {
  background: #eef3ff;
  border-color: #c9d8ff;
}

#conversation-list li .conv-body {
  flex: 1;
  min-width: 0;
}

#conversation-list li .conv-meta {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  margin-bottom: 0.15rem;
}

#conversation-list li .conv-delete {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 4px;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.12s ease, background 0.12s ease, color 0.12s ease;
}

#conversation-list li:hover .conv-delete,
#conversation-list li.active .conv-delete {
  opacity: 1;
}

#conversation-list li .conv-delete:hover {
  background: #fde2e2;
  color: var(--error);
}

.sidebar-empty {
  padding: 1rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
}

/* ---- Sidebar backend status (bottom 10%) ---- */
#sidebar-status {
  flex: 0 0 10%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.85rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--card);
}

.status-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.05rem 0;
  font-size: 0.72rem;
  color: var(--text);
  line-height: 1.3;
}

.status-dot {
  display: inline-block;
  flex: 0 0 auto;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--muted);
}

.status-dot.is-loading {
  background: var(--muted);
  animation: status-pulse 1s ease-in-out infinite;
}

.status-dot.is-ok {
  background: #2f9e44;
  box-shadow: 0 0 0 3px rgba(47, 158, 68, 0.18);
}

.status-dot.is-error {
  background: var(--error);
  box-shadow: 0 0 0 3px rgba(201, 42, 42, 0.18);
}

@keyframes status-pulse {
  0%, 100% { opacity: 0.35; }
  50%      { opacity: 1; }
}

#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}

#chat-header .title { font-weight: 600; }

#chat-header .user-label {
  color: var(--muted);
  font-size: 0.85rem;
  margin-left: 0.5rem;
}

#chat-header .actions button {
  margin-left: 0.5rem;
  padding: 0.4rem 0.75rem;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.85rem;
}

#chat-header .actions button:hover { background: var(--bg); }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.msg.bot { justify-content: flex-start; }

.bubble {
  max-width: min(720px, 80%);
  padding: 0.65rem 0.9rem;
  border-radius: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
}

.msg.bot .bubble {
  background: var(--bot-bubble);
  color: var(--bot-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  white-space: normal;
}

.msg.bot .bubble p:first-child { margin-top: 0; }
.msg.bot .bubble p:last-child { margin-bottom: 0; }
.msg.bot .bubble ul, .msg.bot .bubble ol { padding-left: 1.25rem; }

.msg.bot .bubble code {
  background: #f3f4f6;
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
  font-size: 0.9em;
}

.msg.bot .bubble pre {
  background: #f3f4f6;
  padding: 0.6rem;
  border-radius: 6px;
  overflow-x: auto;
}

.msg.error .bubble {
  background: #fef2f2;
  color: var(--error);
  border: 1px solid #fecaca;
}

.typing { color: var(--muted); font-style: italic; }
.typing::after {
  content: '…';
  display: inline-block;
  animation: dots 1.2s steps(3, end) infinite;
  width: 1em;
  text-align: left;
  overflow: hidden;
  vertical-align: bottom;
}

@keyframes dots {
  0% { width: 0; }
  100% { width: 1em; }
}

#chat-form {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  background: var(--card);
  border-top: 1px solid var(--border);
}

#chat-input {
  flex: 1;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  resize: none;
  font: inherit;
  max-height: 200px;
  min-height: 42px;
}

#chat-input:focus { outline: none; border-color: var(--accent); }

#send-btn {
  padding: 0 1.25rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  font-weight: 500;
}

#send-btn:hover:not(:disabled) { background: var(--accent-hover); }
#send-btn:disabled { opacity: 0.6; cursor: not-allowed; }

.empty {
  margin: auto;
  color: var(--muted);
  text-align: center;
  font-size: 0.9rem;
  max-width: 560px;
  line-height: 1.5;
}
