:root {
  --brand: #875A7B;
  --brand-dark: #5d3e55;
  --brand-light: #f4ecf1;
  --bg: #fafaf9;
  --surface: #ffffff;
  --border: #e7e5e4;
  --text: #1c1917;
  --text-dim: #78716c;
  --ok: #16a34a;
  --warn: #ea580c;
  --err: #dc2626;
  --user-bubble: #f4ecf1;
  --assistant-bubble: #ffffff;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 12px rgba(0,0,0,.04);
}

* { box-sizing: border-box; }

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

button { font-family: inherit; cursor: pointer; border: 0; background: none; }
.hidden { display: none !important; }

/* ── BRAND ─────────────────────────────────────────── */

.brand, .brand-small { display: flex; align-items: center; gap: 12px; }
.brand h1 { margin: 0; font-size: 1.3rem; font-weight: 700; }
.brand-sub { margin: 0; color: var(--text-dim); font-size: .85rem; }
.brand-sub-xs { color: var(--text-dim); font-size: .7rem; }
.brand-mark {
  width: 40px; height: 40px;
  border-radius: 10px;
  background: var(--brand);
  color: white;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 1.2rem;
}
.brand-small .brand-mark { width: 32px; height: 32px; font-size: 1rem; }

/* ── LOGIN ─────────────────────────────────────────── */

.login-view {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background: linear-gradient(135deg, #faf6f9 0%, #f4ecf1 100%);
}
.login-card {
  background: white;
  border-radius: 16px;
  padding: 32px;
  box-shadow: var(--shadow);
  max-width: 380px;
  width: 100%;
}
.login-card .brand { margin-bottom: 20px; }
.login-help { color: var(--text-dim); margin-bottom: 24px; font-size: .95rem; }
.login-error {
  color: var(--err);
  font-size: .85rem;
  margin-top: 12px;
  padding: 8px 12px;
  background: #fee2e2;
  border-radius: 6px;
}
.dev-mode-box { margin-top: 20px; }
.dev-mode-box hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }
.dev-label { font-size: .75rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 8px; }
.dev-mode-box input {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: .95rem; margin-bottom: 8px;
}

/* ── BUTTONS ───────────────────────────────────────── */

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border-radius: 8px;
  font-weight: 600;
  font-size: .9rem;
  transition: all .15s;
}
.btn-primary {
  background: var(--brand); color: white;
}
.btn-primary:hover { background: var(--brand-dark); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.btn-ghost {
  background: transparent; color: var(--text-dim);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-block { width: 100%; }
.btn-small { padding: 6px 10px; font-size: .8rem; }
.btn-google {
  width: 100%;
  padding: 12px 16px;
  gap: 10px;
  font-size: .95rem;
}

/* ── APP LAYOUT ────────────────────────────────────── */

.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.sidebar-header { padding: 16px; border-bottom: 1px solid var(--border); }
.sidebar-header .brand-small { margin-bottom: 14px; }

.sessions {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}
.session-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
  transition: background .1s;
}
.session-item:hover { background: var(--bg); }
.session-item.active { background: var(--brand-light); }
.session-item .title {
  font-size: .9rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.session-item .preview {
  font-size: .8rem;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
}
.user-chip {
  display: flex; align-items: center; gap: 10px;
  padding: 8px;
  margin-bottom: 8px;
}
.user-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--brand);
  color: white;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .9rem;
}
.user-info { flex: 1; min-width: 0; }
.user-info > div:first-child {
  font-weight: 500; font-size: .9rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.user-role {
  font-size: .75rem;
  color: var(--text-dim);
  text-transform: capitalize;
}

/* ── MAIN ──────────────────────────────────────────── */

.main {
  display: flex; flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

.main-header {
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  display: flex; align-items: center; justify-content: space-between;
}
.main-header h2 {
  margin: 0; font-size: 1rem; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.status-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.status-ok { background: var(--ok); }
.status-err { background: var(--err); }
.status-paused { background: var(--warn); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}
.empty-state {
  max-width: 560px;
  margin: 60px auto 0;
  text-align: center;
}
.empty-state h3 { font-size: 1.4rem; margin-bottom: 8px; }
.empty-state p { color: var(--text-dim); margin-bottom: 24px; }
.suggestions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.suggestion {
  text-align: left;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: .88rem;
  transition: all .15s;
}
.suggestion:hover {
  border-color: var(--brand);
  transform: translateY(-1px);
  box-shadow: var(--shadow);
}

/* Bubbles */
.msg {
  max-width: 720px;
  margin: 0 auto 18px;
  display: flex;
  gap: 12px;
}
.msg-avatar {
  flex-shrink: 0;
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .8rem;
  color: white;
}
.msg-body { flex: 1; min-width: 0; }
.msg-role {
  font-size: .8rem;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 4px;
}
.msg-content {
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg.user .msg-avatar { background: var(--brand); }
.msg.user .msg-content { background: var(--user-bubble); border-color: transparent; }
.msg.assistant .msg-avatar { background: #1c1917; }

.tool-chip {
  display: inline-block;
  font-size: .72rem;
  background: #f3f4f6;
  color: #4b5563;
  padding: 2px 8px;
  border-radius: 6px;
  margin-top: 6px;
  margin-right: 4px;
}

.feedback-bar {
  margin-top: 6px;
  display: flex; gap: 6px;
}
.fb-btn {
  font-size: .8rem;
  padding: 4px 8px;
  border-radius: 6px;
  color: var(--text-dim);
  border: 1px solid var(--border);
  background: var(--surface);
}
.fb-btn:hover { color: var(--text); border-color: var(--text-dim); }
.fb-btn.active { background: var(--brand-light); color: var(--brand); border-color: var(--brand); }

.typing {
  display: inline-flex; gap: 4px;
  align-items: center;
}
.typing span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-dim);
  animation: typing 1.4s infinite;
}
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes typing {
  0%, 60%, 100% { opacity: .3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

.msg-error .msg-content { background: #fee2e2; border-color: #fecaca; color: #991b1b; }

/* Composer */
.composer {
  max-width: 720px;
  margin: 0 auto;
  width: 100%;
  padding: 0 24px 8px;
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
#input {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  font-family: inherit;
  font-size: .95rem;
  line-height: 1.4;
  max-height: 200px;
  background: var(--surface);
}
#input:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-light);
}
.btn-attach {
  width: 44px; height: 44px;
  padding: 0;
  border-radius: 10px;
  flex-shrink: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 1.2rem;
  transition: all .15s;
}
.btn-attach:hover {
  background: var(--brand-light);
  border-color: var(--brand);
}

.attachments-tray {
  max-width: 720px;
  margin: 0 auto;
  padding: 8px 24px 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--brand-light);
  color: var(--brand-dark);
  padding: 4px 10px;
  border-radius: 16px;
  font-size: .82rem;
  max-width: 200px;
}
.attachment-chip .filename {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attachment-chip .chip-remove {
  cursor: pointer;
  color: var(--err);
  font-weight: bold;
  padding: 0 4px;
}
.attachment-chip.uploading { opacity: .6; }
.attachment-chip.error {
  background: #fee2e2; color: var(--err);
}

#send {
  width: 44px; height: 44px;
  padding: 0;
  flex-shrink: 0;
}
.composer-hint {
  max-width: 720px;
  margin: 0 auto;
  padding: 4px 24px 16px;
  display: flex; justify-content: space-between;
  font-size: .72rem; color: var(--text-dim);
}

/* Admin modal */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
  padding: 20px;
}
.modal {
  background: white;
  border-radius: 14px;
  width: 100%; max-width: 720px;
  max-height: 90vh;
  display: flex; flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
}
.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-header h3 { margin: 0; }
.btn-close {
  font-size: 1.3rem; color: var(--text-dim);
  padding: 4px 10px; border-radius: 6px;
}
.btn-close:hover { background: var(--bg); color: var(--text); }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-body hr { border: 0; border-top: 1px solid var(--border); margin: 20px 0; }
.modal-body h4 { margin: 0 0 12px; font-size: .95rem; }

.users-table {
  width: 100%;
  border-collapse: collapse;
}
.users-table th, .users-table td {
  padding: 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: .88rem;
}
.users-table th {
  color: var(--text-dim);
  font-weight: 600;
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .05em;
}
.users-table select {
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: .85rem;
}
.user-row.inactive { opacity: .5; }
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: .72rem;
  font-weight: 600;
}
.badge-ok { background: #dcfce7; color: #166534; }
.badge-off { background: #fee2e2; color: #991b1b; }

.admin-form {
  display: grid;
  grid-template-columns: 2fr 2fr 1fr auto;
  gap: 8px;
}
.admin-form input, .admin-form select {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: .88rem;
}
.admin-msg {
  margin-top: 12px;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: .85rem;
}
.admin-msg.ok { background: #dcfce7; color: #166534; }
.admin-msg.err { background: #fee2e2; color: #991b1b; }

.cli-new-token {
  margin-top: 14px;
  padding: 12px;
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 8px;
}
.kb-entry {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 12px;
  background: var(--surface);
}
.kb-entry.verified { border-left: 4px solid var(--ok); }
.kb-entry.pending { border-left: 4px solid var(--warn); }
.kb-entry h4 { margin: 0 0 6px; font-size: 1rem; }
.kb-entry .kb-meta {
  font-size: .75rem; color: var(--text-dim);
  margin-bottom: 8px;
}
.kb-entry .kb-section {
  margin: 8px 0;
  font-size: .88rem;
}
.kb-entry .kb-section strong {
  color: var(--text-dim);
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  display: block;
  margin-bottom: 2px;
}
.kb-entry .kb-actions {
  display: flex; gap: 6px; margin-top: 10px;
}

.cli-new-token code {
  display: block;
  background: #1c1917;
  color: #fbbf24;
  padding: 10px;
  border-radius: 6px;
  font-family: "SF Mono", Menlo, Monaco, "Courier New", monospace;
  font-size: .85rem;
  word-break: break-all;
  margin: 10px 0;
}

/* Mobile */
@media (max-width: 768px) {
  .admin-form { grid-template-columns: 1fr; }
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 280px;
    transform: translateX(-100%);
    transition: transform .2s;
    z-index: 100;
  }
  .sidebar.open { transform: translateX(0); box-shadow: var(--shadow); }
  .suggestions { grid-template-columns: 1fr; }
}
