:root {
  --bg: #0f172a;
  --surface: #ffffff;
  --surface-muted: #f4f6f9;
  --text: #1c2333;
  --text-muted: #5b6472;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --border: #e2e6ec;
  --user-bubble: #2563eb;
  --user-bubble-text: #ffffff;
  --assistant-bubble: #f4f6f9;
  --error: #d92d20;
  --radius: 12px;
  color-scheme: light;
}

* { box-sizing: border-box; }

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

.view { min-height: 100vh; }

/* ---------- Login ---------- */
#login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(160deg, var(--bg), #1e293b);
}

.login-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 32px 28px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.25);
}

.login-card h1 {
  margin: 0 0 8px;
  font-size: 1.4rem;
}

.subtitle {
  margin: 0 0 24px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

#login-form label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 14px;
}

#login-form input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
}

#login-button {
  width: 100%;
  padding: 11px;
  margin-top: 6px;
  border: none;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

#login-button:hover { background: var(--primary-dark); }

.error {
  color: var(--error);
  font-size: 0.85rem;
  margin-top: 14px;
}

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

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { font-weight: 700; }

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.link-button {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 0;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
}

.msg { display: flex; flex-direction: column; max-width: 85%; }
.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: 16px;
  white-space: pre-wrap;
  line-height: 1.45;
  font-size: 0.96rem;
}

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

.msg.assistant .bubble {
  background: var(--assistant-bubble);
  border-bottom-left-radius: 4px;
}

.msg.pending .bubble { color: var(--text-muted); font-style: italic; }

.sources {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.source-card {
  width: 148px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  text-align: left;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}

.source-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0,0,0,0.08);
}

.source-thumb {
  width: 100%;
  height: 96px;
  object-fit: cover;
  background: var(--surface-muted);
  display: block;
}

.source-thumb.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
}

.source-meta {
  padding: 8px 10px;
}

.source-title {
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.source-page {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.composer {
  display: flex;
  gap: 10px;
  padding: 14px 20px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
}

.composer textarea {
  flex: 1;
  resize: none;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  font-size: 0.96rem;
  font-family: inherit;
  max-height: 140px;
}

.composer button {
  padding: 0 20px;
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.composer button:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Lightbox / PDF modal ---------- */
.lightbox, .pdf-modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
}

.lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
}

.lightbox-close {
  position: absolute;
  top: 16px;
  right: 20px;
  background: none;
  border: none;
  color: #fff;
  font-size: 2rem;
  line-height: 1;
  cursor: pointer;
}

.pdf-modal {
  flex-direction: column;
  padding: 0;
}

.pdf-modal-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 20px;
  background: var(--bg);
  color: #fff;
}

.pdf-modal-header span { flex: 1; font-weight: 600; font-size: 0.9rem; }
.pdf-modal-header a { color: #93c5fd; font-size: 0.85rem; text-decoration: none; }
.pdf-modal-header .lightbox-close { position: static; }

#pdf-modal-frame {
  flex: 1;
  width: 100%;
  border: none;
  background: #333;
}

@media (max-width: 480px) {
  .source-card { width: 120px; }
  .messages { padding: 14px; }
  .composer { padding: 10px 14px 16px; }
}
