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

:root {
  --bg: #0a0a0f;
  --bg2: #111118;
  --bg3: #1a1a24;
  --border: #2a2a3a;
  --border2: #3a3a50;
  --text: #e8e8f0;
  --text2: #9090b0;
  --text3: #5a5a7a;
  --accent: #7c6fff;
  --accent2: #a89eff;
  --accent-glow: rgba(124, 111, 255, 0.15);
  --green: #4ade80;
  --green-dim: rgba(74, 222, 128, 0.1);
  --red: #f87171;
  --mono: 'IBM Plex Mono', monospace;
  --sans: 'Space Grotesk', sans-serif;
}

html, body { height: 100%; background: var(--bg); color: var(--text); font-family: var(--sans); overflow: hidden; }

/* ── AUTH ───────────────────────────────────────────────── */
#auth-screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  z-index: 100;
}

.auth-box {
  width: 380px;
  padding: 48px 40px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 16px;
  text-align: center;
}

.auth-logo {
  display: flex; align-items: center; justify-content: center; gap: 14px;
  margin-bottom: 12px;
}

.logo-ring {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  box-shadow: 0 0 16px var(--accent-glow);
  animation: pulse 3s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 8px var(--accent-glow); }
  50% { box-shadow: 0 0 24px rgba(124,111,255,0.4); }
}

.auth-logo span {
  font-family: var(--mono); font-size: 28px; font-weight: 500;
  color: var(--accent2); letter-spacing: 4px;
}

.auth-subtitle { font-size: 12px; color: var(--text3); letter-spacing: 0.5px; margin-bottom: 36px; }

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

#key-input {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px 16px;
  color: var(--text); font-family: var(--mono); font-size: 14px;
  text-align: center; letter-spacing: 2px;
  outline: none; transition: border-color 0.2s;
}
#key-input:focus { border-color: var(--accent); }

#auth-btn {
  background: var(--accent); border: none; border-radius: 8px;
  padding: 12px; color: #fff; font-family: var(--sans);
  font-size: 14px; font-weight: 500; cursor: pointer;
  transition: opacity 0.2s, transform 0.1s;
}
#auth-btn:hover { opacity: 0.9; }
#auth-btn:active { transform: scale(0.98); }

.auth-error { font-size: 12px; color: var(--red); margin-top: 8px; min-height: 18px; }

/* ── APP LAYOUT ─────────────────────────────────────────── */
#app { display: flex; height: 100vh; }
.hidden { display: none !important; }

/* ── SIDEBAR ────────────────────────────────────────────── */
#sidebar {
  width: 240px; min-width: 240px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 20px 16px 16px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}

.logo-small {
  font-family: var(--mono); font-size: 16px; font-weight: 500;
  color: var(--accent2); letter-spacing: 3px;
}

.new-chat-btn {
  background: none; border: 1px solid var(--border);
  color: var(--text2); font-size: 12px; font-family: var(--sans);
  padding: 5px 10px; border-radius: 6px; cursor: pointer;
  transition: all 0.2s;
}
.new-chat-btn:hover { border-color: var(--accent); color: var(--accent2); }

.sidebar-section { flex: 1; overflow-y: auto; padding: 12px 8px; }

.section-label {
  font-size: 10px; font-family: var(--mono); color: var(--text3);
  letter-spacing: 1px; padding: 0 8px 8px; text-transform: uppercase;
}

.conv-item {
  padding: 10px 12px; border-radius: 8px;
  cursor: pointer; transition: background 0.15s;
  border: 1px solid transparent; margin-bottom: 2px;
}
.conv-item:hover { background: var(--bg3); }
.conv-item.active { background: var(--accent-glow); border-color: var(--border2); }
.conv-item .conv-preview { font-size: 13px; color: var(--text2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .conv-date { font-size: 10px; color: var(--text3); font-family: var(--mono); margin-top: 3px; }

.sidebar-bottom { padding: 12px; border-top: 1px solid var(--border); }

.status-btn {
  width: 100%; padding: 10px;
  background: var(--green-dim); border: 1px solid rgba(74,222,128,0.2);
  color: var(--green); border-radius: 8px;
  font-size: 12px; font-family: var(--mono); cursor: pointer;
  transition: all 0.2s;
}
.status-btn:hover { background: rgba(74,222,128,0.2); }

/* ── MAIN CHAT ──────────────────────────────────────────── */
#main {
  flex: 1; display: flex; flex-direction: column;
  overflow: hidden; background: var(--bg);
}

#chat-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center;
}

.chat-title-area { display: flex; align-items: center; gap: 12px; }

#chat-title { font-size: 14px; color: var(--text2); font-family: var(--mono); }

.improvement-badge {
  font-size: 10px; font-family: var(--mono);
  color: var(--green); background: var(--green-dim);
  padding: 3px 8px; border-radius: 20px;
  animation: blink 2s ease-in-out infinite;
}
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0.5} }

#messages-container { flex: 1; overflow-y: auto; padding: 0; }
#messages { max-width: 780px; margin: 0 auto; padding: 24px 24px 8px; }

.welcome-msg {
  text-align: center; padding: 48px 24px;
  color: var(--text2);
}
.welcome-logo {
  font-family: var(--mono); font-size: 40px; font-weight: 500;
  color: var(--accent2); letter-spacing: 8px; margin-bottom: 16px;
  opacity: 0.6;
}
.welcome-msg p { font-size: 15px; line-height: 1.7; max-width: 480px; margin: 0 auto; }

/* Messages */
.message { margin-bottom: 24px; animation: fadeIn 0.3s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.message.user { display: flex; justify-content: flex-end; }
.message.assistant { display: flex; justify-content: flex-start; }

.msg-bubble {
  max-width: 75%;
  padding: 14px 18px;
  border-radius: 12px;
  font-size: 14px; line-height: 1.75;
}

.message.user .msg-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.message.assistant .msg-bubble {
  background: var(--bg2);
  border: 1px solid var(--border);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.message.assistant .msg-bubble pre {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px 14px;
  overflow-x: auto; font-family: var(--mono); font-size: 12px;
  margin: 10px 0;
}

.message.assistant .msg-bubble code {
  font-family: var(--mono); font-size: 12px;
  background: var(--bg3); padding: 1px 5px;
  border-radius: 4px;
}

.message.assistant .msg-bubble pre code { background: none; padding: 0; }

.typing-indicator {
  display: flex; gap: 4px; align-items: center;
  padding: 14px 18px;
}
.typing-indicator span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text3); animation: bounce 1.2s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce { 0%,60%,100%{transform:translateY(0)} 30%{transform:translateY(-6px)} }

/* ── INPUT ──────────────────────────────────────────────── */
#input-area {
  padding: 16px 24px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.input-wrap {
  max-width: 780px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 10px;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 12px; padding: 10px 10px 10px 16px;
  transition: border-color 0.2s;
}
.input-wrap:focus-within { border-color: var(--accent); }

#user-input {
  flex: 1; background: none; border: none; outline: none;
  color: var(--text); font-family: var(--sans); font-size: 14px;
  line-height: 1.6; resize: none; max-height: 200px; overflow-y: auto;
}
#user-input::placeholder { color: var(--text3); }

#send-btn {
  width: 36px; height: 36px; min-width: 36px;
  background: var(--accent); border: none; border-radius: 8px;
  color: #fff; cursor: pointer; display: flex;
  align-items: center; justify-content: center;
  transition: opacity 0.2s, transform 0.1s;
}
#send-btn:hover { opacity: 0.85; }
#send-btn:active { transform: scale(0.95); }
#send-btn:disabled { opacity: 0.3; cursor: not-allowed; }

.input-meta {
  max-width: 780px; margin: 6px auto 0;
  text-align: right;
}
#token-info { font-size: 11px; color: var(--text3); font-family: var(--mono); }

/* ── STATUS PANEL ───────────────────────────────────────── */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
}

.status-panel {
  width: 600px; max-width: 90vw; max-height: 80vh;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 16px; overflow: hidden;
  display: flex; flex-direction: column;
}

.status-header {
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.status-header h2 { font-size: 16px; font-family: var(--mono); color: var(--accent2); font-weight: 500; }
.status-header button {
  background: none; border: none; color: var(--text2);
  font-size: 16px; cursor: pointer; padding: 4px 8px;
}

#status-content { padding: 24px; overflow-y: auto; }

.stat-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px 16px;
  text-align: center;
}
.stat-card .num {
  font-family: var(--mono); font-size: 28px; font-weight: 500;
  color: var(--accent2);
}
.stat-card .lbl { font-size: 11px; color: var(--text3); margin-top: 4px; }

.status-section { margin-bottom: 20px; }
.status-section h3 {
  font-size: 11px; font-family: var(--mono); color: var(--text3);
  letter-spacing: 1px; text-transform: uppercase;
  margin-bottom: 10px; padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.status-item {
  font-size: 13px; color: var(--text2); line-height: 1.6;
  padding: 6px 0; border-bottom: 1px solid rgba(255,255,255,0.03);
}
.status-item .ts {
  font-family: var(--mono); font-size: 10px; color: var(--text3); display: block;
}

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

/* markdown-ish formatting in assistant bubbles */
.message.assistant .msg-bubble strong { color: var(--accent2); font-weight: 500; }
.message.assistant .msg-bubble h1,
.message.assistant .msg-bubble h2,
.message.assistant .msg-bubble h3 {
  color: var(--accent2); font-weight: 500; margin: 12px 0 6px;
}
.message.assistant .msg-bubble ul,
.message.assistant .msg-bubble ol { padding-left: 20px; margin: 8px 0; }
.message.assistant .msg-bubble li { margin-bottom: 4px; }
.message.assistant .msg-bubble p { margin-bottom: 8px; }
.message.assistant .msg-bubble p:last-child { margin-bottom: 0; }
