:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --ink: #1a1d23;
  --ink-soft: #5b6472;
  --border: #e3e6eb;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --error: #dc2626;
  --sidebar-w: 300px;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  height: 100vh;
  overflow: hidden;
}

.hidden { display: none !important; }
.muted { color: var(--ink-soft); }
.tiny { font-size: 12px; }

/* ---------- Login ---------- */
.login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  padding: 16px;
  background: linear-gradient(135deg, #eff4ff 0%, #f7f8fa 60%);
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 32px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 8px 30px rgba(20, 30, 60, 0.08);
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand-dot {
  width: 14px; height: 14px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #7c3aed);
  display: inline-block;
}
.brand-name { font-weight: 700; font-size: 18px; }
.brand-small .brand-name { font-size: 15px; }
.login-card h1 { margin: 18px 0 4px; font-size: 24px; }

label { display: block; margin: 14px 0 4px; font-size: 14px; font-weight: 600; }
input[type="email"], input[type="password"], input[type="text"], input[type="search"],
select, textarea {
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: var(--surface);
  color: var(--ink);
  font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  border-color: var(--accent);
}

.btn {
  border: none;
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  background: #eef0f4;
  color: var(--ink);
}
.btn:hover { filter: brightness(0.96); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-block { width: 100%; margin-top: 14px; }
.link-btn {
  background: none; border: none; color: var(--accent);
  cursor: pointer; font-size: 13px; margin-top: 12px; padding: 4px 0;
}
.link-btn:hover { text-decoration: underline; }

.error-banner {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: var(--error);
  border-radius: 8px;
  padding: 10px 12px;
  margin: 12px 0;
  font-size: 13px;
}

/* ---------- App layout ---------- */
#app-view { display: flex; height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px;
  gap: 8px;
  overflow-y: auto;
}
.sidebar-top {
  display: flex; align-items: center; justify-content: space-between;
}
.icon-btn {
  background: none; border: none; font-size: 17px; cursor: pointer;
  color: var(--ink-soft); padding: 6px; border-radius: 6px;
}
.icon-btn:hover { background: #eef0f4; }
#sidebar-open { display: none; }
#sidebar-close { display: none; }

.field-label {
  font-size: 12px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.04em; color: var(--ink-soft); margin-top: 6px;
}
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.conv-item {
  padding: 9px 10px; border-radius: 8px; cursor: pointer;
  font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  border: 1px solid transparent;
}
.conv-item:hover { background: #f0f3f8; }
.conv-item.active { background: #e8effd; border-color: #c9dbfb; font-weight: 600; }
.conv-empty { color: var(--ink-soft); font-size: 13px; padding: 8px 4px; }

.sidebar-foot { border-top: 1px solid var(--border); padding-top: 8px; }
.sidebar-foot .link-btn { margin-top: 4px; display: block; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 18px; border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.conv-title { font-weight: 700; font-size: 15px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-chip { font-size: 12px; color: var(--ink-soft); }

.thread {
  flex: 1; overflow-y: auto; padding: 24px 18px;
  display: flex; flex-direction: column; gap: 14px;
  max-width: 860px; width: 100%; margin: 0 auto;
}
.empty-state { text-align: center; margin-top: 12vh; color: var(--ink-soft); }
.empty-icon { font-size: 40px; }
.empty-state h2 { color: var(--ink); margin: 10px 0 6px; }

.msg { display: flex; flex-direction: column; max-width: 100%; }
.msg.user { align-items: flex-end; }
.msg .bubble {
  padding: 12px 15px; border-radius: 12px; font-size: 14.5px; line-height: 1.55;
  max-width: 88%; word-wrap: break-word;
}
.msg.user .bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg .role { font-size: 11px; color: var(--ink-soft); margin-bottom: 4px; font-weight: 600; }

.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre {
  background: #0f172a; color: #e2e8f0; padding: 12px; border-radius: 8px;
  overflow-x: auto; font-size: 12.5px; margin: 8px 0;
}
.bubble code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.bubble :not(pre) > code { background: #eef1f6; padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.bubble ul, .bubble ol { margin: 6px 0; padding-left: 22px; }

.meta-details { margin-top: 10px; border-top: 1px solid var(--border); padding-top: 6px; }
.meta-details summary { cursor: pointer; font-size: 12px; color: var(--ink-soft); font-weight: 600; }
.meta-details summary:hover { color: var(--accent); }
.meta-details ul { font-size: 12.5px; color: var(--ink-soft); margin: 6px 0 2px; padding-left: 18px; }

.typing { font-size: 13px; color: var(--ink-soft); font-style: italic; }

.composer {
  display: flex; gap: 10px; align-items: flex-end;
  padding: 14px 18px 18px; background: var(--surface);
  border-top: 1px solid var(--border);
  max-width: 860px; width: 100%; margin: 0 auto;
}
.composer textarea { resize: none; max-height: 140px; }
.composer .btn { flex-shrink: 0; margin-top: 6px; }

/* ---------- Responsive ---------- */
@media (max-width: 820px) {
  .sidebar {
    position: fixed; z-index: 50; left: 0; top: 0; bottom: 0;
    transform: translateX(-105%); transition: transform 0.22s ease;
    box-shadow: 2px 0 18px rgba(0,0,0,0.12);
  }
  .sidebar.open { transform: translateX(0); }
  #sidebar-open, #sidebar-close { display: inline-block; }
  .thread { padding: 16px 12px; }
  .composer { padding: 12px; }
  .msg .bubble { max-width: 95%; }
}
