/* System font stack — no CDN, fast on any VPS */

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

:root {
  --bg-base:       #090910;
  --bg-surface:    #0f0f1a;
  --bg-card:       rgba(255,255,255,0.04);
  --bg-card-hover: rgba(255,255,255,0.07);
  --border:        rgba(255,255,255,0.08);
  --border-bright: rgba(255,255,255,0.15);
  --accent-1:      #6366f1;
  --accent-2:      #a855f7;
  --accent-grad:   linear-gradient(135deg, #6366f1, #a855f7);
  --accent-glow:   rgba(99,102,241,0.35);
  --text-primary:  #e2e8f0;
  --text-secondary:#94a3b8;
  --text-muted:    #475569;
  --green:         #10b981;
  --green-dim:     rgba(16,185,129,0.15);
  --red:           #ef4444;
  --red-dim:       rgba(239,68,68,0.12);
  --radius-sm:     8px;
  --radius-md:     12px;
  --radius-lg:     18px;
  --radius-xl:     24px;
  --shadow-glow:   0 0 40px rgba(99,102,241,0.15);
  --transition:    0.2s cubic-bezier(0.4,0,0.2,1);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg-base);
  color: var(--text-primary);
  overflow: hidden;
}

/* ── Scrollbar ─────────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.2); }

/* ── Animated BG Orbs ─────────────────────────────────────────────────────── */
.bg-orbs {
  position: fixed; inset: 0; pointer-events: none; z-index: 0;
  overflow: hidden;
}
.bg-orbs::before, .bg-orbs::after {
  content: '';
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.25;
  animation: orbFloat 12s ease-in-out infinite alternate;
}
.bg-orbs::before {
  width: 600px; height: 600px;
  background: radial-gradient(circle, #6366f1, transparent 70%);
  top: -200px; left: -150px;
}
.bg-orbs::after {
  width: 500px; height: 500px;
  background: radial-gradient(circle, #a855f7, transparent 70%);
  bottom: -150px; right: -100px;
  animation-delay: -6s;
}
@keyframes orbFloat {
  from { transform: translate(0,0) scale(1); }
  to   { transform: translate(40px, 30px) scale(1.08); }
}

/* ── Layout ────────────────────────────────────────────────────────────────── */
#app {
  position: relative; z-index: 1;
  display: flex; flex-direction: column;
  height: 100vh;
}

/* ── Header ────────────────────────────────────────────────────────────────── */
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px;
  height: 60px;
  border-bottom: 1px solid var(--border);
  background: rgba(9,9,16,0.85);
  backdrop-filter: blur(20px);
  flex-shrink: 0;
  position: relative; z-index: 10;
}

.logo {
  display: flex; align-items: center; gap: 10px;
  font-weight: 700; font-size: 1.15rem; letter-spacing: -0.02em;
}
.logo-icon {
  width: 32px; height: 32px; border-radius: 8px;
  background: var(--accent-grad);
  display: flex; align-items: center; justify-content: center;
  font-size: 16px;
  box-shadow: 0 0 16px var(--accent-glow);
}
.logo-text { background: var(--accent-grad); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }

.header-right { display: flex; align-items: center; gap: 12px; }

.balance-chip {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 12px;
  border-radius: 99px;
  background: var(--green-dim);
  border: 1px solid rgba(16,185,129,0.25);
  font-size: 0.82rem; font-weight: 600;
  color: var(--green);
  transition: var(--transition);
}
.balance-chip .balance-icon { font-size: 0.75rem; }
#balance-value { font-family: ui-monospace, 'SF Mono', Consolas, 'Liberation Mono', monospace; }

.btn-icon {
  width: 36px; height: 36px; border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-secondary);
  cursor: pointer; font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  transition: var(--transition);
}
.btn-icon:hover { background: var(--bg-card-hover); color: var(--text-primary); border-color: var(--border-bright); }

.btn-admin {
  padding: 6px 14px; border-radius: var(--radius-sm);
  border: 1px solid rgba(99,102,241,0.4);
  background: rgba(99,102,241,0.1);
  color: var(--accent-1); font-size: 0.8rem; font-weight: 600;
  cursor: pointer; text-decoration: none;
  transition: var(--transition);
}
.btn-admin:hover { background: rgba(99,102,241,0.2); border-color: var(--accent-1); }

/* ── Main Chat Area ────────────────────────────────────────────────────────── */
main {
  flex: 1; display: flex; flex-direction: column;
  overflow: hidden; position: relative;
}

/* ── Messages ──────────────────────────────────────────────────────────────── */
#messages {
  flex: 1; overflow-y: auto;
  padding: 32px 0;
  display: flex; flex-direction: column;
  gap: 0;
  scroll-behavior: smooth;
}

.messages-inner {
  max-width: 760px; width: 100%;
  margin: 0 auto; padding: 0 24px;
  display: flex; flex-direction: column; gap: 6px;
}

/* Welcome screen */
.welcome {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 16px; padding: 60px 24px;
  text-align: center;
  animation: fadeUp 0.6s ease;
}
.welcome-icon {
  width: 72px; height: 72px; border-radius: 20px;
  background: var(--accent-grad);
  display: flex; align-items: center; justify-content: center;
  font-size: 36px;
  box-shadow: 0 0 40px var(--accent-glow);
  animation: pulse 3s ease-in-out infinite;
}
@keyframes pulse {
  0%,100% { box-shadow: 0 0 40px var(--accent-glow); }
  50%      { box-shadow: 0 0 60px rgba(99,102,241,0.5); }
}
.welcome h1 {
  font-size: 2rem; font-weight: 700; letter-spacing: -0.03em;
  background: var(--accent-grad); -webkit-background-clip: text; -webkit-text-fill-color: transparent;
}
.welcome p { color: var(--text-secondary); font-size: 0.95rem; max-width: 400px; line-height: 1.6; }

.suggestion-chips {
  display: flex; flex-wrap: wrap; gap: 8px; justify-content: center;
  margin-top: 8px;
}
.chip {
  padding: 8px 16px; border-radius: 99px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-secondary); font-size: 0.82rem;
  cursor: pointer; transition: var(--transition);
  white-space: nowrap;
}
.chip:hover {
  background: var(--bg-card-hover);
  border-color: var(--accent-1);
  color: var(--text-primary);
  transform: translateY(-1px);
}

/* Message bubbles */
.message {
  display: flex; gap: 12px;
  animation: fadeUp 0.3s ease;
  padding: 4px 0;
}
@keyframes fadeUp {
  from { opacity:0; transform: translateY(12px); }
  to   { opacity:1; transform: translateY(0); }
}

.message.user { flex-direction: row-reverse; }

.msg-avatar {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; flex-shrink: 0; margin-top: 4px;
}
.message.assistant .msg-avatar {
  background: var(--accent-grad);
  box-shadow: 0 0 12px var(--accent-glow);
}
.message.user .msg-avatar {
  background: rgba(255,255,255,0.08);
  border: 1px solid var(--border);
}

.msg-content {
  max-width: 78%;
  display: flex; flex-direction: column; gap: 4px;
}

.msg-bubble {
  padding: 12px 16px;
  border-radius: var(--radius-md);
  line-height: 1.65;
  font-size: 0.93rem;
  word-break: break-word;
  position: relative;
}
.message.user .msg-bubble {
  background: var(--accent-grad);
  color: #fff;
  border-bottom-right-radius: 4px;
  box-shadow: 0 4px 20px var(--accent-glow);
}
.message.assistant .msg-bubble {
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  backdrop-filter: blur(8px);
}

/* Streaming cursor */
.streaming-cursor {
  display: inline-block;
  width: 2px; height: 1em;
  background: var(--accent-1);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 0.8s step-end infinite;
  border-radius: 1px;
}
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0} }

.msg-meta {
  font-size: 0.72rem; color: var(--text-muted);
  display: flex; align-items: center; gap: 8px;
  padding: 0 4px;
}
.message.user .msg-meta { flex-direction: row-reverse; }

/* Thinking indicator */
.thinking {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px;
  border-radius: var(--radius-md);
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  width: fit-content;
  animation: fadeUp 0.3s ease;
}
.thinking-dots { display: flex; gap: 4px; }
.thinking-dots span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent-1);
  animation: bounce 1.2s ease-in-out infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%,80%,100% { transform: scale(0.7); opacity:0.5; }
  40%          { transform: scale(1); opacity:1; }
}
.thinking-text { font-size: 0.82rem; color: var(--text-secondary); }

/* Cost badge */
.cost-badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 0.7rem; color: var(--text-muted);
  background: rgba(255,255,255,0.04);
  padding: 2px 8px; border-radius: 99px;
  border: 1px solid var(--border);
}
.cost-badge.deducted { color: var(--green); border-color: rgba(16,185,129,0.2); }

/* Markdown code blocks */
.msg-bubble pre {
  background: rgba(0,0,0,0.4);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  overflow-x: auto;
  margin: 8px 0;
  font-family: ui-monospace, 'SF Mono', Consolas, 'Liberation Mono', monospace;
  font-size: 0.82rem;
}
.msg-bubble code {
  font-family: ui-monospace, 'SF Mono', Consolas, 'Liberation Mono', monospace;
  font-size: 0.85em;
  background: rgba(0,0,0,0.3);
  padding: 1px 5px; border-radius: 4px;
}
.msg-bubble pre code { background: none; padding: 0; }

/* ── Input Area ────────────────────────────────────────────────────────────── */
.input-area {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  background: rgba(9,9,16,0.85);
  backdrop-filter: blur(20px);
  padding: 16px 24px 20px;
}

.input-container {
  max-width: 760px; margin: 0 auto;
  display: flex; flex-direction: column; gap: 10px;
}

.input-row {
  display: flex; align-items: flex-end; gap: 10px;
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px 12px;
  transition: var(--transition);
}
.input-row:focus-within {
  border-color: rgba(99,102,241,0.5);
  box-shadow: 0 0 0 3px rgba(99,102,241,0.1);
}

#user-input {
  flex: 1;
  background: none; border: none; outline: none;
  color: var(--text-primary);
  font-family: inherit; font-size: 0.93rem;
  resize: none; min-height: 24px; max-height: 200px;
  line-height: 1.5;
  padding: 2px 0;
}
#user-input::placeholder { color: var(--text-muted); }

.btn-send {
  width: 36px; height: 36px; border-radius: 10px;
  border: none;
  background: var(--accent-grad);
  color: white;
  cursor: pointer; font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  transition: var(--transition);
  flex-shrink: 0;
  box-shadow: 0 0 16px var(--accent-glow);
}
.btn-send:hover { transform: scale(1.05); box-shadow: 0 0 24px var(--accent-glow); }
.btn-send:active { transform: scale(0.97); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.btn-stop {
  width: 36px; height: 36px; border-radius: 10px;
  border: 1px solid rgba(239,68,68,0.4);
  background: rgba(239,68,68,0.1);
  color: var(--red);
  cursor: pointer; font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  transition: var(--transition); flex-shrink: 0;
}
.btn-stop:hover { background: rgba(239,68,68,0.2); }

.input-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 4px;
}

.model-select-wrap {
  display: flex; align-items: center; gap: 8px;
}
.model-label { font-size: 0.75rem; color: var(--text-muted); }

#model-select {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 0.78rem;
  padding: 4px 8px;
  cursor: pointer;
  outline: none;
  transition: var(--transition);
  font-family: inherit;
}
#model-select:focus { border-color: var(--accent-1); color: var(--text-primary); }

.input-hint { font-size: 0.72rem; color: var(--text-muted); }

/* ── Modals ────────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(8px);
  display: flex; align-items: center; justify-content: center;
  animation: fadein 0.2s ease;
}
@keyframes fadein { from { opacity:0; } to { opacity:1; } }
.modal-overlay.hidden { display: none; }

.modal {
  background: #13131f;
  border: 1px solid var(--border-bright);
  border-radius: var(--radius-xl);
  padding: 32px;
  width: 420px; max-width: 95vw;
  box-shadow: 0 24px 80px rgba(0,0,0,0.6), 0 0 60px rgba(99,102,241,0.08);
  animation: modalIn 0.3s cubic-bezier(0.34,1.56,0.64,1);
}
@keyframes modalIn {
  from { opacity:0; transform: scale(0.9) translateY(20px); }
  to   { opacity:1; transform: scale(1) translateY(0); }
}

.modal-title {
  font-size: 1.2rem; font-weight: 700; margin-bottom: 24px;
  background: var(--accent-grad); -webkit-background-clip: text; -webkit-text-fill-color: transparent;
}

.form-group { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.form-group label { font-size: 0.8rem; color: var(--text-secondary); font-weight: 500; }
.form-group input {
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 0.9rem; font-family: inherit;
  padding: 10px 14px; outline: none;
  transition: var(--transition);
}
.form-group input:focus { border-color: var(--accent-1); box-shadow: 0 0 0 3px rgba(99,102,241,0.1); }

.modal-actions { display: flex; gap: 10px; margin-top: 24px; }

.btn-primary {
  flex: 1; padding: 10px 20px; border-radius: var(--radius-sm);
  border: none; background: var(--accent-grad);
  color: white; font-size: 0.9rem; font-weight: 600;
  cursor: pointer; transition: var(--transition);
  font-family: inherit;
}
.btn-primary:hover { opacity: 0.9; transform: translateY(-1px); box-shadow: 0 8px 24px var(--accent-glow); }

.btn-secondary {
  padding: 10px 20px; border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: transparent; color: var(--text-secondary);
  font-size: 0.9rem; cursor: pointer; transition: var(--transition);
  font-family: inherit;
}
.btn-secondary:hover { background: var(--bg-card-hover); color: var(--text-primary); }

.error-msg {
  font-size: 0.8rem; color: var(--red);
  padding: 8px 12px; border-radius: var(--radius-sm);
  background: var(--red-dim); border: 1px solid rgba(239,68,68,0.2);
  margin-top: 4px; display: none;
}
.error-msg.visible { display: block; }

/* ── Toast ─────────────────────────────────────────────────────────────────── */
#toast-container {
  position: fixed; bottom: 90px; right: 24px;
  z-index: 200; display: flex; flex-direction: column; gap: 8px;
  pointer-events: none;
}
.toast {
  padding: 10px 18px; border-radius: var(--radius-md);
  font-size: 0.85rem; font-weight: 500;
  backdrop-filter: blur(12px);
  animation: toastIn 0.3s cubic-bezier(0.34,1.56,0.64,1);
  pointer-events: auto;
}
.toast.success { background: rgba(16,185,129,0.2); border: 1px solid rgba(16,185,129,0.3); color: #34d399; }
.toast.error   { background: rgba(239,68,68,0.2);  border: 1px solid rgba(239,68,68,0.3);  color: #f87171; }
.toast.info    { background: rgba(99,102,241,0.2); border: 1px solid rgba(99,102,241,0.3); color: #818cf8; }
@keyframes toastIn {
  from { opacity:0; transform: translateX(20px) scale(0.9); }
  to   { opacity:1; transform: translateX(0) scale(1); }
}

/* ── Balance depleted banner ─────────────────────────────────────────────── */
.low-balance-banner {
  max-width: 760px; margin: 0 auto 12px;
  padding: 10px 16px; border-radius: var(--radius-md);
  background: var(--red-dim); border: 1px solid rgba(239,68,68,0.25);
  color: var(--red); font-size: 0.83rem;
  display: none; align-items: center; gap: 8px;
}
.low-balance-banner.visible { display: flex; }

/* ── Responsive ────────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  header { padding: 0 16px; }
  .input-area { padding: 12px 16px 16px; }
  .btn-admin span { display: none; }
}
