:root {
  --bg: #faf9f7;
  --bg-alt: #ffffff;
  --fg: #1a1a1a;
  --fg-muted: #666;
  --border: #e5e3df;
  --accent: #ff5a1f;
  --accent-fg: #ffffff;
  --user-bubble: #fff0e8;
  --bot-bubble: #ffffff;
  --error: #c62828;
  --radius: 14px;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16161a;
    --bg-alt: #1e1e22;
    --fg: #f4f4f5;
    --fg-muted: #a1a1aa;
    --border: #2a2a2f;
    --user-bubble: #3a201a;
    --bot-bubble: #24242a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.55;
  color: var(--fg);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

#app { height: 100dvh; display: flex; flex-direction: column; }
.view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.view[hidden] { display: none; }

/* ---------- Login ---------- */

#login {
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.login-card {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  max-width: 26rem;
  width: 100%;
  box-shadow: 0 2px 20px rgba(0,0,0,0.04);
}
.login-card h1 { margin: 0 0 .25rem; font-size: 1.5rem; }
.subtitle { margin: 0 0 1.5rem; color: var(--fg-muted); }
#login-form { display: flex; flex-direction: column; gap: .75rem; }
#login-form label { font-size: .875rem; color: var(--fg-muted); }
#code-input {
  padding: .85rem 1rem;
  font-size: 1.125rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--fg);
  letter-spacing: .15em;
  font-family: var(--font-mono);
  text-transform: uppercase;
}
#code-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
button, .link-btn {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}
#login-form button[type="submit"],
#send-btn {
  background: var(--accent);
  color: var(--accent-fg);
  padding: .85rem 1rem;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  transition: transform .05s, filter .15s;
}
#login-form button[type="submit"]:hover,
#send-btn:hover { filter: brightness(1.07); }
#login-form button[type="submit"]:active,
#send-btn:active { transform: translateY(1px); }
#login-form button[type="submit"]:disabled,
#send-btn:disabled { opacity: .5; cursor: not-allowed; }

.error {
  color: var(--error);
  font-size: .875rem;
  margin: .25rem 0 0;
}
.privacy {
  margin-top: 1.5rem;
  font-size: .8125rem;
  color: var(--fg-muted);
  border-top: 1px solid var(--border);
  padding-top: 1rem;
}

/* ---------- Chat ---------- */

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: .85rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-alt);
}
.chat-header strong { font-size: 1rem; }
.header-sub { color: var(--fg-muted); margin-left: .5rem; font-size: .875rem; }
.header-actions { display: flex; gap: 1rem; }
.link-btn {
  color: var(--fg-muted);
  font-size: .875rem;
  padding: .25rem .5rem;
  border-radius: 6px;
}
.link-btn:hover { color: var(--fg); background: var(--bg); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  max-width: 48rem;
  width: 100%;
  margin: 0 auto;
}

.bubble {
  max-width: 85%;
  padding: .85rem 1.1rem;
  border-radius: var(--radius);
  word-wrap: break-word;
  white-space: pre-wrap;
  border: 1px solid var(--border);
}
.bubble.user {
  align-self: flex-end;
  background: var(--user-bubble);
  border-color: transparent;
}
.bubble.assistant {
  align-self: flex-start;
  background: var(--bot-bubble);
}
.bubble.error {
  align-self: flex-start;
  background: #fde8e8;
  color: var(--error);
  border-color: #f5b5b5;
}
@media (prefers-color-scheme: dark) {
  .bubble.error { background: #3a1f1f; color: #ff9b9b; border-color: #502525; }
}

.sources {
  font-size: .75rem;
  color: var(--fg-muted);
  margin-top: .5rem;
  padding-top: .5rem;
  border-top: 1px dashed var(--border);
}
.sources strong { display: block; margin-bottom: .25rem; font-weight: 600; }
.sources ul { margin: 0; padding: 0; list-style: none; }
.sources li { font-family: var(--font-mono); font-size: .75rem; }

.typing-dot {
  display: inline-block;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--fg-muted);
  margin-right: 3px;
  animation: bounce 1.2s infinite ease-in-out;
}
.typing-dot:nth-child(2) { animation-delay: .15s; }
.typing-dot:nth-child(3) { animation-delay: .3s; }
@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: .4; }
  40% { transform: translateY(-4px); opacity: 1; }
}

.composer {
  display: flex;
  gap: .6rem;
  padding: .85rem 1.25rem;
  border-top: 1px solid var(--border);
  background: var(--bg-alt);
  max-width: 48rem;
  width: 100%;
  margin: 0 auto;
}
.composer textarea {
  flex: 1;
  padding: .75rem .9rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--fg);
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.4;
  resize: none;
  max-height: 10rem;
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
