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

:root {
  --sidebar-bg: #2c3e2d;
  --sidebar-text: #e8e0d0;
  --sidebar-accent: #c9a96e;
  --main-bg: #f8f6f1;
  --card-bg: #ffffff;
  --text: #3a3a3a;
  --text-light: #7a7a7a;
  --user-bg: #e7efe8;
  --border: #e0ddd5;
  --accent: #8b6f47;
  --error: #b44;
  --radius: 12px;
  --sidebar-w: 280px;
}

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

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

/* ── Sidebar ─────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  padding: 2rem 1.5rem;
  gap: 1.5rem;
}

.sidebar-portrait {
  width: 100%;
  border-radius: var(--radius);
  opacity: 0.92;
}

.sidebar h1 {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.5rem;
  letter-spacing: 0.12em;
  color: var(--sidebar-accent);
  text-align: center;
}

.sidebar-tagline {
  font-size: 0.85rem;
  text-align: center;
  opacity: 0.75;
  line-height: 1.5;
  font-style: italic;
}

.sidebar-divider {
  border: none;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
}

.sidebar-info {
  font-size: 0.8rem;
  opacity: 0.55;
  line-height: 1.6;
}

.sidebar-footer {
  margin-top: auto;
  font-size: 0.72rem;
  opacity: 0.4;
  text-align: center;
}

/* ── Sidebar download link ──────────────────────────── */
.sidebar-download-link {
  display: block;
  text-align: center;
  font-size: 0.82rem;
  color: var(--sidebar-accent);
  text-decoration: none;
  opacity: 0.7;
  transition: opacity 0.15s;
  margin:0;
  padding:0;
  /*padding: 0.3rem 0;*/
}

.sidebar-download-link:hover {
  opacity: 1;
  text-decoration: underline;
}

/* ── New Chat button ────────────────────────────────── */
.new-chat-btn {
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  color: var(--sidebar-accent);
  font-size: 0.85rem;
  padding: 0.45rem 0.75rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  text-align: center;
}

.new-chat-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  border-color: var(--sidebar-accent);
}

/* ── Main area ───────────────────────────────────────── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* ── Messages ────────────────────────────────────────── */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 2rem 2rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.welcome {
  margin: auto;
  text-align: center;
  max-width: 460px;
  color: var(--text-light);
}

.welcome h2 {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.3rem;
  color: var(--accent);
  margin-bottom: 0.5rem;
}

.welcome p {
  font-size: 0.92rem;
  line-height: 1.7;
}

.welcome .suggestions {
  margin-top: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.suggestion-btn {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 1rem;
  font-size: 0.88rem;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.suggestion-btn:hover {
  border-color: var(--accent);
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

/* ── Message bubbles ─────────────────────────────────── */
.msg {
  max-width: 720px;
  width: 100%;
}

.msg.user {
  align-self: flex-end;
  background: var(--user-bg);
  border-radius: var(--radius) var(--radius) 4px var(--radius);
  padding: 0.75rem 1rem;
}

.msg.assistant {
  align-self: flex-start;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) var(--radius) 4px;
  padding: 1rem 1.25rem;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

.msg.assistant .response-text p {
  margin-bottom: 0.6em;
}

.msg.assistant .response-text p:last-child {
  margin-bottom: 0;
}

.msg.assistant .response-text ul,
.msg.assistant .response-text ol {
  margin: 0.4em 0 0.6em 1.4em;
}

.msg.assistant .response-text strong {
  color: var(--accent);
}

.msg.error {
  align-self: flex-start;
  background: #fef2f2;
  border: 1px solid #f5c6c6;
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  color: var(--error);
  font-size: 0.9rem;
}

/* ── Streaming cursor ────────────────────────────────── */
.cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 0.8s step-end infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

/* ── Sources accordion ───────────────────────────────── */
.sources-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin-top: 0.75rem;
  padding: 0.3rem 0;
  background: none;
  border: none;
  font-size: 0.82rem;
  color: var(--text-light);
  cursor: pointer;
  transition: color 0.15s;
}

.sources-toggle:hover {
  color: var(--accent);
}

.sources-toggle .arrow {
  display: inline-block;
  transition: transform 0.2s;
  font-size: 0.7em;
}

.sources-toggle.open .arrow {
  transform: rotate(90deg);
}

.sources-note {
  font-style: italic;
  font-size: 0.78rem;
  color: var(--text-light);
  margin-left: 0.4em;
}

.sources-note::before {
  content: '\00b7 ';
}

.sources-list {
  display: none;
  margin-top: 0.5rem;
  border-top: 1px solid var(--border);
  padding-top: 0.5rem;
}

.sources-list.open {
  display: block;
}

.source-item {
  display: flex;
  gap: 0.75rem;
  padding: 0.4rem 0;
  font-size: 0.82rem;
  color: var(--text-light);
  line-height: 1.5;
}

.source-item .source-num {
  flex-shrink: 0;
  width: 1.4em;
  text-align: right;
  font-weight: 600;
  color: var(--accent);
}

.source-item .source-title {
  font-weight: 600;
  color: var(--text);
}

/* ── Input bar ───────────────────────────────────────── */
.input-bar {
  padding: 1rem 2rem 1.25rem;
  background: var(--main-bg);
  border-top: 1px solid var(--border);
}

.input-wrapper {
  display: flex;
  gap: 0.5rem;
  max-width: 720px;
  margin: 0 auto;
}

.input-wrapper textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.7rem 1rem;
  font-family: inherit;
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--text);
  background: var(--card-bg);
  outline: none;
  transition: border-color 0.15s;
  max-height: 120px;
}

.input-wrapper textarea:focus {
  border-color: var(--accent);
}

.input-wrapper textarea::placeholder {
  color: var(--text-light);
}

.send-btn {
  align-self: flex-end;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 10px;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, transform 0.1s;
  flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
  background: #3d5a3e;
}

.send-btn:active:not(:disabled) {
  transform: scale(0.95);
}

.send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ── Style toggle (segmented control) ────────────────── */
.style-toggle {
  display: flex;
  justify-content: center;
  gap: 0;
  max-width: 720px;
  margin: 0 auto 0.5rem;
}

.style-btn {
  background: none;
  border: 1px solid var(--border);
  padding: 0.3rem 0.9rem;
  font-size: 0.78rem;
  font-family: inherit;
  color: var(--text-light);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.style-btn:first-child { border-radius: 6px 0 0 6px; border-right: none; }
.style-btn:not(:first-child):not(:last-child) { border-radius: 0; border-right: none; }
.style-btn:last-child  { border-radius: 0 6px 6px 0; }
.style-btn:hover:not(.active) { color: var(--text); border-color: var(--accent); }
.style-btn.active { background: var(--sidebar-bg); color: var(--sidebar-text); border-color: var(--sidebar-bg); }

/* ── Turn counter badge ──────────────────────────────── */
.turn-counter {
  display: none;
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-light);
  margin-bottom: 0.4rem;
  max-width: 720px;
  margin-left: auto;
  margin-right: auto;
}

/* ── Out-of-context messages ─────────────────────────── */
.msg.out-of-context {
  opacity: 0.45;
  transition: opacity 0.4s ease;
}

/* ── Context boundary separator ──────────────────────── */
.context-boundary {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  color: var(--text-light);
  font-size: 0.75rem;
  margin: 0.25rem 0;
}

.context-boundary::before,
.context-boundary::after {
  content: '';
  flex: 1;
  border-top: 1px solid var(--border);
}

/* ── Auth widget (sidebar) ───────────────────────────── */
.auth-widget {
  font-size: 0.82rem;
}

.auth-tier-label {
  display: block;
  font-size: 0.75rem;
  opacity: 0.5;
  margin-top: 0.3rem;
}

.auth-error {
  width: 100%;
  font-size: 0.75rem;
  color: #e88;
  min-height: 0;
  margin-top: 0.25rem;
}

.auth-user-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.auth-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.auth-status {
  color: var(--sidebar-accent);
  font-weight: 600;
  font-size: 0.82rem;
}

.auth-actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 0.25rem;
}

.auth-admin-link {
  font-size: 0.75rem;
  color: var(--sidebar-accent);
  text-decoration: none;
  opacity: 0.7;
  transition: opacity 0.15s;
}

.auth-admin-link:hover {
  opacity: 1;
  text-decoration: underline;
}

.auth-signout-btn {
  background: none;
  border: none;
  color: var(--sidebar-text);
  opacity: 0.5;
  font-size: 0.75rem;
  cursor: pointer;
  text-decoration: underline;
  padding: 0;
}

.auth-signout-btn:hover {
  opacity: 0.8;
}

/* ── Rate-limit banner ──────────────────────────────── */
.rate-limit-banner {
  background: #fef3cd;
  border: 1px solid #f0d78c;
  border-radius: 8px;
  padding: 0.5rem 1rem;
  margin: 0 2rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: #856404;
}

.rate-limit-msg {
  flex: 1;
}

.rate-limit-dismiss {
  background: none;
  border: none;
  font-size: 1.1rem;
  color: #856404;
  cursor: pointer;
  padding: 0 0.25rem;
  opacity: 0.6;
}

.rate-limit-dismiss:hover {
  opacity: 1;
}

/* ── Responsive ──────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar {
    display: none;
  }

  .messages {
    padding: 1.25rem 1rem 0.75rem;
  }

  .input-bar {
    padding: 0.75rem 1rem 1rem;
  }

  .style-toggle {
    margin-bottom: 0.4rem;
  }
}
