:root {
  --bg: #2b2a26;
  --bg-sidebar: #1a1915;
  --bg-surface: #363530;
  --bg-hover: #3d3c37;
  --bg-user-msg: #3a3935;
  --bg-input: #35342f;
  --text: #e8e4de;
  --text-secondary: #9e9a91;
  --text-muted: #6b6860;
  --accent: #d97757;
  --accent-hover: #e08a6d;
  --border: #3a3835;
  --code-bg: #1e1d1a;
  --danger: #d9534f;
  --radius: 16px;
  --radius-sm: 10px;
  --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-chat: 'Source Serif 4', Georgia, 'Times New Roman', serif;
}

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

html { height: 100%; -webkit-text-size-adjust: 100%; }

body {
  height: 100%;
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.screen { display: none; width: 100%; height: 100%; }
.screen.active { display: flex; }
.hidden { display: none !important; }

/* ── Auth ── */
#auth-screen { align-items: center; justify-content: center; background: var(--bg-sidebar); padding: 20px; }

.auth-container {
  width: 100%; max-width: 360px; padding: 36px 28px;
  background: var(--bg); border-radius: var(--radius); border: 1px solid var(--border);
}

.auth-logo { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 32px; }
.auth-logo h1 { font-size: 26px; font-weight: 600; }

.auth-tabs { display: flex; margin-bottom: 20px; border-bottom: 1px solid var(--border); }

.auth-tab {
  flex: 1; padding: 10px; background: none; border: none;
  color: var(--text-secondary); font-size: 14px; font-weight: 500;
  cursor: pointer; border-bottom: 2px solid transparent; transition: all 0.2s; font-family: var(--font-ui);
}
.auth-tab.active { color: var(--text); border-bottom-color: var(--accent); }

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

.auth-form input {
  padding: 14px 16px; background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text); font-size: 16px;
  font-family: var(--font-ui); outline: none; transition: border-color 0.2s; -webkit-appearance: none;
}
.auth-form input:focus { border-color: var(--accent); }

.btn-primary {
  padding: 14px; background: var(--accent); color: #fff; border: none;
  border-radius: var(--radius-sm); font-size: 15px; font-weight: 500;
  cursor: pointer; font-family: var(--font-ui); transition: background 0.2s; -webkit-tap-highlight-color: transparent;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary.btn-sm { padding: 8px 16px; font-size: 13px; border-radius: 8px; }

.btn-danger {
  padding: 8px 16px; background: none; color: var(--danger); border: 1px solid var(--danger);
  border-radius: 8px; font-size: 13px; cursor: pointer; font-family: var(--font-ui);
  margin-top: 12px; transition: all 0.2s; -webkit-tap-highlight-color: transparent;
}
.btn-danger:hover { background: var(--danger); color: #fff; }

.error-msg { color: #ef6b6b; font-size: 13px; margin-top: 8px; text-align: center; min-height: 18px; }

/* ── Chat Layout ── */
#chat-screen { display: flex; height: 100%; position: relative; }
#chat-screen.active { display: flex; }

/* ── Sidebar ── */
#sidebar-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.55); z-index: 90; -webkit-tap-highlight-color: transparent; }

#sidebar {
  position: fixed; top: 0; left: 0; bottom: 0; width: 280px;
  background: var(--bg-sidebar); z-index: 100; display: flex; flex-direction: column;
  transform: translateX(-100%); transition: transform 0.25s ease; overflow: hidden;
}
#sidebar.open { display: flex !important; transform: translateX(0); }

.sidebar-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 12px; }
.sidebar-brand { font-size: 18px; font-weight: 600; }

#sidebar-close {
  background: none; border: none; color: var(--text-secondary); padding: 6px;
  cursor: pointer; border-radius: 8px; display: flex; -webkit-tap-highlight-color: transparent;
}

#new-chat-btn {
  display: flex; align-items: center; gap: 10px; margin: 0 12px 4px; padding: 11px 14px;
  background: var(--bg-surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  color: var(--text); font-size: 14px; font-family: var(--font-ui); cursor: pointer;
  -webkit-tap-highlight-color: transparent; transition: background 0.15s;
}
#new-chat-btn:active { background: var(--bg-hover); }

.sidebar-actions { padding: 4px 12px 0; display: flex; flex-direction: column; gap: 1px; }

.sidebar-action-btn {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 14px;
  background: none; border: none; border-radius: 8px; color: var(--text-secondary);
  font-size: 13px; font-family: var(--font-ui); cursor: pointer; text-align: left;
  -webkit-tap-highlight-color: transparent; transition: background 0.15s;
}
.sidebar-action-btn:active { background: var(--bg-surface); }

.sidebar-search {
  padding: 8px 12px 4px;
}
.sidebar-search input {
  width: 100%; padding: 8px 12px; background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 13px; font-family: var(--font-ui);
  outline: none; -webkit-appearance: none;
}
.sidebar-search input:focus { border-color: var(--text-muted); }
.sidebar-search input::placeholder { color: var(--text-muted); }

#conversation-list { flex: 1; overflow-y: auto; padding: 0 8px; -webkit-overflow-scrolling: touch; }

.conv-item {
  display: flex; align-items: center; padding: 11px 12px; border-radius: 8px;
  cursor: pointer; font-size: 14px; color: var(--text-secondary); transition: background 0.15s;
  -webkit-tap-highlight-color: transparent; margin-bottom: 1px;
}
.conv-item:active, .conv-item.active { background: var(--bg-surface); color: var(--text); }

.conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.conv-bookmark {
  display: none; background: none; border: none; color: var(--text-muted);
  font-size: 14px; padding: 2px 4px; cursor: pointer; border-radius: 4px; line-height: 1;
  -webkit-tap-highlight-color: transparent;
}
.conv-bookmark.active { display: block; color: var(--accent); }
.conv-item.active .conv-bookmark, .conv-item:hover .conv-bookmark { display: block; }

.conv-delete {
  display: none; background: none; border: none; color: var(--text-muted);
  font-size: 18px; padding: 2px 6px; cursor: pointer; border-radius: 4px; line-height: 1;
  -webkit-tap-highlight-color: transparent;
}
.conv-item.active .conv-delete, .conv-item:hover .conv-delete { display: block; }

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

.user-info { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--text-secondary); }

.user-avatar {
  width: 30px; height: 30px; border-radius: 50%; background: var(--accent);
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600;
}

#logout-btn {
  background: none; border: none; color: var(--text-muted); padding: 8px;
  cursor: pointer; border-radius: 8px; display: flex; -webkit-tap-highlight-color: transparent;
}

/* ── Main Chat ── */
#chat-main { flex: 1; display: flex; flex-direction: column; height: 100%; min-width: 0; }

#chat-header {
  height: 48px; display: flex; align-items: center; gap: 8px; padding: 0 12px;
  flex-shrink: 0; border-bottom: 1px solid var(--border);
}

#sidebar-toggle {
  background: none; border: none; color: var(--text-secondary); padding: 8px;
  cursor: pointer; border-radius: 8px; display: flex; -webkit-tap-highlight-color: transparent;
}

#chat-title {
  flex: 1; font-size: 14px; font-weight: 500; color: var(--text-secondary);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

#chat-badges { display: flex; align-items: center; gap: 6px; }

.badge {
  display: flex; align-items: center; padding: 4px 6px; border-radius: 6px;
  background: var(--bg-surface); border: none; color: var(--text-secondary);
  cursor: pointer; font-size: 11px; -webkit-tap-highlight-color: transparent;
}
.badge.active { color: var(--accent); background: rgba(217,119,87,0.15); }
.badge.off { opacity: 0.4; }

#badge-incognito { background: rgba(217,119,87,0.15); color: var(--accent); cursor: default; }

/* ── Messages ── */
#messages-container { flex: 1; overflow-y: auto; display: none; -webkit-overflow-scrolling: touch; }
#messages-container.has-messages { display: block; }

#messages { max-width: 720px; margin: 0 auto; padding: 16px 16px 120px; }

.message { margin-bottom: 20px; animation: fadeIn 0.15s ease; }

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

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

.message.user .message-content {
  background: var(--bg-user-msg); padding: 12px 16px; border-radius: var(--radius);
  max-width: 85%; font-family: var(--font-chat); font-size: 16px; line-height: 1.6;
}

.message.assistant .message-content {
  font-family: var(--font-chat); font-size: 16px; line-height: 1.7; color: var(--text); padding: 0 2px;
}

.message-content p { margin-bottom: 12px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content ul, .message-content ol { margin: 8px 0; padding-left: 22px; }
.message-content li { margin-bottom: 4px; }
.message-content a { color: var(--accent); text-decoration: none; }
.message-content a:hover { text-decoration: underline; }
.message-content strong { font-weight: 600; }
.message-content em { font-style: italic; }
.message-content h1, .message-content h2, .message-content h3 { font-family: var(--font-ui); margin: 18px 0 8px; font-weight: 600; }
.message-content h1 { font-size: 1.3em; }
.message-content h2 { font-size: 1.15em; }
.message-content h3 { font-size: 1.05em; }
.message-content blockquote { border-left: 3px solid var(--accent); padding-left: 14px; color: var(--text-secondary); margin: 12px 0; }
.message-content hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

.message-content code:not(pre code) {
  background: var(--code-bg); padding: 2px 6px; border-radius: 4px; font-size: 0.88em;
  font-family: 'SF Mono', 'Fira Code', Consolas, monospace;
}

.message-content pre { background: var(--code-bg); border-radius: var(--radius-sm); margin: 12px 0; overflow: hidden; }

.code-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; background: #141310; font-size: 12px; color: var(--text-muted); font-family: var(--font-ui);
}

.copy-btn {
  background: none; border: 1px solid var(--border); color: var(--text-secondary);
  padding: 3px 10px; border-radius: 4px; cursor: pointer; font-size: 12px; font-family: var(--font-ui);
  -webkit-tap-highlight-color: transparent;
}

.message-content pre code {
  display: block; padding: 12px; overflow-x: auto; font-size: 13px; line-height: 1.5;
  font-family: 'SF Mono', 'Fira Code', Consolas, monospace; -webkit-overflow-scrolling: touch;
}

.message-content table { border-collapse: collapse; margin: 12px 0; width: 100%; font-size: 14px; overflow-x: auto; display: block; }
.message-content th, .message-content td { border: 1px solid var(--border); padding: 8px 10px; text-align: left; }
.message-content th { background: var(--bg-surface); font-weight: 600; }

/* ── Message Images ── */
.message-images {
  display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px;
}
.message-images:last-child { margin-bottom: 0; }
.message-images img {
  max-width: 180px; max-height: 180px; border-radius: 8px;
  object-fit: cover; cursor: pointer; display: block;
}

/* ── Lightbox ── */
#lightbox {
  position: fixed; inset: 0; z-index: 300; background: rgba(0,0,0,0.92);
  display: flex; align-items: center; justify-content: center;
  -webkit-tap-highlight-color: transparent; touch-action: none;
}
#lightbox-close {
  position: absolute; top: 12px; right: 12px; z-index: 301;
  background: none; border: none; color: #fff; padding: 8px;
  cursor: pointer; opacity: 0.7; -webkit-tap-highlight-color: transparent;
}
#lightbox-close:hover { opacity: 1; }
#lightbox-img {
  max-width: 100%; max-height: 100%; object-fit: contain;
  transform-origin: center center; transition: transform 0.1s ease;
  -webkit-user-select: none; user-select: none; pointer-events: none;
}

/* ── Welcome ── */
#welcome { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; padding: 20px; }
#welcome.hidden { display: none; }
.welcome-icon { opacity: 0.7; }
#welcome h2 { font-size: 20px; font-weight: 500; color: var(--text-secondary); font-family: var(--font-chat); }

/* ── Input ── */
#input-area {
  padding: 0 12px 16px; padding-bottom: max(16px, env(safe-area-inset-bottom));
  position: absolute; bottom: 0; left: 0; right: 0;
  background: linear-gradient(transparent, var(--bg) 25%); padding-top: 32px;
}

#image-preview-bar {
  display: flex; gap: 8px; padding: 0 4px 8px; overflow-x: auto; max-width: 720px; margin: 0 auto;
}

.img-preview {
  position: relative; width: 56px; height: 56px; border-radius: 8px; overflow: hidden;
  flex-shrink: 0; border: 1px solid var(--border);
}
.img-preview img { width: 100%; height: 100%; object-fit: cover; }
.img-preview .img-remove {
  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; -webkit-tap-highlight-color: transparent;
}

.input-wrapper {
  max-width: 720px; margin: 0 auto; background: var(--bg-input);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px;
  transition: border-color 0.2s;
}
.input-wrapper:focus-within { border-color: var(--text-muted); }

#message-input {
  width: 100%; background: none; border: none; color: var(--text);
  font-size: 16px; font-family: var(--font-chat); line-height: 1.5;
  resize: none; outline: none; max-height: 150px; overflow-y: auto; -webkit-appearance: none;
}
#message-input::placeholder { color: var(--text-muted); }

.input-controls { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.input-left { display: flex; align-items: center; gap: 8px; }

#image-btn {
  background: none; border: none; color: var(--text-muted); padding: 4px;
  cursor: pointer; display: flex; -webkit-tap-highlight-color: transparent; border-radius: 6px;
}
#image-btn:active { color: var(--text-secondary); }

#model-select {
  padding: 5px 24px 5px 8px; background: transparent; border: 1px solid var(--border);
  border-radius: 6px; color: var(--text-secondary); font-size: 13px; font-family: var(--font-ui);
  cursor: pointer; outline: none; -webkit-appearance: none; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1L5 5L9 1' stroke='%239e9a91' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 8px center;
}
#model-select option { background: var(--bg); }

#send-btn {
  width: 34px; height: 34px; border-radius: 50%; background: var(--accent);
  border: none; color: #fff; cursor: pointer; display: flex; align-items: center;
  justify-content: center; flex-shrink: 0; transition: opacity 0.2s; -webkit-tap-highlight-color: transparent;
}
#send-btn:disabled { opacity: 0.25; }
#send-btn:not(:disabled):active { transform: scale(0.93); }

/* ── Streaming ── */
.streaming-cursor::after {
  content: ''; display: inline-block; width: 7px; height: 15px;
  background: var(--accent); margin-left: 2px; animation: blink 0.7s infinite;
  vertical-align: text-bottom; border-radius: 1px;
}
@keyframes blink { 0%, 50% { opacity: 1; } 51%, 100% { opacity: 0; } }

/* ── Modals ── */
.modal {
  position: fixed; inset: 0; z-index: 200; display: flex;
  align-items: flex-end; justify-content: center;
  background: rgba(0,0,0,0.55); -webkit-tap-highlight-color: transparent;
}

.modal-content {
  width: 100%; max-width: 480px; max-height: 85vh;
  background: var(--bg); border-radius: 16px 16px 0 0; overflow: hidden;
  display: flex; flex-direction: column;
  animation: slideUp 0.25s ease;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 17px; font-weight: 600; }

.modal-close {
  background: none; border: none; color: var(--text-secondary); padding: 4px;
  cursor: pointer; display: flex; -webkit-tap-highlight-color: transparent;
}

.modal-body { padding: 16px 20px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.modal-desc { font-size: 13px; color: var(--text-secondary); margin-bottom: 16px; }

/* Memory items */
.memory-item {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 10px 0; border-bottom: 1px solid var(--border);
}
.memory-item:last-child { border-bottom: none; }

.memory-text { flex: 1; font-size: 14px; color: var(--text); line-height: 1.5; word-break: break-word; }

.memory-actions { display: flex; gap: 4px; flex-shrink: 0; }
.memory-actions button {
  background: none; border: none; color: var(--text-muted); padding: 4px; cursor: pointer;
  font-size: 14px; -webkit-tap-highlight-color: transparent;
}
.memory-actions button:hover { color: var(--text); }

/* Memories toggle in modal */
.memories-toggle-row {
  padding: 12px; margin-bottom: 16px; background: var(--bg-surface);
  border-radius: var(--radius-sm); border: 1px solid var(--border);
}
.toggle-label {
  display: flex; align-items: center; gap: 10px; cursor: pointer;
  font-size: 14px; font-weight: 500; color: var(--text);
}
.toggle-label input[type="checkbox"] {
  width: 18px; height: 18px; accent-color: var(--accent); cursor: pointer;
  flex-shrink: 0;
}
.toggle-hint {
  font-size: 12px; color: var(--text-muted); margin: 6px 0 0 28px; line-height: 1.4;
}

.memory-add { display: flex; gap: 8px; margin-top: 12px; align-items: flex-end; }
.memory-add textarea {
  flex: 1; padding: 10px 12px; background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 14px; font-family: var(--font-ui);
  outline: none; resize: none; -webkit-appearance: none;
}
.memory-add textarea:focus { border-color: var(--accent); }

/* Invite codes */
.invite-code-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 14px;
}

.invite-code {
  font-family: 'SF Mono', Consolas, monospace; font-size: 16px; font-weight: 600;
  color: var(--accent); letter-spacing: 1px;
}

.invite-status { font-size: 12px; color: var(--text-muted); }
.invite-status.used { color: var(--text-muted); text-decoration: line-through; }

/* ── Desktop ── */
@media (min-width: 769px) {
  #sidebar {
    position: relative; transform: none; width: 280px; min-width: 280px;
    border-right: 1px solid var(--border);
  }
  #sidebar.open { transform: none; }
  #sidebar.desktop-hidden { display: none !important; }
  #sidebar-overlay { display: none !important; }
  #sidebar-close { display: none; }
  #messages { padding: 24px 24px 120px; }
  .message.user .message-content { max-width: 70%; }

  .modal { align-items: center; }
  .modal-content { border-radius: 16px; max-height: 70vh; }
}
