:root {
  --bg: #0b0d10;
  --surface: #12161c;
  --raised: #1a2029;
  --border: #2a3340;
  --border-strong: #3a4656;
  --fg: #e8edf4;
  --muted: #9aa7b8;
  --subtle: #6b7888;
  --accent: #6ee7b7;
  --ok: #4ade80;
  --warn: #fbbf24;
  --danger: #f87171;
  --radius: 10px;
  --font: "IBM Plex Sans", system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
#app { height: 100%; display: flex; flex-direction: column; min-height: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--fg);
  line-height: 1.45;
}
button, input, textarea, select {
  font: inherit;
  color: inherit;
}
.hidden { display: none !important; }

.topbar {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand strong { display: block; font-size: 14px; }
.brand .sub { display: block; font-size: 12px; color: var(--muted); }
.logo {
  width: 32px; height: 32px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  position: relative;
}
.logo::before {
  content: "";
  position: absolute; inset: 6px;
  border: 2px solid var(--accent);
  border-radius: 50%;
}
.logo::after {
  content: "";
  position: absolute; top: 12px; left: 12px;
  width: 8px; height: 8px;
  background: var(--accent);
  border-radius: 50%;
}

.layout {
  display: grid;
  grid-template-columns: 240px 1fr 280px;
  height: calc(100vh - 56px);
  min-height: 0;
}
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar, .delegacao { display: none; }
}

.sidebar, .delegacao {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.delegacao { border-right: none; border-left: 1px solid var(--border); padding: 12px; }
.label {
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--subtle);
  margin: 12px 12px 8px;
  font-weight: 600;
}
.sessions { flex: 1; overflow: auto; padding: 0 8px 8px; }
.session-btn {
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--muted);
  cursor: pointer;
  margin-bottom: 4px;
}
.session-btn:hover { background: var(--surface); color: var(--fg); }
.session-btn.active { background: var(--raised); color: var(--fg); }

.side-nav {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 4px;
  border-top: 1px solid var(--border);
  padding: 8px;
}
.nav-btn {
  background: transparent;
  border: none;
  border-radius: 8px;
  padding: 10px 4px;
  font-size: 11px;
  color: var(--muted);
  cursor: pointer;
}
.nav-btn.active { background: var(--raised); color: var(--fg); }

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}
.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  padding: 0 12px;
}
.tab {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  padding: 12px 12px;
  cursor: pointer;
}
.tab.active { color: var(--fg); border-bottom-color: var(--accent); }

.view {
  flex: 1;
  overflow: auto;
  padding: 20px 24px;
  min-height: 0;
}
/* Chat: só as mensagens rolam; caixa de escrever sempre visível em baixo */
#view-chat.view {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  padding: 0;
}
#view-chat .messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 20px 24px 16px;
  max-width: none;
  width: 100%;
}
#view-chat .messages-inner {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
#view-chat .composer {
  flex: 0 0 auto;
  position: relative;
  max-width: none;
  width: 100%;
  margin: 0;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));
  background: var(--bg);
  border-top: 1px solid var(--border);
  box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.25);
  z-index: 20;
}
#view-chat .composer-inner {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}
#view-chat .composer-row {
  display: flex;
  gap: 8px;
  width: 100%;
  align-items: flex-end;
}
.bubble-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}
.edit-hint {
  margin: 0;
  font-size: 12px;
  color: var(--accent);
}
.view h1 { font-size: 18px; margin: 0 0 6px; }
.muted { color: var(--muted); font-size: 14px; }
.small { font-size: 12px; }

.messages {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 200px;
}
.bubble {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 14px;
  white-space: pre-wrap;
}
.bubble.user { background: var(--raised); }
.bubble .meta { margin-top: 10px; font-size: 12px; color: var(--subtle); }
.welcome {
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 20px;
  color: var(--muted);
}
.welcome strong { color: var(--fg); display: block; margin-bottom: 8px; font-size: 16px; }

.composer {
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  gap: 8px;
  max-width: 720px;
  margin: 0 auto;
  width: 100%;
}
.composer textarea {
  flex: 1;
  resize: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  outline: none;
  min-height: 48px;
  max-height: 140px;
}
.composer textarea:focus { border-color: var(--border-strong); box-shadow: 0 0 0 2px rgba(110, 231, 183, 0.2); }

.bubble {
  position: relative;
}
.bubble-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}
.bubble-actions .chip {
  font-size: 11px;
  padding: 4px 10px;
  opacity: 0.85;
}
.bubble-actions .chip:hover { opacity: 1; }
.edit-hint {
  font-size: 11px;
  color: var(--subtle);
  margin: 0 0 6px;
}

.btn {
  background: var(--accent);
  color: #0b0d10;
  border: none;
  border-radius: 8px;
  padding: 10px 14px;
  font-weight: 600;
  cursor: pointer;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.secondary {
  background: var(--raised);
  color: var(--fg);
  border: 1px solid var(--border);
}
.btn.icon {
  width: 44px;
  height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
}

#deleg-list { list-style: none; margin: 12px 0 0; padding: 0; }
#deleg-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  font-size: 14px;
}
.dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: rgba(107, 120, 136, 0.5);
}
.dot.running { background: var(--accent); animation: pulse 1s infinite; }
.dot.done { background: var(--ok); }
.dot.failed { background: var(--danger); }
.dot.queued { background: var(--warn); }
@keyframes pulse { 50% { opacity: 0.4; } }
.st { margin-left: auto; font-size: 11px; color: var(--subtle); }

.agent-grid {
  list-style: none;
  padding: 0;
  margin: 16px 0 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}
.agent-grid li {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 12px;
  padding: 14px;
}
.agent-grid strong { display: block; margin-bottom: 4px; }
.agent-grid span { font-size: 13px; color: var(--muted); }

.stack { display: flex; flex-direction: column; gap: 10px; max-width: 480px; }
.stack input, .stack textarea, .stack select, .card input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  outline: none;
}
.card {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 12px;
  padding: 16px;
  margin-top: 16px;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.card h2 { margin: 0; font-size: 15px; }
.mt { margin-top: 16px; }
.file-card {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 12px;
  margin-top: 12px;
  overflow: hidden;
}
.file-card header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
}
.file-card pre {
  margin: 0;
  padding: 14px;
  overflow: auto;
  max-height: 360px;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
}
.plan-step {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 12px;
  padding: 14px;
  margin-top: 10px;
}
.plan-step .idx {
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--subtle);
  font-weight: 600;
}
.error {
  border: 1px solid rgba(248, 113, 113, 0.35);
  background: rgba(248, 113, 113, 0.1);
  color: var(--danger);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 14px;
}
.thinking {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 12px;
  padding: 12px 14px;
  color: var(--muted);
  font-size: 14px;
}

/* Auth */
.auth-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(110, 231, 183, 0.12), transparent),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 16px;
  padding: 24px;
}
.auth-brand { margin-bottom: 20px; }
.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 4px;
  margin-bottom: 16px;
  background: var(--bg);
  border-radius: 10px;
  padding: 4px;
}
.auth-tab {
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  padding: 8px 6px;
  font-size: 13px;
  cursor: pointer;
}
.auth-tab.active {
  background: var(--raised);
  color: var(--fg);
  font-weight: 600;
}
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.field input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 11px 12px;
  color: var(--fg);
  outline: none;
}
.field input:focus {
  border-color: var(--border-strong);
  box-shadow: 0 0 0 2px rgba(110, 231, 183, 0.2);
}
.btn.block { width: 100%; }
.auth-error {
  color: var(--danger);
  font-size: 13px;
  margin: 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: rgba(248, 113, 113, 0.1);
  border: 1px solid rgba(248, 113, 113, 0.3);
}
.auth-ok {
  color: var(--ok);
  font-size: 13px;
  margin: 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: rgba(74, 222, 128, 0.1);
  border: 1px solid rgba(74, 222, 128, 0.3);
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* Live progress */
.thinking.live {
  border-color: rgba(110, 231, 183, 0.35);
  background: linear-gradient(180deg, rgba(110, 231, 183, 0.06), var(--surface));
}
.live-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.live-head strong { flex: 1; font-size: 14px; color: var(--fg); }
.timer {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--subtle);
}
.pulse-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1s infinite;
}
.live-log {
  margin: 0;
  padding-left: 18px;
  color: var(--muted);
  font-size: 13px;
}
.live-log li { margin-bottom: 4px; }

.file-actions, .chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}
.chip {
  border: 1px solid var(--border);
  background: var(--raised);
  color: var(--fg);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
}
.chip:hover { border-color: var(--border-strong); }
.chip.accent {
  background: rgba(110, 231, 183, 0.15);
  border-color: rgba(110, 231, 183, 0.4);
  color: var(--accent);
}
.chip.suggest { text-align: left; max-width: 100%; }
.suggestions { margin-top: 12px; }
.row-actions { display: flex; gap: 6px; }
