:root {
  --bg: #ffffff;
  --bg-alt: #f3f4f6;
  --sidebar-bg: #f9fafb;
  --text: #1f2328;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --bubble-user: #2563eb;
  --bubble-user-text: #ffffff;
  --bubble-assistant: #f3f4f6;
  --bubble-assistant-text: #1f2328;
}

:root[data-theme="dark"] {
  --bg: #111318;
  --bg-alt: #1b1e26;
  --sidebar-bg: #15171d;
  --text: #e8eaed;
  --text-muted: #9aa0a6;
  --border: #2a2d36;
  --accent: #3b82f6;
  --accent-text: #0b0d10;
  --danger: #f87171;
  --bubble-user: #3b82f6;
  --bubble-user-text: #0b0d10;
  --bubble-assistant: #1b1e26;
  --bubble-assistant-text: #e8eaed;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button { font-family: inherit; cursor: pointer; }
textarea, input, select { font-family: inherit; }

.hidden { display: none !important; }

.btn {
  padding: 0.5rem 1rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-alt);
  color: var(--text);
}
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-block { width: 100%; }
.btn-link { background: none; border: none; color: var(--accent); text-decoration: underline; padding: 0.25rem 0; }
.btn-icon { background: none; border: none; font-size: 1.1rem; padding: 0.25rem 0.5rem; color: var(--text); }

.app { display: flex; height: 100vh; overflow: hidden; }

.sidebar {
  width: 280px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 0.75rem;
  gap: 0.5rem;
  flex-shrink: 0;
  transition: transform 0.2s ease;
}

.sidebar-header { display: flex; gap: 0.5rem; align-items: center; }
.sidebar-close { display: none; }

.search-input, #setting-system-prompt, #setting-max-tokens, .inline-form input {
  width: 100%;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.conversation-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conversation-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.6rem;
  border-radius: 8px;
  cursor: pointer;
  gap: 0.4rem;
}
.conversation-item:hover { background: var(--bg-alt); }
.conversation-item.active { background: var(--accent); color: var(--accent-text); }
.conversation-item-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.9rem; }
.conversation-item-actions { display: flex; gap: 0.2rem; }
.conversation-item-actions button { background: none; border: none; color: inherit; opacity: 0.7; }

.sidebar-footer { border-top: 1px solid var(--border); padding-top: 0.5rem; display: flex; flex-direction: column; gap: 0.4rem; }
.user-email { font-size: 0.8rem; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-footer-actions { display: flex; gap: 0.3rem; }

.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.chat-topbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
}
.chat-topbar h1 { font-size: 1rem; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-open { display: none; }

.status-banner {
  padding: 0.5rem 1rem;
  background: var(--bg-alt);
  color: var(--text-muted);
  font-size: 0.85rem;
  border-bottom: 1px solid var(--border);
}
.status-banner.error { color: var(--danger); }

.messages { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 0.9rem; }

.message { max-width: 80%; display: flex; flex-direction: column; gap: 0.3rem; }
.message.user { align-self: flex-end; align-items: flex-end; }
.message.assistant { align-self: flex-start; align-items: flex-start; }

.bubble { padding: 0.6rem 0.9rem; border-radius: 12px; line-height: 1.5; word-wrap: break-word; }
.message.user .bubble { background: var(--bubble-user); color: var(--bubble-user-text); }
.message.assistant .bubble { background: var(--bubble-assistant); color: var(--bubble-assistant-text); }

.bubble pre { overflow-x: auto; border-radius: 8px; padding: 0.6rem; }
.bubble code { font-family: "Cascadia Code", Consolas, monospace; font-size: 0.85rem; }
.bubble table { border-collapse: collapse; }
.bubble table, .bubble th, .bubble td { border: 1px solid var(--border); padding: 0.3rem 0.5rem; }

.code-block-wrapper { position: relative; }
.copy-code-btn {
  position: absolute; top: 0.4rem; right: 0.4rem;
  font-size: 0.75rem; padding: 0.2rem 0.5rem;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px; color: var(--text);
}

.message-meta { font-size: 0.72rem; color: var(--text-muted); }
.message-actions { display: flex; gap: 0.5rem; }
.message-actions button { background: none; border: none; font-size: 0.75rem; color: var(--text-muted); }

.composer { border-top: 1px solid var(--border); padding: 0.75rem 1rem; }
.composer-row { display: flex; gap: 0.5rem; align-items: flex-end; }
#message-input {
  flex: 1;
  resize: none;
  max-height: 200px;
  padding: 0.6rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-alt);
  color: var(--text);
}
.composer-footer { display: flex; justify-content: space-between; align-items: center; margin-top: 0.4rem; font-size: 0.78rem; color: var(--text-muted); }
.composer-actions { display: flex; gap: 0.8rem; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal-content {
  background: var(--bg); border-radius: 12px; padding: 1.2rem; width: min(480px, 92vw);
  display: flex; flex-direction: column; gap: 0.5rem; max-height: 90vh; overflow-y: auto;
}
.modal-content label { font-size: 0.85rem; color: var(--text-muted); margin-top: 0.4rem; }
.modal-content select, .modal-content textarea, .modal-content input {
  padding: 0.5rem; border-radius: 8px; border: 1px solid var(--border); background: var(--bg-alt); color: var(--text);
}
.modal-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 0.6rem; }

.auth-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 1rem; }
.auth-card { width: min(380px, 100%); border: 1px solid var(--border); border-radius: 12px; padding: 1.5rem; background: var(--bg-alt); }
.auth-card h1 { margin: 0; }
.auth-subtitle { color: var(--text-muted); margin-top: 0.1rem; }
.auth-tabs { display: flex; gap: 0.4rem; margin: 1rem 0; }
.auth-tab { flex: 1; padding: 0.5rem; border: 1px solid var(--border); background: var(--bg); color: var(--text); border-radius: 8px; }
.auth-tab.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.auth-form { display: flex; flex-direction: column; gap: 0.4rem; }
.auth-form label { font-size: 0.85rem; color: var(--text-muted); margin-top: 0.4rem; }
.auth-form input { padding: 0.55rem; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text); }
.auth-form button { margin-top: 0.8rem; }
.auth-error { color: var(--danger); margin-top: 0.8rem; font-size: 0.85rem; }

.admin-page { max-width: 960px; margin: 0 auto; padding: 1rem; }
.admin-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.admin-section { margin-bottom: 2rem; }
.status-pill { display: inline-block; padding: 0.4rem 0.8rem; border-radius: 20px; background: var(--bg-alt); }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 0.75rem; }
.stat-tile { border: 1px solid var(--border); border-radius: 10px; padding: 0.75rem; text-align: center; }
.stat-tile .stat-value { font-size: 1.4rem; font-weight: 600; }
.stat-tile .stat-label { font-size: 0.78rem; color: var(--text-muted); }
.admin-table { width: 100%; border-collapse: collapse; margin-top: 0.5rem; font-size: 0.85rem; }
.admin-table th, .admin-table td { border-bottom: 1px solid var(--border); padding: 0.5rem; text-align: left; }
.inline-form { display: flex; gap: 0.5rem; flex-wrap: wrap; margin: 0.75rem 0; }
.inline-form input { flex: 1; min-width: 120px; }
.new-key-display { background: var(--bg-alt); border: 1px solid var(--border); border-radius: 8px; padding: 0.6rem; font-family: monospace; word-break: break-all; }
.usage-log { margin-top: 0.75rem; max-height: 300px; overflow-y: auto; font-size: 0.8rem; border: 1px solid var(--border); border-radius: 8px; padding: 0.5rem; }

@media (max-width: 800px) {
  .sidebar {
    position: fixed; z-index: 40; top: 0; left: 0; bottom: 0;
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-close { display: inline-block; margin-left: auto; }
  .sidebar-open { display: inline-block; }
  .message { max-width: 94%; }
}
