:root {
  --primary:       #6366f1;
  --primary-dark:  #4f46e5;
  --purple:        #8b5cf6;

  --bg:            #eef0ff;
  --surface:       #ffffff;
  --surface-2:     #f5f4ff;
  --border:        #e0dfff;

  --text:          #1e1b4b;
  --text-muted:    #7c78b0;

  --gradient:      linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  --glow:          0 4px 16px rgba(99, 102, 241, 0.38);

  --shadow-sm: 0 1px 3px rgba(99,102,241,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg: 0 20px 60px rgba(0,0,0,0.07), 0 8px 24px rgba(99,102,241,0.07);

  --r-sm:   8px;
  --r-md:   16px;
  --r-full: 9999px;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ═══════════════════════════════════════
   LOGIN PAGE
═══════════════════════════════════════ */
#username-page {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: linear-gradient(135deg, #4338ca 0%, #6366f1 45%, #8b5cf6 75%, #a78bfa 100%);
  position: relative;
  overflow: hidden;
}

#username-page::before,
#username-page::after {
  content: '';
  position: absolute;
  border-radius: 50%;
  pointer-events: none;
}
#username-page::before {
  width: 560px; height: 560px;
  top: -180px; left: -160px;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
}
#username-page::after {
  width: 420px; height: 420px;
  bottom: -120px; right: -100px;
  background: radial-gradient(circle, rgba(255,255,255,0.07) 0%, transparent 70%);
}

.username-page-container {
  background: rgba(255,255,255,0.13);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border: 1px solid rgba(255,255,255,0.28);
  border-radius: var(--r-md);
  width: min(420px, 90vw);
  padding: 2.5rem;
  box-shadow:
    0 25px 50px rgba(0,0,0,0.2),
    inset 0 1px 0 rgba(255,255,255,0.35);
  position: relative;
  z-index: 1;
}

/* Logo */
.login-logo {
  text-align: center;
  margin-bottom: 1.75rem;
}
.login-logo-mark {
  width: 60px; height: 60px;
  background: rgba(255,255,255,0.95);
  border-radius: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.875rem;
  margin-bottom: 0.75rem;
  box-shadow: 0 8px 24px rgba(0,0,0,0.18);
}
.login-logo h2 {
  font-size: 1.75rem;
  font-weight: 800;
  color: #fff;
  letter-spacing: -0.5px;
}
.login-logo p {
  font-size: 0.8rem;
  color: rgba(255,255,255,0.62);
  margin-top: 0.2rem;
}

.login-title {
  font-size: 0.9375rem;
  font-weight: 600;
  color: rgba(255,255,255,0.88);
  text-align: center;
  margin-bottom: 1.25rem;
}

/* Form elements */
.form-control {
  width: 100%;
  padding: 0.75rem 1.1rem;
  font-size: 0.9375rem;
  font-family: inherit;
  border: 1.5px solid rgba(255,255,255,0.28);
  border-radius: var(--r-sm);
  background: rgba(255,255,255,0.13);
  color: #fff;
  outline: none;
  transition: all 0.2s ease;
}
.form-control::placeholder { color: rgba(255,255,255,0.52); }
.form-control:focus {
  border-color: rgba(255,255,255,0.62);
  background: rgba(255,255,255,0.2);
  box-shadow: 0 0 0 3px rgba(255,255,255,0.12);
}

.form-group { margin-bottom: 0.875rem; }

.username-submit {
  width: 100%;
  padding: 0.8rem 1.5rem;
  font-size: 0.9375rem;
  font-family: inherit;
  font-weight: 700;
  background: #fff;
  color: var(--primary-dark);
  border: none;
  border-radius: var(--r-sm);
  cursor: pointer;
  letter-spacing: 0.01em;
  box-shadow: 0 4px 16px rgba(0,0,0,0.18);
  transition: all 0.2s ease;
  display: block;
}
.username-submit:hover {
  background: rgba(255,255,255,0.93);
  transform: translateY(-1px);
  box-shadow: 0 6px 22px rgba(0,0,0,0.22);
}
.username-submit:active { transform: translateY(0); }

.login-hint {
  text-align: center;
  font-size: 0.78rem;
  color: rgba(255,255,255,0.52);
  margin-top: 1rem;
}
.login-hint strong { color: rgba(255,255,255,0.82); }

/* ═══════════════════════════════════════
   CHAT PAGE
═══════════════════════════════════════ */
#chat-page {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 1rem;
}

.chat-container {
  width: 100%;
  max-width: 820px;
  height: calc(100vh - 2rem);
  background: var(--surface);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg), 0 0 0 1px var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Header ── */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 1rem;
  height: 62px;
  background: var(--gradient);
  position: relative;
  flex-shrink: 0;
}

.chat-header-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
}

.chat-header h2 {
  font-size: 1rem;
  font-weight: 700;
  color: #fff;
}

.chat-header-hint {
  font-size: 0.67rem;
  color: rgba(255,255,255,0.62);
  letter-spacing: 0.01em;
}

.exit-btn {
  position: absolute;
  left: 1rem;
  padding: 0.4rem 0.9rem;
  font-size: 0.78rem;
  font-family: inherit;
  font-weight: 600;
  background: rgba(255,255,255,0.15);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: var(--r-full);
  cursor: pointer;
  transition: background 0.2s;
}
.exit-btn:hover { background: rgba(255,255,255,0.25); }

.back-btn {
  position: absolute;
  right: 1rem;
  padding: 0.4rem 0.9rem;
  font-size: 0.78rem;
  font-family: inherit;
  font-weight: 600;
  background: rgba(255,255,255,0.15);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: var(--r-full);
  text-decoration: none;
  transition: background 0.2s;
  display: inline-flex;
  align-items: center;
}
.back-btn:hover { background: rgba(255,255,255,0.25); }

/* ── Connecting banner ── */
.connecting {
  flex-shrink: 0;
  padding: 0.4rem 1rem;
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-muted);
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

/* ── Message Area ── */
#messageArea {
  list-style: none;
  padding: 1.25rem 1.5rem;
  margin: 0;
  overflow-y: auto;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
  scroll-behavior: smooth;
}

#messageArea::-webkit-scrollbar { width: 4px; }
#messageArea::-webkit-scrollbar-track { background: transparent; }
#messageArea::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* empty state */
.empty-message {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.875rem;
  padding: 3rem 0;
  font-style: italic;
}

/* ── Event Messages ── */
.event-message {
  display: flex;
  justify-content: center;
  padding: 0.15rem 0;
}

.event-message p {
  display: inline-block;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-full);
  padding: 0.18rem 0.875rem;
  font-size: 0.74rem;
  color: var(--text-muted);
  font-weight: 500;
}

/* ── Chat Messages ── */
.chat-message {
  display: flex;
  flex-direction: column;
  max-width: 70%;
}

.chat-message.self {
  align-self: flex-end;
  align-items: flex-end;
}

.chat-message.other {
  align-self: flex-start;
  align-items: flex-start;
}

/* sender name above bubble (others only) */
.sender-name {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-muted);
  padding-left: 36px;
  margin-bottom: 3px;
}

/* row: avatar + bubble side by side */
.message-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.chat-message.self .message-row {
  flex-direction: row-reverse;
}

/* avatar */
.chat-message i {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-style: normal;
  font-weight: 700;
  color: #fff;
  font-size: 0.7rem;
  text-transform: uppercase;
}

/* bubbles */
.chat-message p {
  margin: 0;
  padding: 0.6rem 0.9rem;
  font-size: 0.9rem;
  line-height: 1.55;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.chat-message.self p {
  background: var(--gradient);
  color: #fff;
  border-radius: 18px 18px 4px 18px;
  box-shadow: var(--glow);
}

.chat-message.other p {
  background: var(--surface-2);
  color: var(--text);
  border-radius: 4px 18px 18px 18px;
  border: 1px solid var(--border);
}

/* ── AI Messages ── */
.ai-message { max-width: 78%; }

.ai-label {
  font-size: 0.67rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--primary);
  background: rgba(99,102,241,0.1);
  border-radius: var(--r-full);
  padding: 2px 8px;
  margin-bottom: 4px;
  display: inline-block;
}

/* plain <p> fallback for non-AI bubbles still needs styling */
.ai-message p {
  background: linear-gradient(135deg, rgba(99,102,241,0.07), rgba(139,92,246,0.05));
  color: var(--text);
  border: 1px solid rgba(99,102,241,0.18);
  border-radius: 4px 18px 18px 18px;
}

/* ── Rendered Markdown bubble (AI only) ── */
.ai-content {
  padding: 0.7rem 1rem;
  font-size: 0.9rem;
  line-height: 1.6;
  word-wrap: break-word;
  background: linear-gradient(135deg, rgba(99,102,241,0.07), rgba(139,92,246,0.05));
  color: var(--text);
  border: 1px solid rgba(99,102,241,0.18);
  border-radius: 4px 18px 18px 18px;
  min-width: 0;
}

/* inline elements */
.ai-content strong { font-weight: 700; }
.ai-content em     { font-style: italic; }
.ai-content a      { color: var(--primary); text-decoration: underline; }

/* inline code */
.ai-content code {
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 0.82em;
  background: rgba(99,102,241,0.12);
  color: var(--primary-dark);
  padding: 0.1em 0.38em;
  border-radius: 4px;
}

/* code blocks */
.ai-content pre {
  background: #1e1b4b;
  border-radius: 10px;
  padding: 0.875rem 1rem;
  margin: 0.6rem 0;
  overflow-x: auto;
}
.ai-content pre code {
  background: transparent;
  color: #c4c0ff;
  padding: 0;
  font-size: 0.83em;
  border-radius: 0;
}

/* lists */
.ai-content ul,
.ai-content ol {
  padding-left: 1.35rem;
  margin: 0.4rem 0;
}
.ai-content li { margin-bottom: 0.2rem; }

/* paragraphs inside markdown */
.ai-content p {
  margin: 0 0 0.45rem;
  background: none;
  border: none;
  border-radius: 0;
  padding: 0;
}
.ai-content p:last-child { margin-bottom: 0; }

/* headings (if AI uses them) */
.ai-content h1,
.ai-content h2,
.ai-content h3 {
  font-weight: 700;
  margin: 0.5rem 0 0.25rem;
  color: var(--text);
}
.ai-content h1 { font-size: 1.05em; }
.ai-content h2 { font-size: 0.98em; }
.ai-content h3 { font-size: 0.93em; }

/* blockquote */
.ai-content blockquote {
  border-left: 3px solid rgba(99,102,241,0.4);
  margin: 0.4rem 0;
  padding: 0.2rem 0.75rem;
  color: var(--text-muted);
  font-style: italic;
}

/* horizontal rule */
.ai-content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.5rem 0;
}

/* ── @ai Autocomplete Suggestion ── */
.input-wrapper {
  flex: 1;
  position: relative;
}

.input-wrapper .form-control {
  width: 100%;
}

#ai-suggestion {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  box-shadow: 0 8px 24px rgba(99,102,241,0.18), 0 2px 8px rgba(0,0,0,0.08);
  overflow: hidden;
  z-index: 50;
  animation: suggestionIn 0.12s ease;
}

@keyframes suggestionIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.ai-suggestion-item {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.625rem 0.875rem;
  cursor: pointer;
  transition: background 0.15s;
}
.ai-suggestion-item:hover { background: rgba(99,102,241,0.07); }

.ai-sug-avatar {
  width: 26px;
  height: 26px;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 0.7rem;
  flex-shrink: 0;
}

.ai-sug-text {
  flex: 1;
  font-size: 0.875rem;
  color: var(--text);
}
.ai-sug-text strong { color: var(--primary); }

.ai-sug-key {
  font-size: 0.67rem;
  font-family: inherit;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 6px;
  flex-shrink: 0;
}

/* ── Message Form ── */
#messageForm {
  flex-shrink: 0;
  padding: 0.875rem 1.25rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.input-group {
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

#messageForm .form-control {
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  border-radius: var(--r-full);
  color: var(--text);
  padding: 0.65rem 1.25rem;
  font-size: 0.9rem;
}

#messageForm .form-control::placeholder { color: var(--text-muted); }

#messageForm .form-control:focus {
  border-color: var(--primary);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(99,102,241,0.12);
  outline: none;
}

.send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--gradient);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: var(--glow);
  transition: all 0.2s ease;
}
.send-btn:hover { transform: scale(1.08); box-shadow: 0 6px 20px rgba(99,102,241,0.45); }
.send-btn:active { transform: scale(0.95); }

/* ── Responsive ── */
@media (max-width: 640px) {
  #chat-page { padding: 0; }
  .chat-container {
    border-radius: 0;
    height: 100vh;
    height: 100dvh;
    box-shadow: none;
    border: none;
  }
  #messageArea { padding: 1rem; }
  .chat-message { max-width: 85%; }
  .ai-message { max-width: 90%; }
}
