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

:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface2: #16213e;
  --accent: #7c4dff;
  --accent2: #448aff;
  --text: #e8eaf6;
  --text2: #9fa8da;
  --user-bubble: #1e3a5f;
  --ai-bubble: #1a1a2e;
  --border: #2a2a4a;
  --danger: #ef5350;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}

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

#app { height: 100%; display: flex; flex-direction: column; }

/* ── Login ── */
.login-wrap {
  flex: 1; display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%; max-width: 360px; background: var(--surface);
  border-radius: 20px; padding: 40px 32px; border: 1px solid var(--border);
}
.login-logo {
  text-align: center; margin-bottom: 32px;
  font-size: 48px;
}
.login-title { text-align: center; font-size: 22px; font-weight: 600; margin-bottom: 8px; }
.login-sub { text-align: center; color: var(--text2); font-size: 14px; margin-bottom: 32px; }
.login-input {
  width: 100%; background: var(--bg); border: 1px solid var(--border);
  color: var(--text); border-radius: 12px; padding: 14px 16px;
  font-size: 16px; outline: none; margin-bottom: 16px;
}
.login-input:focus { border-color: var(--accent); }
.login-btn {
  width: 100%; background: var(--accent); color: #fff;
  border: none; border-radius: 12px; padding: 14px;
  font-size: 16px; font-weight: 600; cursor: pointer;
  transition: opacity 0.2s;
}
.login-btn:active { opacity: 0.8; }
.login-err { color: var(--danger); font-size: 13px; text-align: center; margin-top: 12px; display: none; }

/* ── Chat layout ── */
.chat-wrap { flex: 1; display: flex; flex-direction: column; height: 100%; overflow: hidden; }

.chat-header {
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px; flex-shrink: 0;
}
.chat-header-icon { font-size: 28px; }
.chat-header-title { font-size: 17px; font-weight: 600; flex: 1; }
.chat-header-status { font-size: 12px; color: var(--text2); }
.btn-logout {
  background: none; border: 1px solid var(--border); color: var(--text2);
  border-radius: 8px; padding: 6px 12px; font-size: 13px; cursor: pointer;
}

.chat-messages {
  flex: 1; overflow-y: auto; padding: 16px 16px 8px;
  display: flex; flex-direction: column; gap: 12px;
  -webkit-overflow-scrolling: touch;
}

/* ── Messages ── */
.msg { display: flex; flex-direction: column; max-width: 90%; }
.msg--user { align-self: flex-end; align-items: flex-end; }
.msg--ai { align-self: flex-start; align-items: flex-start; }

.msg-bubble {
  padding: 12px 16px; border-radius: 18px;
  font-size: 15px; line-height: 1.5; white-space: pre-wrap; word-break: break-word;
}
.msg--user .msg-bubble { background: var(--user-bubble); border-bottom-right-radius: 4px; }
.msg--ai   .msg-bubble { background: var(--ai-bubble); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg--ai .msg-bubble.streaming::after { content: '▋'; animation: blink 1s infinite; display: inline; }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0} }

.msg-time { font-size: 11px; color: var(--text2); margin-top: 4px; padding: 0 4px; }

/* Code blocks */
.msg-bubble pre { background: #0d1117; border-radius: 8px; padding: 12px; margin: 8px 0; overflow-x: auto; font-size: 13px; }
.msg-bubble code { font-family: 'Menlo', 'Monaco', monospace; font-size: 13px; }
.msg-bubble p:not(:last-child) { margin-bottom: 8px; }

/* File attachments */
.attach-preview { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.attach-thumb {
  position: relative; width: 80px; height: 80px; border-radius: 10px;
  overflow: hidden; background: var(--surface2); border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.attach-thumb img { width: 100%; height: 100%; object-fit: cover; }
.attach-thumb .attach-name { font-size: 10px; color: var(--text2); text-align: center; padding: 4px; word-break: break-all; }
.attach-thumb .attach-rm {
  position: absolute; top: 2px; right: 2px; width: 18px; height: 18px;
  background: rgba(0,0,0,0.7); border: none; color: #fff; border-radius: 50%;
  font-size: 12px; cursor: pointer; display: flex; align-items: center; justify-content: center;
  line-height: 1;
}

/* ── Input bar ── */
.chat-input-bar {
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 10px 12px calc(var(--safe-bot) + 10px);
  flex-shrink: 0;
}
.input-attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.input-row { display: flex; align-items: flex-end; gap: 8px; }

.btn-attach {
  background: none; border: 1px solid var(--border); color: var(--text2);
  width: 40px; height: 40px; border-radius: 12px; font-size: 20px;
  cursor: pointer; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
}
.btn-attach:active { background: var(--surface2); }

.chat-textarea {
  flex: 1; background: var(--bg); border: 1px solid var(--border);
  color: var(--text); border-radius: 12px; padding: 10px 14px;
  font-size: 15px; line-height: 1.4; resize: none; outline: none;
  max-height: 120px; overflow-y: auto; font-family: inherit;
}
.chat-textarea:focus { border-color: var(--accent); }
.chat-textarea::placeholder { color: var(--text2); }

.btn-send {
  background: var(--accent); border: none; color: #fff;
  width: 40px; height: 40px; border-radius: 12px; font-size: 18px;
  cursor: pointer; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  transition: opacity 0.2s;
}
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-send:not(:disabled):active { opacity: 0.8; }

/* ── Typing indicator ── */
.typing-dots span {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--text2); margin: 0 2px; animation: bounce 1.2s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce { 0%,80%,100%{transform:translateY(0)} 40%{transform:translateY(-6px)} }

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

/* ── Version badge ── */
.ver-badge {
  font-size: 11px; color: var(--text2); font-family: monospace;
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 6px; padding: 2px 6px; flex-shrink: 0;
}

/* ── Slash suggestions ── */
.suggestions {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 12px; margin-bottom: 6px; overflow: hidden;
}
.suggest-item {
  padding: 10px 14px; cursor: pointer; display: flex; align-items: center;
  border-bottom: 1px solid var(--border);
}
.suggest-item:last-child { border-bottom: none; }
.suggest-item:active { background: var(--border); }
.suggest-cmd  { color: var(--accent); font-family: monospace; font-size: 14px; }
.suggest-hint { color: var(--text2); font-size: 13px; }

/* ── History note ── */
.history-note {
  text-align: center; color: var(--text2); font-size: 12px;
  padding: 8px; margin-bottom: 8px;
}

/* ── Thinking block ── */
.thinking-wrap { margin-bottom: 6px; }
.thinking-toggle {
  font-size: 12px; color: var(--text2); cursor: pointer;
  padding: 4px 8px; border-radius: 6px; display: inline-block;
  border: 1px solid var(--border); background: var(--bg);
  user-select: none;
}
.thinking-toggle:active { opacity: 0.7; }
.thinking-body {
  margin-top: 6px; padding: 10px 12px;
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; font-size: 13px; color: var(--text2);
  line-height: 1.5; white-space: pre-wrap; word-break: break-word;
  max-height: 300px; overflow-y: auto;
}
