:root {
  --bg: #0f0f12;
  --bg-elevated: #1a1a22;
  --bg-bubble-user: #3b2a5c;
  --bg-bubble-asst: #22222c;
  --text: #f2f0f7;
  --text-muted: #9a96a8;
  --accent: #c084fc;
  --accent-dim: #7c3aed;
  --error: #f87171;
  --border: #2a2a36;
  --radius: 16px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  /* Centered chat column (ChatGPT-style) */
  --chat-max: 720px;
  --chat-pad: max(16px, calc((100% - var(--chat-max)) / 2));
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden; /* page never scrolls; only the message list does */
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
}
#app {
  height: 100%;
  height: 100dvh;
  overflow: hidden;
}

.view {
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Critical: .view { display:flex } would otherwise override the hidden attribute */
.view[hidden],
[hidden] {
  display: none !important;
}

/* —— Login —— */
#login-view {
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: auto;
  background:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(124, 58, 237, 0.35), transparent),
    var(--bg);
}
.login-card {
  width: min(400px, 100%);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 32px 28px;
  box-shadow: 0 24px 60px rgba(0,0,0,0.45);
}
.brand { text-align: center; margin-bottom: 28px; }
.brand .logo {
  display: inline-block;
  width: 48px;
  height: 48px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(109, 40, 217, 0.35);
}
.brand h1 { margin: 12px 0 4px; font-weight: 650; letter-spacing: -0.02em; }
.brand .sub { margin: 0; color: var(--text-muted); font-size: 0.9rem; line-height: 1.4; }

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
  font-size: 0.85rem;
  color: var(--text-muted);
}
.field input, .composer textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 12px;
  padding: 12px 14px;
  font: inherit;
  outline: none;
}
.field input:focus, .composer textarea:focus {
  border-color: var(--accent-dim);
  box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.25);
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 12px 18px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.primary {
  width: 100%;
  background: linear-gradient(135deg, var(--accent-dim), #a855f7);
  color: white;
}
.btn.ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  padding: 8px 12px;
  font-size: 0.85rem;
}
.error {
  color: var(--error);
  font-size: 0.85rem;
  margin: 12px 0 0;
  text-align: center;
}

/* —— Chat —— */
#chat-view {
  /* Fill viewport: header + scrollable messages + composer */
  min-height: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px var(--chat-pad);
  border-bottom: 1px solid var(--border);
  background: rgba(15, 15, 18, 0.95);
  flex-shrink: 0;
  z-index: 2;
}
.topbar-title { display: flex; align-items: baseline; gap: 8px; }
.status { color: var(--text-muted); font-size: 0.8rem; }

.messages {
  flex: 1 1 auto;
  min-height: 0; /* allow flex child to shrink and scroll */
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px var(--chat-pad) 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  /* no smooth on initial jump — we pin to bottom instantly */
}

.bubble {
  max-width: min(100%, 560px);
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.95rem;
}
.bubble.user {
  align-self: flex-end;
  background: var(--bg-bubble-user);
  border-bottom-right-radius: 4px;
}
.bubble.assistant {
  align-self: flex-start;
  background: var(--bg-bubble-asst);
  border-bottom-left-radius: 4px;
  border: 1px solid var(--border);
}
.bubble .meta {
  display: block;
  margin-top: 6px;
  font-size: 0.7rem;
  color: var(--text-muted);
}
.bubble.autonomy { border-color: rgba(192, 132, 252, 0.35); }
.reasoning {
  font-size: 0.78rem;
  line-height: 1.35;
  color: var(--text-muted);
  border-left: 2px solid var(--accent);
  padding-left: 8px;
  margin-bottom: 8px;
  white-space: pre-wrap;
}
.bubble.admin {
  align-self: center;
  max-width: min(100%, 640px);
  background: transparent;
  border: 1px dashed var(--accent);
  color: var(--text);
  font-size: 0.9rem;
}
.bubble { position: relative; }
.bubble .bubble-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  opacity: 0.7;
}
.bubble:hover .bubble-actions,
.bubble:focus-within .bubble-actions,
body.debug .bubble .bubble-actions {
  opacity: 1;
}
.bubble .bubble-actions button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 8px;
  font-size: 0.72rem;
  padding: 3px 8px;
  cursor: pointer;
}
.bubble .bubble-actions button:hover { color: var(--text); border-color: var(--accent); }

.topbar-actions { display: flex; gap: 6px; align-items: center; }
body.debug #dev-toggle { color: var(--accent); border-color: var(--accent); }

.admin-row {
  width: 100%;
  max-width: var(--chat-max);
  display: flex;
  gap: 8px;
  align-items: flex-end;
  margin-bottom: 8px;
}
.admin-row textarea {
  flex: 1;
  resize: none;
  max-height: 100px;
  min-height: 36px;
  border-radius: 12px;
  border: 1px dashed var(--accent);
  background: transparent;
  color: var(--text);
  padding: 8px 10px;
  font-family: inherit;
}
.composer { flex-direction: column; align-items: center; }
.bubble.thinking {
  opacity: 0.7;
  font-style: italic;
  color: var(--text-muted);
}

.composer {
  display: flex;
  justify-content: center;
  flex-shrink: 0;
  padding: 12px var(--chat-pad) calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg-elevated);
}

/* Inner row sits in the same ~720px column as the messages */
.composer-inner {
  width: 100%;
  max-width: var(--chat-max);
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 8px 10px 8px 14px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.25);
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 160px;
  min-height: 40px;
  line-height: 1.4;
  border: none;
  background: transparent;
  padding: 8px 4px;
  box-shadow: none;
}
.composer textarea:focus {
  border: none;
  box-shadow: none;
}
.composer .btn.primary {
  width: auto;
  min-width: 80px;
  flex-shrink: 0;
  border-radius: 12px;
  padding: 10px 16px;
}

@media (max-width: 520px) {
  :root {
    --chat-max: 100%;
    --chat-pad: 12px;
  }
  .bubble { max-width: 92%; font-size: 0.92rem; }
  .composer-inner { border-radius: 14px; }
}

.btn.ghost.on {
  color: var(--accent);
  border-color: var(--accent);
}

.mic-btn {
  flex-shrink: 0;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.mic-btn:hover { color: var(--accent); border-color: var(--accent-dim); }
.mic-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.mic-btn.recording {
  color: #fff;
  background: var(--error);
  border-color: var(--error);
  animation: mic-pulse 1.4s ease-in-out infinite;
}
.mic-btn.busy { opacity: 0.6; cursor: progress; }
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(248, 113, 113, 0.5); }
  50% { box-shadow: 0 0 0 8px rgba(248, 113, 113, 0); }
}

/* Developer mode: context start */
.ctx-banner {
  width: 100%;
  max-width: var(--chat-max);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
  padding: 6px 6px 6px 12px;
  border: 1px dashed var(--accent);
  border-radius: 12px;
  font-size: 0.8rem;
  color: var(--accent);
}
.ctx-banner .btn.ghost { padding: 4px 10px; font-size: 0.75rem; }
body.debug .bubble.out-of-context { opacity: 0.35; }
body.debug .bubble.ctx-start { box-shadow: 0 -3px 0 0 var(--accent); }
