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

:root {
  --color-bg:           #0f0f0f;
  --color-surface:      #1a1a1a;
  --color-border:       #2a2a2a;
  --color-text:         #e8e8e8;
  --color-text-muted:   #888;
  --color-accent:       #7c6af7;
  --color-accent-hover: #9585f9;
  --color-user-bubble:  #1e1a3a;
  --color-error:        #e55;

  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 10px;
  --radius-xl: 12px;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-mono: "SF Mono", "Fira Code", Consolas, monospace;

  font-family: var(--font-sans);
}

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ─── Auth pages ─────────────────────────────────────────── */

.auth-page {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  padding: 2rem;
  width: 100%;
  max-width: 400px;
  margin: 1rem;
}

.auth-card h1 {
  margin: 0 0 0.25rem;
  font-size: 1.5rem;
}

.auth-card p {
  margin: 0 0 1.5rem;
  color: var(--color-text-muted);
  font-size: 0.9rem;
}

.auth-card label {
  display: block;
  font-size: 0.85rem;
  color: var(--color-text-muted);
  margin-bottom: 0.4rem;
}

.auth-card input[type="email"],
.auth-card input[type="text"] {
  width: 100%;
  padding: 0.6rem 0.8rem;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  color: var(--color-text);
  font-size: 1rem;
  outline: none;
  transition: border-color 0.15s;

  &:focus {
    border-color: var(--color-accent);
  }
}

.auth-card button[type="submit"] {
  width: 100%;
  padding: 0.65rem;
  margin-top: 1rem;
  background: var(--color-accent);
  color: white;
  border: none;
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;

  &:hover  { background: var(--color-accent-hover); }
  &:disabled { opacity: 0.5; cursor: not-allowed; }
}

/* ─── Chat layout ────────────────────────────────────────── */

.chat-layout {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  padding: 0 1rem;
  overflow: hidden;
}

.chat-header {
  padding: 1rem 0 0.5rem;
  border-bottom: 1px solid var(--color-border);
  display: flex;
  justify-content: space-between;
  align-items: center;

  h1 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
  }

  a {
    font-size: 0.8rem;
    color: var(--color-text-muted);
    text-decoration: none;
  }
}

.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.new-chat-btn {
  padding: 0.3rem 0.75rem;
  background: transparent;
  color: var(--color-text-muted);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;

  &:hover {
    border-color: var(--color-accent);
    color: var(--color-text);
  }
}

/* ─── Messages ───────────────────────────────────────────── */

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 0;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.message {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.message-role {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.message-content {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 0.75rem 1rem;
  line-height: 1.6;
  font-size: 0.95rem;
}

.message.user .message-content {
  background: var(--color-user-bubble);
  white-space: pre-wrap;
}

/* ─── Markdown prose (assistant messages) ────────────────── */

.message-content.md {
  p { margin: 0 0 0.6em; }
  p:last-child { margin-bottom: 0; }

  ul, ol { margin: 0 0 0.6em 1.25em; padding: 0; }
  li { margin-bottom: 0.2em; }

  h1, h2, h3 { margin: 0.8em 0 0.3em; font-size: 1em; font-weight: 600; }

  strong { font-weight: 600; }

  code {
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: 4px;
    padding: 0.1em 0.35em;
    font-family: var(--font-mono);
    font-size: 0.85em;
  }

  pre {
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    padding: 0.75rem;
    overflow-x: auto;
    font-family: var(--font-mono);
    font-size: 0.85rem;
    line-height: 1.5;
    margin: 0.5rem 0;

    code {
      background: none;
      border: none;
      padding: 0;
      font-size: inherit;
    }
  }
}

/* ─── Error bubble ───────────────────────────────────────── */

.message-content.error {
  color: var(--color-error);
  border-color: color-mix(in srgb, var(--color-error) 30%, transparent);
  background: color-mix(in srgb, var(--color-error) 8%, var(--color-surface));
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.retry-form { margin-left: auto; flex-shrink: 0; }

.retry-btn {
  padding: 0.3rem 0.75rem;
  background: transparent;
  color: var(--color-error);
  border: 1px solid color-mix(in srgb, var(--color-error) 50%, transparent);
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.15s;

  &:hover {
    background: color-mix(in srgb, var(--color-error) 15%, transparent);
  }
}

/* ─── Streaming cursor ───────────────────────────────────── */

.streaming::after {
  content: "▋";
  animation: blink 1s step-end infinite;
  opacity: 0.7;
}

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

/* ─── Input area ─────────────────────────────────────────── */

.chat-input-area {
  padding: 0.75rem 0 1rem;
  border-top: 1px solid var(--color-border);
}

.chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

.chat-input-row textarea {
  flex: 1;
  padding: 0.6rem 0.8rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  color: var(--color-text);
  font-size: 0.95rem;
  font-family: inherit;
  resize: none;
  outline: none;
  min-height: 44px;
  max-height: 200px;
  transition: border-color 0.15s;

  &:focus {
    border-color: var(--color-accent);
  }
}

.send-btn {
  height: 44px;
  padding: 0 1.25rem;
  background: var(--color-accent);
  color: white;
  border: none;
  border-radius: var(--radius-md);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  transition: background 0.15s;

  &:hover    { background: var(--color-accent-hover); }
  &:disabled { opacity: 0.5; cursor: not-allowed; }
}
