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

:root {
  --bg:               #f5f4ef;
  --sidebar-bg:       #eceae3;
  --surface:          #ffffff;
  --border:           #e2e0d8;
  --text:             #1a1a1a;
  --text-muted:       #888;
  --text-light:       #aaa;
  --user-bg:          #1a1a1a;
  --user-text:        #f5f4ef;
  --accent:           #1a1a1a;
  --tool-call:        #d97706;
  --tool-result:      #16a34a;
  --error:            #dc2626;
  --radius:           16px;
  --sidebar-w:        260px;
}

html, body { height: 100%; }

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

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

body.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  overflow: auto;
}

.auth-wrap {
  width: 100%;
  max-width: 400px;
  padding: 24px 16px;
}

.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 36px 32px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.06);
}

.auth-logo {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin-bottom: 20px;
  color: var(--text);
}

.auth-title {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin-bottom: 24px;
}

.auth-error {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: var(--error);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 14px;
  margin-bottom: 16px;
}

.auth-form { display: flex; flex-direction: column; gap: 16px; }

.form-group { display: flex; flex-direction: column; gap: 6px; }

.form-group label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}

.form-group input {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  font-family: inherit;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.form-group input:focus {
  border-color: #b0aaa0;
  box-shadow: 0 0 0 3px rgba(0,0,0,0.05);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 11px 18px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
  margin-top: 4px;
}
.btn-primary:hover { background: #333; }

/* ── Main layout ─────────────────────────────── */

#layout {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────── */

#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

.sidebar-top {
  padding: 14px 12px 10px;
  flex-shrink: 0;
}

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 14px;
  border-radius: 10px;
  text-decoration: none;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  background: var(--surface);
  border: 1px solid var(--border);
  transition: background 0.15s, box-shadow 0.15s;
  width: 100%;
}
.new-chat-btn:hover {
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}

.chat-list::-webkit-scrollbar { width: 4px; }
.chat-list::-webkit-scrollbar-track { background: transparent; }
.chat-list::-webkit-scrollbar-thumb { background: #ccc8be; border-radius: 2px; }

.chat-list-empty {
  padding: 12px 8px;
  font-size: 13px;
  color: var(--text-muted);
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 8px;
  text-decoration: none;
  color: var(--text);
  font-size: 13.5px;
  cursor: pointer;
  transition: background 0.1s;
  position: relative;
  min-width: 0;
}
.chat-item:hover { background: rgba(0,0,0,0.05); }
.chat-item.active { background: rgba(0,0,0,0.08); }

.chat-item-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item-del {
  flex-shrink: 0;
  opacity: 0;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 2px 4px;
  border-radius: 4px;
  transition: opacity 0.15s, color 0.15s;
  display: flex;
  align-items: center;
}
.chat-item:hover .chat-item-del { opacity: 1; }
.chat-item-del:hover { color: var(--error); }

.sidebar-bottom {
  flex-shrink: 0;
  padding: 10px 12px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  background: none;
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-size: 13.5px;
  color: var(--text-muted);
  padding: 7px 10px;
  border-radius: 8px;
  width: 100%;
  text-align: left;
  transition: background 0.15s, color 0.15s;
}
.sidebar-btn:hover { background: rgba(0,0,0,0.05); color: var(--text); }

.user-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
}

.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  flex-shrink: 0;
}

.user-info { min-width: 0; flex: 1; }

.user-name {
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.logout-link {
  font-size: 12px;
  color: var(--text-muted);
  text-decoration: none;
}
.logout-link:hover { color: var(--error); }

/* ── Main chat area ──────────────────────────── */

#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  min-width: 0;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 28px 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  scroll-behavior: smooth;
}

#messages::-webkit-scrollbar { width: 5px; }
#messages::-webkit-scrollbar-track { background: transparent; }
#messages::-webkit-scrollbar-thumb { background: #d0cec8; border-radius: 3px; }
#messages::-webkit-scrollbar-thumb:hover { background: #b0ada8; }

/* ── Empty state ────────────────────────────── */

#empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
  padding: 40px 0;
  animation: fadeIn 0.3s ease;
}

.empty-title {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.suggestions-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  max-width: 720px;
  width: 100%;
}

.suggestion-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  font-size: 13.5px;
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.1s;
  appearance: none;
}
.suggestion-card:hover {
  border-color: #c0bdb5;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  transform: translateY(-1px);
}
.suggestion-card:active { transform: translateY(0); }

.s-title { font-weight: 500; margin-bottom: 3px; }
.s-desc  { font-size: 12px; color: var(--text-muted); }

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

.message { display: flex; flex-direction: column; animation: fadeIn 0.2s ease; }
.message.user      { align-items: flex-end; }
.message.assistant { align-items: flex-start; max-width: 100%; }

.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  max-width: 78%;
  word-break: break-word;
}

.message.user .bubble {
  background: var(--user-bg);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}

.message.assistant .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  max-width: 100%;
  width: 100%;
}

/* ── Markdown ───────────────────────────────── */

.md p { margin: 4px 0; }
.md p:first-child { margin-top: 0; }
.md p:last-child  { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { font-weight: 600; margin: 14px 0 6px; letter-spacing: -0.01em; }
.md h1 { font-size: 1.2em; } .md h2 { font-size: 1.1em; } .md h3 { font-size: 1em; }
.md ul, .md ol { padding-left: 22px; margin: 6px 0; }
.md li { margin: 2px 0; }
.md code {
  background: rgba(0,0,0,0.06);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
  font-size: 0.88em;
}
.md pre {
  background: #f0efe9;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  overflow-x: auto;
  margin: 10px 0;
}
.md pre code { background: none; padding: 0; font-size: 0.87em; }
.mermaid-wrap { margin: 10px 0; overflow-x: auto; }
.mermaid-wrap svg { max-width: 100%; height: auto; display: block; }
.md blockquote { border-left: 3px solid var(--border); padding-left: 14px; color: var(--text-muted); margin: 8px 0; }
.md strong { font-weight: 600; }
.md hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.md table { border-collapse: collapse; width: 100%; margin: 8px 0; font-size: 0.9em; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.md th { background: rgba(0,0,0,0.03); font-weight: 600; }

/* ── Tool cards ─────────────────────────────── */

.tool-card {
  background: #fafaf8;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin: 6px 0;
  overflow: hidden;
  font-size: 13px;
}
.tool-card.call   { border-left: 3px solid var(--tool-call); }
.tool-card.result { border-left: 3px solid var(--tool-result); }

.tool-card-header {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 12px;
  cursor: pointer;
  user-select: none;
  transition: background 0.1s;
}
.tool-card-header:hover { background: rgba(0,0,0,0.03); }

.tool-icon   { font-size: 13px; flex-shrink: 0; }
.tool-name   { font-family: "SF Mono", "Fira Code", monospace; font-weight: 500; flex: 1; }
.tool-toggle { font-size: 10px; color: var(--text-muted); transition: transform 0.15s; display: inline-block; }
.tool-toggle.open { transform: rotate(90deg); }

.tool-body {
  display: none;
  padding: 8px 12px 10px;
  border-top: 1px solid var(--border);
  font-family: "SF Mono", "Fira Code", monospace;
  font-size: 12px;
  white-space: pre-wrap;
  color: #555;
  background: #f7f7f5;
  max-height: 300px;
  overflow-y: auto;
}
.tool-body.open { display: block; }

/* ── Thinking dots ──────────────────────────── */

.thinking { display: flex; align-items: center; gap: 4px; padding: 4px 2px; }
.dot { width: 5px; height: 5px; background: var(--text-muted); border-radius: 50%; animation: pulse 1.4s infinite ease-in-out both; }
.dot:nth-child(1) { animation-delay: 0s; }
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }

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

#input-area {
  flex-shrink: 0;
  padding: 10px 24px 20px;
  background: var(--bg);
  border-top: 1px solid var(--border);
}

.input-row {
  max-width: 760px;
  margin: 0 auto;
}

.input-box {
  display: flex;
  align-items: flex-end;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 4px 4px 4px 14px;
  gap: 6px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.input-box:focus-within {
  border-color: #b0aaa0;
  box-shadow: 0 0 0 3px rgba(0,0,0,0.05);
}

textarea#input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-family: inherit;
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  min-height: 36px;
  max-height: 180px;
  padding: 8px 0;
  color: var(--text);
  overflow-y: auto;
}
textarea#input::placeholder { color: var(--text-muted); }
textarea#input:disabled { opacity: 0.5; }

#send-btn {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
#send-btn:hover:not(:disabled) { background: #333; }
#send-btn:disabled { background: #ccc; cursor: not-allowed; }

.error-msg { color: var(--error); }

/* ── Modal ──────────────────────────────────── */

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.3);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open { display: flex; }

.modal-card {
  background: var(--surface);
  border-radius: 16px;
  width: 480px;
  max-width: calc(100vw - 32px);
  box-shadow: 0 8px 32px rgba(0,0,0,0.15);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 15px;
}

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 16px;
  padding: 2px 6px;
  border-radius: 6px;
  transition: background 0.1s;
}
.modal-close:hover { background: rgba(0,0,0,0.06); }

.modal-body { padding: 20px; }

/* ── Animations ─────────────────────────────── */

@keyframes pulse {
  0%, 80%, 100% { transform: scale(0.7); opacity: 0.5; }
  40%           { transform: scale(1);   opacity: 1; }
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Responsive ─────────────────────────────── */

@media (max-width: 800px) {
  :root { --sidebar-w: 220px; }
  .suggestions-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 600px) {
  #sidebar { display: none; }
  #messages { padding: 16px 12px 8px; }
  #input-area { padding: 8px 12px 14px; }
  .suggestions-grid { grid-template-columns: repeat(2, 1fr); }
  .bubble { max-width: 92%; }
}
