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

/* Tokens */
:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --border: #2a2a2a;
  --text: #f0f0f0;
  --muted: #888;
  --accent: #7c3aed;
  --accent-hover: #6d28d9;
  --danger: #ef4444;
  --radius: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Nav */
nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 2rem 0rem;
  gap: 0.75rem;
}

.nav-logo {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text);
  letter-spacing: -0.02em;
  flex-shrink: 0;
}

.nav-links {
  display: flex;
  gap: 1.25rem;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.nav-links a { color: var(--muted); font-size: 0.9rem; }
.nav-links a:hover { color: var(--text); text-decoration: none; }

/* Hamburger menu toggle — hidden by default */
.nav-toggle {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.25rem;
  color: var(--muted);
  flex-shrink: 0;
}

.nav-toggle svg { display: block; }

@media (max-width: 640px) {
  nav {
    padding: 0.75rem 1rem;
    flex-wrap: wrap;
  }

  .nav-toggle {
    display: block;
  }

  .nav-links {
    display: none;
    width: 100%;
    flex-direction: column;
    align-items: flex-start;
    gap: 0;
    border-top: 1px solid var(--border);
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    padding-bottom: 0.25rem;
  }

  .nav-links.open { display: flex; }

  .nav-links a,
  .nav-links form {
    width: 100%;
    padding: 0.6rem 0;
    border-bottom: 1px solid rgba(255,255,255,0.04);
    font-size: 0.95rem;
  }

  .nav-links form button {
    padding: 0;
    background: none;
    border: none;
    color: var(--muted);
    font-size: 0.95rem;
    cursor: pointer;
    font-family: var(--font);
    text-align: left;
  }
}

/* Buttons */
.btn {
  display: inline-block;
  padding: 0.55rem 1.25rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: background 0.15s;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); text-decoration: none; }

.btn-outline {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-outline:hover { background: var(--surface); text-decoration: none; }

.btn-full { width: 100%; text-align: center; padding: 0.75rem; }

/* Hero */
.hero {
  max-width: 680px;
  margin: 6rem auto 0;
  padding: 0 2rem;
  text-align: center;
}

.hero h1 {
  font-size: clamp(2rem, 5vw, 3.25rem);
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.15;
  margin-bottom: 1.25rem;
}

.hero h1 span { color: var(--accent); }

.hero p {
  font-size: 1.1rem;
  color: var(--muted);
  margin-bottom: 2rem;
  max-width: 480px;
  margin-left: auto;
  margin-right: auto;
}

.hero-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  flex-wrap: wrap;
}

/* Features */
.features {
  max-width: 900px;
  margin: 6rem auto;
  padding: 0 2rem;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1.5rem;
}

.feature-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

.feature-card .icon {
  font-size: 1.5rem;
  margin-bottom: 0.75rem;
}

.feature-card h3 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 0.4rem;
}

.feature-card p {
  font-size: 0.875rem;
  color: var(--muted);
  line-height: 1.6;
}

/* Auth pages */
.auth-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.auth-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}

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

.auth-card h1 {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 0.4rem;
}

.auth-card .subtitle {
  font-size: 0.875rem;
  color: var(--muted);
  margin-bottom: 1.75rem;
}

/* Form */
.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 0.4rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.form-group input {
  width: 100%;
  padding: 0.65rem 0.85rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.95rem;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.15s;
}

.form-group input:focus {
  border-color: var(--accent);
}

.form-group input::placeholder { color: var(--muted); }

.form-footer {
  margin-top: 1.25rem;
  text-align: center;
  font-size: 0.85rem;
  color: var(--muted);
}

/* Banners */
.banner {
  padding: 0.65rem 1.5rem;
  font-size: 0.85rem;
  text-align: center;
}
.banner-warning { background: rgba(234,179,8,0.15); color: #ca8a04; }
.banner-success { background: rgba(34,197,94,0.15); color: #16a34a; }

/* Chat layout */
body.chat-page {
  min-height: 0;                        /* override body's min-height: 100vh */
  height: 100vh;                        /* baseline fallback */
  height: calc(var(--vh, 1vh) * 100);  /* JS-computed: shrinks with keyboard on iOS */
  height: 100dvh;                       /* modern browsers (overrides above if supported) */
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

body.chat-page nav { flex-shrink: 0; }

/* Outer row: sidebar + chat */
.chat-outer {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* History sidebar */
.chat-sidebar {
  width: 200px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-sidebar-header {
  padding: 0.85rem 1rem 0.5rem;
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  flex-shrink: 0;
}

.chat-sidebar-list {
  overflow-y: auto;
  padding: 0 0.5rem 1rem;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}

.chat-sidebar-item {
  display: block;
  padding: 0.45rem 0.6rem;
  border-radius: 6px;
  text-decoration: none;
  color: var(--muted);
  line-height: 1.3;
  transition: background 0.1s;
}
.chat-sidebar-item:hover { background: var(--surface); color: var(--text); text-decoration: none; }
.chat-sidebar-item.active { background: var(--surface); color: var(--text); }

.chat-sidebar-date {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text);
  display: block;
}
.chat-sidebar-item.active .chat-sidebar-date,
.chat-sidebar-item:hover .chat-sidebar-date { color: var(--text); }

.chat-sidebar-label {
  font-size: 0.72rem;
  color: var(--muted);
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 0.1rem;
}

.chat-sidebar-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.4rem 0.6rem;
}

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

@media (max-width: 768px) {
  .chat-sidebar { display: none; }
}

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

/* Empty state */
.chat-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
  gap: 0.4rem;
  padding: 4rem 0;
}

.chat-empty-greeting {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text);
}

/* Messages */
.message {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 0.6rem;
  max-width: 80%;
}

@media (max-width: 640px) {
  .message { max-width: 92%; }
  .chat-wrap { padding: 0 0.75rem; }
  .chat-outer { flex-direction: column; }
}

.message.user      { align-self: flex-end; flex-direction: row-reverse; }
.message.assistant { align-self: flex-start; }

.msg-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  margin-top: 0.35rem;
}

.msg-avatar.user-avatar {
  background: var(--accent);
  color: #fff;
}

.msg-avatar.bot-avatar {
  width: 42px;
  height: 42px;
  background: #e8e4ff;
  border: 1px solid var(--border);
  overflow: hidden;
}

.msg-avatar.bot-avatar img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  padding: 2px;
}

.bubble {
  padding: 0.7rem 1rem;
  line-height: 1.65;
  font-size: 0.95rem;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.message.user .bubble {
  background: var(--accent);
  color: #fff;
  border-radius: 18px 18px 4px 18px;
}

.message.assistant .bubble {
  min-width: 2.5rem;
  min-height: 2.75rem;
  white-space: normal;
}

/* Typing cursor */
.cursor {
  display: inline-block;
  animation: blink 0.75s step-end infinite;
}

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

/* Markdown inside assistant bubbles */
.message.assistant .bubble p         { margin-bottom: 0.5em; }
.message.assistant .bubble p:last-child { margin-bottom: 0; }

.message.assistant .bubble h1,
.message.assistant .bubble h2,
.message.assistant .bubble h3        { font-weight: 600; line-height: 1.3; margin: 0.8em 0 0.35em; }
.message.assistant .bubble h1        { font-size: 1.15em; }
.message.assistant .bubble h2        { font-size: 1.05em; }
.message.assistant .bubble h3        { font-size: 0.95em; }

.message.assistant .bubble ul,
.message.assistant .bubble ol        { padding-left: 1.4em; margin: 0.35em 0; }
.message.assistant .bubble li        { margin: 0.15em 0; }

.message.assistant .bubble table     { width: 100%; border-collapse: collapse; font-size: 0.875em; margin: 0.6em 0; }
.message.assistant .bubble th,
.message.assistant .bubble td        { border: 1px solid var(--border); padding: 0.4em 0.7em; text-align: left; }
.message.assistant .bubble th        { background: rgba(255,255,255,0.06); font-weight: 600; }
.message.assistant .bubble tr:nth-child(even) td { background: rgba(255,255,255,0.02); }

.message.assistant .bubble code      { font-family: ui-monospace, monospace; font-size: 0.875em; background: rgba(0,0,0,0.35); padding: 0.1em 0.35em; border-radius: 3px; }
.message.assistant .bubble pre       { background: rgba(0,0,0,0.35); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.75em 1em; overflow-x: auto; margin: 0.5em 0; }
.message.assistant .bubble pre code  { background: none; padding: 0; }

.message.assistant .bubble hr        { border: none; border-top: 1px solid var(--border); margin: 0.75em 0; }
.message.assistant .bubble blockquote { border-left: 3px solid var(--accent); margin: 0.5em 0; padding: 0.2em 0.75em; color: var(--muted); }

/* Input area */
.chat-input-area {
  flex-shrink: 0;
  padding: 0.75rem 0 1.5rem;
  padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}

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

.chat-input {
  flex: 1;
  padding: 0.65rem 0.85rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.95rem;
  font-family: var(--font);
  line-height: 1.5;
  resize: none;
  outline: none;
  max-height: 160px;
  overflow-y: auto;
  transition: border-color 0.15s;
}

.chat-input:focus { border-color: var(--accent); }
.chat-input::placeholder { color: var(--muted); }

.send-btn { flex-shrink: 0; }

/* Error */
.error-msg {
  background: rgba(239,68,68,0.1);
  border: 1px solid rgba(239,68,68,0.3);
  color: #fca5a5;
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  margin-bottom: 1rem;
}
