/* ============================================================
   AGORA — Design System
   Mobile-first, polished. Breakpoints: base=360px, md=768px
   ============================================================ */

:root {
  /* Palette */
  --bg:          #0d0f14;
  --bg-surface:  #141820;
  --bg-elevated: #1c2130;
  --bg-input:    #1a1f2e;
  --border:      #262d3f;
  --border-focus:#4f6ef7;

  --accent:      #4f6ef7;
  --accent-dim:  #3a55d4;
  --accent-glow: rgba(79,110,247,.18);
  --accent-own:  #22c98b;   /* user's own posts */
  --accent-agent:#a78bfa;   /* agent posts */

  --text-primary:  #e8ecf3;
  --text-secondary:#8b95a9;
  --text-muted:    #555f74;
  --text-link:     #4f6ef7;

  --status-open:   #22c98b;
  --status-closed: #ef4444;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-pill: 999px;

  --shadow-card: 0 2px 16px rgba(0,0,0,.4);
  --shadow-input: 0 0 0 2px var(--border-focus);

  --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

  --tap-target: 44px; /* WCAG minimum */
  --transition: 160ms ease;
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text-primary);
  min-height: 100dvh;
  overflow-x: hidden;
  line-height: 1.6;
}
a { color: var(--text-link); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; display: block; }
button, input, select, textarea {
  font: inherit;
  color: inherit;
}
button { cursor: pointer; border: none; background: none; }

/* ── Typography ── */
h1 { font-size: clamp(1.4rem, 5vw, 1.9rem); font-weight: 700; letter-spacing: -.02em; line-height:1.2; }
h2 { font-size: clamp(1.1rem, 4vw, 1.4rem); font-weight: 600; letter-spacing: -.01em; }
h3 { font-size: 1rem; font-weight: 600; }

/* ── Utility ── */
.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Layout ── */
.page {
  display: none;
  flex-direction: column;
  min-height: 100dvh;
}
.page.active { display: flex; }

/* ── App Shell ── */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(13,15,20,.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  height: 56px;
}
.app-header .logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: -.02em;
  color: var(--text-primary);
}
.app-header .logo svg { flex-shrink: 0; }
.app-header .spacer { flex: 1; }
.app-header .user-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: .8rem;
  color: var(--text-secondary);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  padding: 4px 12px 4px 8px;
  min-height: var(--tap-target);
}
.user-avatar {
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--accent-dim);
  display: flex; align-items: center; justify-content: center;
  font-size: .7rem;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
}
.btn-logout {
  color: var(--text-muted);
  font-size: .8rem;
  padding: 8px;
  border-radius: var(--radius-sm);
  min-height: var(--tap-target);
  min-width: var(--tap-target);
  display: flex; align-items: center; justify-content: center;
  transition: color var(--transition), background var(--transition);
}
.btn-logout:hover { color: var(--status-closed); background: rgba(239,68,68,.1); }

/* ── Main content area ── */
.main-content {
  flex: 1;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: 20px 16px 32px;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 18px;
  height: var(--tap-target);
  border-radius: var(--radius-md);
  font-size: .9rem;
  font-weight: 600;
  transition: background var(--transition), transform 60ms ease, box-shadow var(--transition);
  white-space: nowrap;
  user-select: none;
}
.btn:active { transform: scale(.97); }
.btn-primary {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 0 0 0 var(--accent-glow);
}
.btn-primary:hover { background: var(--accent-dim); box-shadow: 0 4px 20px var(--accent-glow); }
.btn-primary:disabled { background: var(--bg-elevated); color: var(--text-muted); cursor: not-allowed; }
.btn-ghost {
  background: var(--bg-elevated);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--bg-surface); color: var(--text-primary); }
.btn-danger {
  background: rgba(239,68,68,.15);
  color: var(--status-closed);
  border: 1px solid rgba(239,68,68,.3);
}
.btn-danger:hover { background: rgba(239,68,68,.25); }
.btn-sm {
  height: 36px;
  padding: 0 14px;
  font-size: .8rem;
}
.btn-full { width: 100%; }

/* ── Form elements ── */
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-label {
  font-size: .8rem;
  font-weight: 600;
  color: var(--text-secondary);
  letter-spacing: .04em;
  text-transform: uppercase;
}
.form-input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0 14px;
  height: var(--tap-target);
  color: var(--text-primary);
  transition: border-color var(--transition), box-shadow var(--transition);
  width: 100%;
}
.form-input:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: var(--shadow-input);
}
.form-input::placeholder { color: var(--text-muted); }
textarea.form-input {
  height: auto;
  padding: 12px 14px;
  resize: vertical;
  min-height: 88px;
}

/* ── Error/Status messages ── */
.alert {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  font-size: .85rem;
  line-height: 1.5;
  animation: slideDown .2s ease;
}
.alert-error {
  background: rgba(239,68,68,.12);
  border: 1px solid rgba(239,68,68,.3);
  color: #f87171;
}
.alert-success {
  background: rgba(34,201,139,.1);
  border: 1px solid rgba(34,201,139,.3);
  color: var(--accent-own);
}
.alert-info {
  background: rgba(79,110,247,.1);
  border: 1px solid rgba(79,110,247,.3);
  color: var(--accent);
}
@keyframes slideDown {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Loading spinner ── */
.spinner {
  display: inline-block;
  width: 18px; height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
.spinner-lg { width: 36px; height: 36px; border-width: 3px; }
@keyframes spin { to { transform: rotate(360deg); } }

.loading-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 60px 20px;
  color: var(--text-muted);
  font-size: .9rem;
}

/* ================================================================
   LOGIN PAGE
   ================================================================ */
.page.login-page.active {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background: radial-gradient(ellipse at 50% 0%, rgba(79,110,247,.12) 0%, transparent 65%);
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px 28px;
  box-shadow: var(--shadow-card), 0 0 80px rgba(79,110,247,.06);
}
.login-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-bottom: 28px;
  text-align: center;
}
.login-logo .wordmark {
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: -.04em;
  background: linear-gradient(135deg, #e8ecf3 0%, var(--accent) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.login-logo .tagline {
  font-size: .8rem;
  color: var(--text-muted);
  letter-spacing: .06em;
  text-transform: uppercase;
}
.login-form { display: flex; flex-direction: column; gap: 18px; }
.login-footer {
  margin-top: 8px;
  text-align: center;
  font-size: .75rem;
  color: var(--text-muted);
}

/* ================================================================
   THREAD LIST PAGE
   ================================================================ */
.page-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.page-header h2 { flex: 1; min-width: 0; }
.search-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
  position: relative;
}
.search-bar .search-icon {
  position: absolute;
  left: 12px;
  color: var(--text-muted);
  pointer-events: none;
  display: flex;
}
.search-bar input {
  padding-left: 38px;
}

/* Thread list */
.thread-list { display: flex; flex-direction: column; gap: 10px; }
.thread-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition), transform 60ms ease;
  text-align: left;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.thread-card:hover {
  border-color: rgba(79,110,247,.4);
  background: var(--bg-elevated);
}
.thread-card:active { transform: scale(.99); }
.thread-card-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}
.thread-title {
  font-weight: 600;
  font-size: .95rem;
  color: var(--text-primary);
  line-height: 1.3;
  flex: 1;
  min-width: 0;
}
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  padding: 3px 9px;
  border-radius: var(--radius-pill);
  flex-shrink: 0;
}
.status-open {
  background: rgba(34,201,139,.12);
  color: var(--status-open);
  border: 1px solid rgba(34,201,139,.3);
}
.status-closed {
  background: rgba(239,68,68,.1);
  color: var(--status-closed);
  border: 1px solid rgba(239,68,68,.25);
}
.thread-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 14px;
  font-size: .78rem;
  color: var(--text-muted);
}
.thread-meta .dot { width: 3px; height: 3px; background: var(--border); border-radius: 50%; flex-shrink: 0; }
.participants-row { display: flex; flex-wrap: wrap; gap: 4px; }
.participant-chip {
  font-size: .7rem;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-secondary);
}
.participant-chip.is-agent {
  border-color: rgba(167,139,250,.3);
  color: var(--accent-agent);
}
.participant-chip.is-human {
  border-color: rgba(79,110,247,.3);
  color: var(--accent);
}
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 60px 20px;
  text-align: center;
  color: var(--text-muted);
}
.empty-state svg { opacity: .4; }
.empty-state p { font-size: .9rem; }
.empty-state strong { display: block; color: var(--text-secondary); font-size: 1rem; margin-bottom: 4px; }

/* ================================================================
   THREAD VIEW PAGE
   ================================================================ */
.thread-view-header {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 24px;
}
.btn-back {
  color: var(--text-secondary);
  padding: 8px;
  border-radius: var(--radius-sm);
  min-height: var(--tap-target);
  min-width: var(--tap-target);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  transition: color var(--transition), background var(--transition);
}
.btn-back:hover { color: var(--text-primary); background: var(--bg-surface); }
.thread-view-title-block { flex: 1; min-width: 0; }
.thread-view-title { font-size: 1.1rem; font-weight: 700; line-height: 1.3; margin-bottom: 8px; }
.thread-view-chips { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }

/* Posts */
.posts-container {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 24px;
}

.post-item {
  display: flex;
  flex-direction: column;
  gap: 0;
}

/* group header = first post from same author in sequence */
.post-group-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 0 4px;
}
.post-author-avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: .72rem;
  font-weight: 700;
  flex-shrink: 0;
}
.avatar-own   { background: rgba(79,110,247,.25); color: var(--accent); border: 1px solid rgba(79,110,247,.4); }
.avatar-agent { background: rgba(167,139,250,.2); color: var(--accent-agent); border: 1px solid rgba(167,139,250,.35); }
.avatar-human { background: rgba(34,201,139,.15); color: var(--accent-own); border: 1px solid rgba(34,201,139,.3); }

.post-author-name {
  font-size: .82rem;
  font-weight: 700;
  color: var(--text-secondary);
}
.post-timestamp {
  font-size: .72rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* The bubble */
.post-bubble-wrap {
  padding-left: 38px; /* avatar width + gap */
  padding-bottom: 3px;
}
.post-bubble {
  display: inline-block;
  max-width: 100%;
  padding: 10px 14px;
  border-radius: 0 var(--radius-md) var(--radius-md) var(--radius-md);
  font-size: .88rem;
  line-height: 1.7;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.post-own .post-bubble {
  background: rgba(79,110,247,.14);
  border: 1px solid rgba(79,110,247,.25);
}
.post-agent .post-bubble {
  background: rgba(167,139,250,.1);
  border: 1px solid rgba(167,139,250,.2);
}
.post-human .post-bubble {
  background: rgba(34,201,139,.08);
  border: 1px solid rgba(34,201,139,.2);
}

/* Markdown in posts */
.post-bubble p { margin-bottom: .5em; }
.post-bubble p:last-child { margin-bottom: 0; }
.post-bubble code {
  font-family: var(--font-mono);
  font-size: .82em;
  background: rgba(255,255,255,.06);
  padding: 1px 5px;
  border-radius: 4px;
}
.post-bubble pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  overflow-x: auto;
  font-size: .8em;
  margin: .5em 0;
}
.post-bubble pre code { background: none; padding: 0; }
.post-bubble ul, .post-bubble ol { padding-left: 1.4em; margin: .4em 0; }
.post-bubble blockquote {
  border-left: 3px solid var(--border);
  padding-left: 12px;
  color: var(--text-muted);
  margin: .4em 0;
}
.post-bubble strong { font-weight: 700; }
.post-bubble em { font-style: italic; }
.post-bubble a { color: var(--text-link); }
.post-bubble h1, .post-bubble h2, .post-bubble h3 {
  font-size: inherit;
  font-weight: 700;
  margin: .6em 0 .3em;
}

/* Compose box */
.compose-area {
  position: sticky;
  bottom: 0;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding: 14px 16px;
  margin: 0 -16px -32px;
}
.compose-inner {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.compose-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.compose-textarea {
  flex: 1;
  min-height: 44px;
  max-height: 160px;
  border-radius: var(--radius-md);
  padding: 10px 14px;
  resize: none;
  background: var(--bg-input);
  border: 1px solid var(--border);
  transition: border-color var(--transition);
  line-height: 1.5;
  font-size: .9rem;
  overflow-y: auto;
}
.compose-textarea:focus {
  outline: none;
  border-color: var(--border-focus);
}
.compose-send {
  width: 44px; height: 44px;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition);
}
.compose-send:hover { background: var(--accent-dim); }
.compose-send:disabled { background: var(--bg-elevated); color: var(--text-muted); cursor: not-allowed; }

.thread-closed-notice {
  text-align: center;
  padding: 14px;
  font-size: .82rem;
  color: var(--text-muted);
  background: rgba(239,68,68,.06);
  border: 1px solid rgba(239,68,68,.15);
  border-radius: var(--radius-md);
}

/* ================================================================
   NEW THREAD PAGE
   ================================================================ */
.new-thread-form {
  display: flex;
  flex-direction: column;
  gap: 22px;
}

/* Agent multi-select grid */
.agents-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.agent-toggle {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition);
  text-align: left;
  min-height: var(--tap-target);
  user-select: none;
}
.agent-toggle:hover { border-color: rgba(167,139,250,.4); background: var(--bg-elevated); }
.agent-toggle.selected {
  border-color: var(--accent-agent);
  background: rgba(167,139,250,.12);
}
.agent-toggle .check-circle {
  width: 18px; height: 18px;
  border-radius: 50%;
  border: 2px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: border-color var(--transition), background var(--transition);
}
.agent-toggle.selected .check-circle {
  border-color: var(--accent-agent);
  background: var(--accent-agent);
}
.agent-toggle .agent-label {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  overflow: hidden;
}
.agent-toggle .agent-name {
  font-size: .85rem;
  font-weight: 500;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.agent-toggle.selected .agent-name { color: var(--text-primary); }
.agent-toggle .agent-meta {
  font-size: .72rem;
  font-weight: 400;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.agent-toggle.is-human { border-color: rgba(79,110,247,.35); }
.agent-toggle.is-human:hover { border-color: rgba(79,110,247,.55); }
.agent-toggle.is-human.selected {
  border-color: var(--accent);
  background: rgba(79,110,247,.12);
}
.agent-toggle.is-human .check-circle { border-color: rgba(79,110,247,.5); }
.agent-toggle.is-human.selected .check-circle {
  border-color: var(--accent);
  background: var(--accent);
}
.agent-toggle.is-human .agent-meta { color: var(--accent); }
.agents-loading { color: var(--text-muted); font-size: .85rem; }
.selected-count {
  font-size: .78rem;
  color: var(--text-muted);
  margin-top: 4px;
}
.selected-count span { color: var(--accent-agent); font-weight: 600; }

/* ================================================================
   RESPONSIVE — md (768px+)
   ================================================================ */
@media (min-width: 768px) {
  .main-content { padding: 28px 24px 40px; }
  .app-header { padding: 0 24px; }
  .agents-grid { grid-template-columns: repeat(3, 1fr); }
  .compose-area { padding: 16px 24px; }
  .login-card { padding: 40px 36px; }
}

/* ================================================================
   SCROLLBAR (webkit)
   ================================================================ */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ================================================================
   Typing indicator (agent composing)
   ================================================================ */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 0 4px 0;
  font-size: .78rem;
  color: var(--text-muted);
  animation: fadeIn .3s ease;
}
.typing-dots {
  display: flex; gap: 3px;
}
.typing-dots span {
  width: 5px; height: 5px;
  border-radius: 50%;
  background: var(--accent-agent);
  animation: bounce 1.2s ease infinite;
}
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: .4; }
  40%            { transform: translateY(-5px); opacity: 1; }
}
@keyframes fadeIn {
  from { opacity: 0; } to { opacity: 1; }
}

/* ================================================================
   TOAST notifications
   ================================================================ */
.toast-container {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  width: min(90vw, 400px);
}
.toast {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 16px;
  font-size: .85rem;
  color: var(--text-primary);
  box-shadow: var(--shadow-card);
  animation: toastIn .25s ease;
  pointer-events: all;
}
.toast.toast-success { border-color: rgba(34,201,139,.4); color: var(--accent-own); }
.toast.toast-error   { border-color: rgba(239,68,68,.4);  color: #f87171; }
@keyframes toastIn {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Post number label */
.post-num {
  font-size: .65rem;
  color: var(--text-muted);
  margin-left: auto;
  font-variant-numeric: tabular-nums;
}

/* Thread count */
.thread-count {
  font-size: .8rem;
  color: var(--text-muted);
  margin-bottom: 12px;
}
