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

:root {
  --bg:          #0a0a0f;
  --surface:     #13131a;
  --surface2:    #1c1c28;
  --border:      #ffffff0f;
  --accent:      #7C3AED;
  --accent2:     #3B82F6;
  --user-bubble: linear-gradient(135deg, #7C3AED, #3B82F6);
  --ai-bubble:   #1c1c28;
  --text:        #f0f0f5;
  --text-muted:  #6b7280;
  --text-dim:    #9ca3af;
  --danger:      #ef4444;
  --radius:      18px;
  --radius-sm:   10px;
  --shadow:      0 4px 24px #0008;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ───────────────────────────────────────────────── */
#app { display: flex; height: 100dvh; overflow: hidden; }

/* Sidebar */
#sidebar {
  width: 300px;
  min-width: 300px;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  transition: transform .25s ease;
}

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

#sidebar-header h1 {
  font-size: 22px;
  font-weight: 700;
  background: var(--user-bubble);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

#btn-new-conv {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--accent);
  border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; color: #fff;
  transition: transform .15s, opacity .15s;
}
#btn-new-conv:hover { transform: scale(1.1); }

#conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}
#conv-list::-webkit-scrollbar { width: 4px; }
#conv-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.conv-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  border-radius: var(--radius-sm);
  margin: 2px 8px;
  transition: background .15s;
  position: relative;
}
.conv-item:hover { background: var(--surface2); }
.conv-item.active { background: var(--surface2); }
.conv-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 50%;
  transform: translateY(-50%);
  width: 3px; height: 60%;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
}

.conv-avatar {
  width: 42px; height: 42px;
  border-radius: 50%;
  background: var(--user-bubble);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.conv-info { flex: 1; min-width: 0; }
.conv-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-preview { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }

.conv-delete {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  padding: 4px;
  transition: opacity .15s, color .15s;
}
.conv-item:hover .conv-delete { opacity: 1; }
.conv-delete:hover { color: var(--danger); }

/* Chat area */
#chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

#chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
}

#btn-back {
  display: none;
  background: none; border: none;
  color: var(--text); font-size: 22px;
  cursor: pointer; padding: 4px 8px 4px 0;
}

#chat-conv-name {
  font-weight: 700;
  font-size: 17px;
  flex: 1;
}

#chat-status {
  font-size: 12px;
  color: var(--text-muted);
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}
#messages::-webkit-scrollbar { width: 4px; }
#messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Message bubbles ──────────────────────────────────────── */
.msg-group { display: flex; flex-direction: column; gap: 2px; margin-bottom: 8px; }
.msg-group.user { align-items: flex-end; }
.msg-group.assistant { align-items: flex-start; }

.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 82%;
}
.msg-group.user .msg-row { flex-direction: row-reverse; }

.msg-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--user-bubble);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px;
  flex-shrink: 0;
  align-self: flex-end;
}
.msg-group.user .msg-avatar { background: linear-gradient(135deg, #3B82F6, #7C3AED); }

.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  position: relative;
  word-break: break-word;
  cursor: pointer;
  transition: opacity .1s;
  animation: bubbleIn .2s ease;
  user-select: text;
  -webkit-user-select: text;
}
.bubble:active { opacity: .85; }

.msg-group.user .bubble {
  background: var(--user-bubble);
  border-bottom-right-radius: 4px;
  color: #fff;
}
.msg-group.assistant .bubble {
  background: var(--ai-bubble);
  border-bottom-left-radius: 4px;
  color: var(--text);
  border: 1px solid var(--border);
}

/* Consecutive bubbles */
.msg-group.user .bubble + .bubble { border-radius: var(--radius); border-top-right-radius: 6px; border-bottom-right-radius: var(--radius); }
.msg-group.assistant .bubble + .bubble { border-radius: var(--radius); border-top-left-radius: 6px; border-bottom-left-radius: var(--radius); }

.bubble-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
  padding: 0 4px;
}

/* Reply preview inside bubble */
.reply-preview {
  background: #ffffff12;
  border-left: 3px solid #ffffff60;
  border-radius: 6px;
  padding: 6px 10px;
  margin-bottom: 8px;
  font-size: 12px;
  color: #ffffffaa;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.msg-group.assistant .reply-preview {
  background: #ffffff08;
  border-left-color: var(--accent);
  color: var(--text-muted);
}

/* Markdown in bubbles */
.bubble p { margin-bottom: 6px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble code {
  background: #ffffff18;
  border-radius: 4px;
  padding: 1px 5px;
  font-family: "SF Mono", monospace;
  font-size: 13px;
}
.bubble pre {
  background: #00000040;
  border-radius: 8px;
  padding: 10px;
  overflow-x: auto;
  margin: 8px 0;
}
.bubble pre code { background: none; padding: 0; }
.bubble strong { font-weight: 700; }
.bubble em { font-style: italic; }
.bubble ul, .bubble ol { padding-left: 18px; margin: 4px 0; }
.bubble li { margin: 2px 0; }

/* ── Typing indicator ─────────────────────────────────────── */
#typing-indicator {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 4px 12px;
  margin-bottom: 4px;
}
#typing-indicator.visible { display: flex; animation: bubbleIn .2s ease; }

.typing-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--user-bubble);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px;
}
.typing-bubble {
  background: var(--ai-bubble);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  border-bottom-left-radius: 4px;
  padding: 12px 16px;
  display: flex;
  gap: 5px;
  align-items: center;
}
.dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite;
}
.dot:nth-child(2) { animation-delay: .2s; }
.dot:nth-child(3) { animation-delay: .4s; }

/* ── Audio player ─────────────────────────────────────────── */
.audio-player {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 200px;
}
.audio-play-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: #ffffff20;
  border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; color: #fff;
  transition: background .15s;
  flex-shrink: 0;
}
.audio-play-btn:hover { background: #ffffff35; }
.msg-group.user .audio-play-btn { background: #ffffff25; }

.waveform {
  flex: 1;
  height: 28px;
  display: flex;
  align-items: center;
  gap: 2px;
  overflow: hidden;
}
.waveform-bar {
  width: 3px;
  border-radius: 2px;
  background: #ffffff50;
  transition: height .1s;
}
.waveform-bar.played { background: #ffffffcc; }

.audio-duration { font-size: 12px; color: #ffffffaa; min-width: 32px; }

.audio-speed-btn {
  background: #ffffff15;
  border: 1px solid #ffffff25;
  border-radius: 12px;
  padding: 2px 8px;
  font-size: 11px;
  color: #ffffffcc;
  cursor: pointer;
  transition: background .15s;
  white-space: nowrap;
}
.audio-speed-btn:hover { background: #ffffff25; }

/* ── Transfer modal ───────────────────────────────────────── */
/* ── Copy modal ───────────────────────────────────────────── */
#copy-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: #00000070;
  backdrop-filter: blur(4px);
  align-items: flex-end;
  justify-content: center;
}
#copy-backdrop.visible { display: flex; }
#copy-modal {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
  animation: slideUp .2s ease;
  padding-bottom: env(safe-area-inset-bottom);
}
#copy-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  font-size: 16px;
}
#copy-close {
  background: none; border: none;
  color: var(--text-muted); font-size: 18px;
  cursor: pointer; padding: 4px;
}
#copy-textarea {
  margin: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  padding: 12px;
  min-height: 120px;
  max-height: 40dvh;
  resize: none;
  -webkit-user-select: text;
  user-select: text;
}
#copy-actions {
  display: flex;
  justify-content: flex-end;
  padding: 8px 16px 16px;
}
#copy-select-all {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 20px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

#transfer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: #00000070;
  backdrop-filter: blur(4px);
  align-items: flex-end;
  justify-content: center;
  animation: fadeIn .15s ease;
}
#transfer-backdrop.visible { display: flex; }

#transfer-modal {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-width: 480px;
  max-height: 70dvh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
  animation: slideUp .2s ease;
}

#transfer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  font-size: 16px;
}
#transfer-close {
  background: none; border: none;
  color: var(--text-muted); font-size: 18px;
  cursor: pointer; padding: 4px;
}
#transfer-close:hover { color: var(--text); }

#transfer-preview {
  margin: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 13px;
  color: var(--text-dim);
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

#transfer-conv-list {
  overflow-y: auto;
  padding: 8px;
  flex: 1;
}
#transfer-conv-list::-webkit-scrollbar { width: 4px; }
#transfer-conv-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.transfer-conv-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background .15s;
  font-size: 15px;
  font-weight: 500;
}
.transfer-conv-item:hover { background: #ffffff0a; }
.transfer-conv-item .conv-avatar { width: 36px; height: 36px; font-size: 16px; }

/* New conversation modal */
#new-conv-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: #00000070;
  backdrop-filter: blur(4px);
  align-items: center;
  justify-content: center;
  animation: fadeIn .15s ease;
}
#new-conv-backdrop.visible { display: flex; }

#new-conv-modal {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: calc(100% - 40px);
  max-width: 360px;
  padding: 24px 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: var(--shadow);
  animation: slideUp .2s ease;
}

#new-conv-header {
  font-weight: 700;
  font-size: 17px;
  color: var(--text);
}

#new-conv-input {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 15px;
  padding: 10px 14px;
  outline: none;
  width: 100%;
}
#new-conv-input:focus { border-color: var(--accent); }

#new-conv-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}
#new-conv-cancel {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  padding: 9px 18px;
  font-size: 14px;
  cursor: pointer;
}
#new-conv-cancel:hover { background: #ffffff0a; }
#new-conv-ok {
  background: var(--accent);
  border: none;
  border-radius: var(--radius-sm);
  color: #fff;
  padding: 9px 22px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
#new-conv-ok:hover { filter: brightness(1.1); }

/* Toast de confirmation */
#toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 10px 20px;
  font-size: 14px;
  color: var(--text);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 3000;
  white-space: nowrap;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Context menu ─────────────────────────────────────────── */
#context-menu {
  position: fixed;
  inset: 0;
  z-index: 1500;
  background: #00000060;
  backdrop-filter: blur(2px);
  display: none;
  align-items: flex-end;
  justify-content: center;
}
#context-menu.visible { display: flex; animation: fadeIn .15s ease; }
#context-menu-sheet {
  background: var(--surface2);
  border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  width: 100%;
  max-width: 480px;
  overflow: hidden;
  animation: slideUp .2s ease;
  padding-bottom: env(safe-area-inset-bottom);
}
#ctx-preview {
  padding: 12px 16px;
  font-size: 13px;
  color: var(--text-dim);
  border-bottom: 1px solid var(--border);
  max-height: 80px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  background: var(--surface);
}
#ctx-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: var(--border);
}
.ctx-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 14px 8px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text);
  background: var(--surface2);
  border: none;
  transition: background .1s;
}
.ctx-item:active { background: #ffffff10; }
.ctx-item .ctx-icon { font-size: 22px; }

/* ── Reply bar (above input) ──────────────────────────────── */
#reply-bar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  animation: slideUp .15s ease;
}
#reply-bar.visible { display: flex; }

#reply-bar-content {
  flex: 1;
  font-size: 13px;
  color: var(--text-dim);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
#reply-bar-content strong { color: var(--accent); }

#reply-bar-close {
  background: none; border: none;
  color: var(--text-muted); font-size: 18px;
  cursor: pointer; padding: 4px;
}

/* ── Input area ───────────────────────────────────────────── */
#input-area {
  padding: 10px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#input-wrap {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 24px;
  display: flex;
  align-items: flex-end;
  padding: 10px 14px;
  gap: 8px;
  transition: border-color .2s;
}
#input-wrap:focus-within { border-color: var(--accent); }

#input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: var(--text);
  font-size: 15px;
  resize: none;
  max-height: 120px;
  line-height: 1.5;
  font-family: inherit;
}
#input::placeholder { color: var(--text-muted); }

#btn-mic {
  background: none; border: none;
  color: var(--text-muted); font-size: 20px;
  cursor: pointer; padding: 2px;
  transition: color .15s, transform .15s;
  flex-shrink: 0;
}
#btn-mic:hover { color: var(--text); }
#btn-mic.recording {
  color: var(--danger);
  animation: pulse 1s infinite;
}

#btn-send {
  width: 42px; height: 42px;
  border-radius: 50%;
  background: var(--accent);
  border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  transition: transform .15s, opacity .15s, background .15s;
  flex-shrink: 0;
}
#btn-send:hover { transform: scale(1.08); }
#btn-send:disabled { opacity: .4; cursor: default; transform: none; }

/* ── Empty states ─────────────────────────────────────────── */
#empty-chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-muted);
}
#empty-chat .empty-icon { font-size: 56px; opacity: .4; }
#empty-chat p { font-size: 15px; }

#empty-sidebar {
  padding: 20px 16px;
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
}

/* ── Animations ───────────────────────────────────────────── */
@keyframes bubbleIn {
  from { opacity: 0; transform: scale(.94) translateY(4px); }
  to   { opacity: 1; transform: scale(1)  translateY(0); }
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes slideUp {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%           { transform: translateY(-6px); }
}
@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.2); }
}
@keyframes blink {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0; }
}
.stream-cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--text-muted);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 1s step-start infinite;
}

/* ── Mobile ───────────────────────────────────────────────── */
@media (max-width: 640px) {
  #sidebar {
    position: fixed;
    left: 0; top: 0;
    height: 100dvh;
    z-index: 100;
    width: 100%;
    transform: translateX(0);
  }
  #sidebar.hidden { transform: translateX(-100%); }

  #btn-back { display: block; }

  #chat { width: 100%; }
}
