:root {
  --bg: #0f0f13;
  --bg-elevated: #16161c;
  --bg-elevated-2: #1c1c24;
  --border: #2a2a34;
  --text: #e8e8ec;
  --text-dim: #9797a6;
  --text-faint: #6a6a78;
  --accent: #6366f1;
  --accent-hover: #7678f3;
  --accent-dim: rgba(99, 102, 241, 0.15);
  --danger: #ef4444;
  --success: #22c55e;
  --radius: 10px;
  --sidebar-width: 280px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
}

#app {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

/* ============ AUTH GATE ============ */
.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}
.auth-gate.hidden { display: none; }

.auth-card {
  width: 100%;
  max-width: 340px;
  padding: 28px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 12px 32px rgba(0,0,0,0.5);
}
.auth-card .brand {
  text-align: center;
  margin-bottom: 20px;
}
.auth-subtitle {
  color: var(--text-dim);
  font-size: 13px;
  margin: 0 0 12px 0;
}
.auth-card form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.auth-card input[type="text"] {
  width: 100%;
}
.auth-error {
  color: var(--danger);
  font-size: 12.5px;
  min-height: 1em;
  margin: 10px 0 0 0;
}
.auth-switch {
  font-size: 12.5px;
  color: var(--text-dim);
  margin: 14px 0 0 0;
  text-align: center;
}
.auth-switch a {
  color: var(--accent);
  text-decoration: none;
}
.auth-switch a:hover { text-decoration: underline; }
.auth-generated-username {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  text-align: center;
  word-break: break-all;
  margin: 4px 0 12px 0;
}
.auth-hint {
  font-size: 12px;
  color: var(--text-faint);
  text-align: center;
  margin: 0 0 18px 0;
}

/* ============ SIDEBAR ============ */
#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
}

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

.brand {
  font-size: 16px;
  font-weight: 700;
  margin: 0 0 12px 0;
  letter-spacing: -0.02em;
}

.user-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}
.current-username {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.private-toggle-label {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-dim);
  cursor: pointer;
}
.private-toggle-label input[type="checkbox"] {
  accent-color: var(--accent);
}

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

.sidebar-search input {
  width: 100%;
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conversation-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  margin-bottom: 2px;
  border: 1px solid transparent;
  position: relative;
}

.conversation-item:hover {
  background: var(--bg-elevated-2);
}

.conversation-item.active {
  background: var(--accent-dim);
  border-color: rgba(99, 102, 241, 0.35);
}

.conversation-item-title {
  font-size: 13.5px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
}

.conversation-item-meta {
  font-size: 11.5px;
  color: var(--text-faint);
}

.conversation-empty {
  color: var(--text-faint);
  text-align: center;
  padding: 24px 12px;
  font-size: 13px;
}

/* ============ MAIN ============ */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
  min-width: 0;
}

#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  z-index: 5;
}

.chat-title-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

#chat-title {
  font-size: 15px;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 40vw;
  outline: none;
}

#chat-title[contenteditable="true"] {
  border-bottom: 1px dashed var(--accent);
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 14px;
  padding: 4px 6px;
  border-radius: 6px;
}
.icon-btn:hover {
  background: var(--bg-elevated-2);
  color: var(--text);
}

/* ============ MODEL DROPDOWN ============ */
.model-dropdown {
  position: absolute;
  top: 58px;
  right: 20px;
  width: 360px;
  max-height: 60vh;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 12px 32px rgba(0,0,0,0.5);
  z-index: 20;
  display: flex;
  flex-direction: column;
}
.model-dropdown.hidden { display: none; }

.model-dropdown #model-search {
  margin: 12px;
  width: calc(100% - 24px);
}

.model-options {
  overflow-y: auto;
  flex: 1;
  padding: 0 8px;
}

.model-option {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
}
.model-option:hover { background: var(--bg-elevated-2); }

.model-option input[type="checkbox"] {
  margin-top: 3px;
  accent-color: var(--accent);
}

.model-option-text { min-width: 0; }

.model-option-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.model-option-id {
  font-size: 11px;
  color: var(--text-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.model-dropdown-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-dim);
}

/* ============ MESSAGES ============ */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.empty-state {
  margin: auto;
  max-width: 480px;
  text-align: center;
  color: var(--text-dim);
}
.empty-state h3 { color: var(--text); margin-bottom: 8px; }

.msg-row { display: flex; flex-direction: column; gap: 6px; }
.msg-row.user { align-items: flex-end; }

.msg-bubble.user {
  background: var(--accent);
  color: white;
  padding: 10px 14px;
  border-radius: 14px 14px 2px 14px;
  max-width: 70%;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Grid of model response cards for a single prompt (1..N models) */
.response-group {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 12px;
  width: 100%;
}

.response-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.response-card.errored {
  border-color: rgba(239, 68, 68, 0.4);
}

.response-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  color: var(--text-dim);
}

.response-model-name {
  font-weight: 600;
  color: var(--text);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.response-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
  font-size: 11.5px;
  color: var(--text-faint);
}

.spinner {
  width: 12px;
  height: 12px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

.retry-btn {
  background: transparent;
  border: none;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 4px;
}
.retry-btn:hover:not(:disabled) {
  background: var(--bg-elevated-2);
  color: var(--text);
}
.retry-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.response-card-body {
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--text);
  overflow-wrap: break-word;
}

.response-card-body p { margin: 0 0 10px 0; }
.response-card-body p:last-child { margin-bottom: 0; }
.response-card-body pre {
  background: #0a0a0d;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  font-size: 12.5px;
}
.response-card-body code {
  font-family: "SF Mono", Menlo, Consolas, monospace;
}
.response-card-body :not(pre) > code {
  background: var(--bg-elevated-2);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 12.5px;
}
.response-card-body ul, .response-card-body ol { margin: 0 0 10px 0; padding-left: 20px; }
.response-card-body blockquote {
  border-left: 3px solid var(--border);
  margin: 0 0 10px 0;
  padding-left: 10px;
  color: var(--text-dim);
}
.response-card-body table { border-collapse: collapse; margin-bottom: 10px; }
.response-card-body th, .response-card-body td {
  border: 1px solid var(--border);
  padding: 4px 8px;
  font-size: 12.5px;
}

.response-card.errored .response-card-body {
  color: var(--danger);
  font-size: 13px;
}

.cursor-blink::after {
  content: "▍";
  color: var(--accent);
  animation: blink 1s step-start infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ============ COMPOSER ============ */
#composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

#prompt-input {
  flex: 1;
  resize: none;
  max-height: 180px;
  min-height: 42px;
  padding: 11px 14px;
  font-family: inherit;
}

/* ============ FORM ELEMENTS ============ */
input[type="text"], textarea {
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  outline: none;
}
input[type="text"]:focus, textarea:focus {
  border-color: var(--accent);
}

.btn {
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, opacity 0.15s ease;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn-stop { background: var(--danger); color: white; }
.btn-stop:hover:not(:disabled) { background: #d13939; }

.btn-secondary {
  background: var(--bg-elevated-2);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: #24242e; }

.btn-block { width: 100%; }
.btn-sm { padding: 6px 12px; font-size: 12px; }

/* ============ SCROLLBARS ============ */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #2f2f3a; border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

/* ============ RESPONSIVE ============ */
.sidebar-toggle-btn { display: none; }

#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 30;
}
#sidebar-overlay.open { display: block; }

@media (max-width: 860px) {
  :root { --sidebar-width: 240px; }

  #sidebar {
    position: fixed;
    left: -260px;
    top: 0;
    bottom: 0;
    z-index: 40;
    transition: left 0.2s ease;
    box-shadow: 8px 0 24px rgba(0,0,0,0.4);
  }
  #sidebar.open { left: 0; }

  .sidebar-toggle-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-elevated-2);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 8px;
    width: 34px;
    height: 34px;
    cursor: pointer;
    margin-right: 8px;
  }

  #chat-title { max-width: 34vw; }
  .model-dropdown { width: calc(100vw - 40px); right: 20px; }
  .response-group { grid-template-columns: 1fr; }
  .msg-bubble.user { max-width: 88%; }
}
