:root {
  --bg: #0b0f17;
  --bg-elev: #121826;
  --bg-card: #182032;
  --bg-hover: #212c42;
  --border: #26324a;
  --text: #eef2ff;
  --muted: #94a3b8;
  --primary: #4f8cff;
  --primary-hover: #3b78f0;
  --danger: #ef4444;
  --success: #22c55e;
  --warn: #f59e0b;
  --radius: 14px;
  --sidebar-w: 295px;
}

* { box-sizing: border-box; }

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

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mt-sm { margin-top: 10px; }

code {
  background: rgba(255, 255, 255, 0.08);
  padding: 2px 6px;
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  color: var(--text);
  background: var(--bg-card);
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:hover { background: var(--bg-hover); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-full { width: 100%; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--bg-card); border-color: var(--border); }
.btn-danger { background: rgba(239, 68, 68, 0.16); color: #fca5a5; border-color: rgba(239, 68, 68, 0.35); }
.btn-danger:hover { background: rgba(239, 68, 68, 0.28); }
.btn-google { background: #fff; color: #111827; width: 100%; padding: 12px 16px; }
.btn-google:hover { background: #f3f4f6; }
.btn-xs { padding: 5px 9px; font-size: 12px; border-radius: 8px; }
.btn-sm { padding: 7px 11px; font-size: 13px; }
.icon-btn {
  width: 38px; height: 38px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg-card); color: var(--text); cursor: pointer;
  font-size: 16px; flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg-hover); }

/* Login screen */
.login-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  width: 100%;
  max-width: 410px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}
.login-card h1 { margin: 0; font-size: 22px; }
.brand-badge {
  width: 48px; height: 48px; border-radius: 14px;
  background: linear-gradient(135deg, #4f8cff, #7c3aed);
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 800; letter-spacing: 0.5px; color: #fff;
}
.brand-badge.lg { width: 64px; height: 64px; font-size: 20px; margin-bottom: 6px; }
.login-form { display: flex; flex-direction: column; gap: 10px; }
.login-form label { font-size: 13px; color: var(--muted); }
.login-form input, .code-submit-row input, .terminal-input-row input, .sidebar-search input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  color: var(--text);
  font-size: 15px;
  outline: none;
}
.login-form input:focus, .sidebar-search input:focus, .code-submit-row input:focus {
  border-color: var(--primary);
}

.alert {
  padding: 11px 13px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.45;
  word-break: break-word;
}
.alert-error { background: rgba(239, 68, 68, 0.14); border: 1px solid rgba(239, 68, 68, 0.35); color: #fecaca; }
.alert-info { background: rgba(79, 140, 255, 0.12); border: 1px solid rgba(79, 140, 255, 0.35); color: #dbeafe; }

/* App Shell */
.app-shell {
  display: flex;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
  position: relative;
}

.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 30;
}
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 10px;
}
.sidebar-brand {
  display: flex; align-items: center; gap: 9px;
  font-weight: 700; font-size: 16px;
}
.brand-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: linear-gradient(135deg, #4f8cff, #a855f7);
}
.sidebar-actions {
  display: flex; gap: 8px; padding: 0 12px 10px;
}
.btn-icon-text { flex-shrink: 0; padding: 10px 10px; }
.sidebar-search { padding: 0 12px 10px; }
.sidebar-search input { padding: 8px 11px; font-size: 13px; }

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.chat-item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 10px;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.12s;
}
.chat-item:hover { background: var(--bg-card); }
.chat-item.active { background: rgba(79, 140, 255, 0.14); border-color: rgba(79, 140, 255, 0.35); }
.chat-item-body { min-width: 0; flex: 1; }
.chat-item-title {
  font-size: 14px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-item-meta {
  display: flex; align-items: center; gap: 6px;
  margin-top: 3px; font-size: 11px; color: var(--muted);
}
.ws-pill {
  background: rgba(255, 255, 255, 0.07);
  padding: 1px 6px; border-radius: 99px;
  max-width: 135px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-delete-btn {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; padding: 4px 6px; border-radius: 6px; font-size: 13px;
}
.chat-delete-btn:hover { color: #fca5a5; background: rgba(239, 68, 68, 0.15); }

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 8px;
}
.agy-status-card {
  width: 100%; text-align: left;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 10px; padding: 9px 11px; cursor: pointer; color: var(--text);
}
.agy-status-card:hover { background: var(--bg-hover); }
.status-row { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; }
.status-sub { font-size: 11px; color: var(--muted); margin-top: 3px; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.dot-ok { background: var(--success); box-shadow: 0 0 8px rgba(34, 197, 94, 0.6); }
.dot-warn { background: var(--warn); }
.dot-err { background: var(--danger); }

.user-card {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 6px 4px 0;
}
.user-info { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1; }
.user-avatar, .user-avatar-fallback {
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0; object-fit: cover;
}
.user-avatar-fallback {
  background: var(--bg-hover); display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 13px;
}
.user-meta { min-width: 0; }
.user-name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-email { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-actions { display: flex; gap: 5px; flex-shrink: 0; }

/* Main chat area */
.main-area {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.topbar {
  min-height: 58px;
  border-bottom: 1px solid var(--border);
  background: rgba(11, 15, 23, 0.92);
  backdrop-filter: blur(10px);
  padding: 8px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.topbar-title-wrap { min-width: 0; flex: 1; }
.topbar-title {
  font-weight: 700; font-size: 15px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar-meta { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-controls { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.compact-select {
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 7px 8px;
  font-size: 12px;
  max-width: 145px;
  outline: none;
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px 24px;
  -webkit-overflow-scrolling: touch;
}
.empty-state {
  max-width: 440px;
  margin: 12vh auto 0;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.empty-state h2 { margin: 0; font-size: 22px; }
.quick-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-top: 8px; }

.messages-list {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.msg-row { display: flex; flex-direction: column; max-width: 100%; }
.msg-row.user { align-items: flex-end; }
.msg-row.assistant { align-items: flex-start; }

.msg-bubble {
  max-width: min(760px, 94%);
  padding: 12px 15px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.55;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg-row.user .msg-bubble {
  background: #1d4ed8;
  color: #fff;
  border-bottom-right-radius: 5px;
  white-space: pre-wrap;
}
.msg-row.assistant .msg-bubble {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
  width: min(760px, 100%);
}
.msg-bubble p { margin: 0 0 10px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble ul, .msg-bubble ol { margin: 6px 0 10px; padding-left: 20px; }
.msg-bubble a { color: #7cb3ff; }
.msg-bubble pre {
  background: #070a10;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  overflow-x: auto;
  margin: 10px 0;
  position: relative;
}
.msg-bubble pre code { background: transparent; padding: 0; font-size: 13px; color: #e2e8f0; }

.msg-details {
  margin-bottom: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.02);
  font-size: 12px;
}
.msg-details summary {
  padding: 7px 10px;
  cursor: pointer;
  color: var(--muted);
  font-weight: 600;
}
.msg-details-body {
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  max-height: 220px;
  overflow-y: auto;
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: #cbd5e1;
}
.tool-item {
  padding: 4px 0;
  border-bottom: 1px dashed rgba(255, 255, 255, 0.08);
}
.tool-item:last-child { border-bottom: none; }
.msg-time { font-size: 11px; color: var(--muted); margin-top: 4px; padding: 0 4px; }

/* Composer */
.composer-wrap {
  border-top: 1px solid var(--border);
  background: var(--bg-elev);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
}
.stream-status-bar {
  max-width: 860px;
  margin: 0 auto 8px;
  display: flex; align-items: center; gap: 10px;
  padding: 7px 12px;
  border-radius: 10px;
  background: rgba(79, 140, 255, 0.12);
  border: 1px solid rgba(79, 140, 255, 0.3);
  font-size: 13px;
}
.stream-status-bar span:nth-child(2) { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spinner {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-top-color: var(--primary);
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

.composer {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 6px 6px 6px 12px;
}
.composer:focus-within { border-color: var(--primary); }
.composer textarea {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.45;
  max-height: 160px;
  resize: none;
  outline: none;
  padding: 6px 0;
}
.btn-send { width: 42px; height: 42px; padding: 0; border-radius: 11px; flex-shrink: 0; font-size: 17px; }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.72);
  display: flex; align-items: center; justify-content: center;
  padding: 14px;
  z-index: 60;
}
.modal-card {
  width: 100%; max-width: 600px; max-height: 90dvh;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 18px;
  display: flex; flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-body {
  padding: 16px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 14px;
}
.agy-info-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  font-size: 13px;
  display: flex; flex-direction: column; gap: 5px;
}
.modal-btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.oauth-box {
  background: rgba(79, 140, 255, 0.1);
  border: 1px solid rgba(79, 140, 255, 0.35);
  border-radius: 12px;
  padding: 12px;
  font-size: 13px;
}
.oauth-box p { margin: 0 0 8px; }
.code-submit-row, .terminal-input-row { display: flex; gap: 8px; }

.terminal-wrap {
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  background: #060910;
}
.terminal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--muted);
}
.terminal-out {
  margin: 0;
  padding: 10px 12px;
  height: 210px;
  overflow-y: auto;
  font-size: 12px;
  line-height: 1.45;
  color: #d1d5db;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.terminal-input-row { padding: 8px; border-top: 1px solid var(--border); }

/* Mobile */
.mobile-only { display: none; }
.sidebar-backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); z-index: 25;
}
@media (max-width: 820px) {
  .mobile-only { display: inline-flex; }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    width: min(85vw, 310px);
  }
  .sidebar.open { transform: translateX(0); }
  .compact-select { max-width: 102px; font-size: 11px; padding: 6px; }
}
