:root {
  /* Surfaces */
  --bg: #0d1424;
  --surface-1: #141d33;
  --surface-2: #1b2643;
  --surface-3: #243152;
  --border: #2c3a5e;
  --border-soft: #1f2a44;

  /* Text */
  --text: #e8eefb;
  --text-dim: #93a1c4;

  /* Brand + accents */
  --primary: #25d366;
  --primary-2: #12b886;
  --grad-primary: linear-gradient(135deg, #2ee27a 0%, #11b3a5 100%);
  --grad-brand: linear-gradient(135deg, #38e07b 0%, #2dd4bf 45%, #60a5fa 100%);
  --blue: #60a5fa;
  --purple: #a78bfa;
  --pink: #f472b6;
  --amber: #fbbf24;
  --red: #fb7185;

  --shadow-soft: 0 6px 20px rgba(0, 0, 0, 0.35);
  --shadow-glow: 0 4px 16px rgba(46, 226, 122, 0.25);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 12% -8%, rgba(96, 165, 250, 0.12), transparent 60%),
    radial-gradient(1000px 500px at 92% 0%, rgba(167, 139, 250, 0.12), transparent 55%),
    radial-gradient(900px 600px at 80% 100%, rgba(45, 212, 191, 0.1), transparent 60%),
    var(--bg);
}
.hidden { display: none !important; }
button {
  cursor: pointer;
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  background: var(--grad-primary);
  color: #04130c;
  font-weight: 700;
  font-size: 14px;
  box-shadow: var(--shadow-glow);
  transition: transform 0.12s ease, box-shadow 0.12s ease, opacity 0.12s ease;
}
button:hover { transform: translateY(-1px); box-shadow: 0 6px 20px rgba(46, 226, 122, 0.35); }
button:active { transform: translateY(0); }
button.secondary {
  background: var(--surface-3);
  color: var(--text);
  box-shadow: none;
}
button.secondary:hover { background: #2c3a5e; box-shadow: none; }
input, textarea {
  font-family: inherit;
  font-size: 14px;
  width: 100%;
  background: var(--surface-1);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(46, 226, 122, 0.15);
}
textarea { resize: vertical; }

/* --- Login --- */
.login-overlay {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background:
    radial-gradient(800px 500px at 30% 20%, rgba(96, 165, 250, 0.18), transparent 60%),
    radial-gradient(700px 500px at 75% 80%, rgba(167, 139, 250, 0.18), transparent 60%),
    var(--bg);
  z-index: 100;
}
.login-card {
  width: 320px; padding: 28px;
  background: var(--surface-1);
  border: 1px solid var(--border-soft);
  border-radius: 16px;
  box-shadow: var(--shadow-soft);
  display: flex; flex-direction: column; gap: 12px;
}
.login-card h2 {
  margin: 0 0 8px;
  background: var(--grad-brand);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
}
.error { color: var(--red); font-size: 13px; min-height: 18px; margin: 0; }

/* --- App layout --- */
.app {
  display: grid;
  grid-template-columns: 340px 1fr;
  height: 100vh;
}
.sidebar {
  background: linear-gradient(180deg, rgba(27, 38, 67, 0.85), rgba(20, 29, 51, 0.85));
  border-right: 1px solid var(--border-soft);
  display: flex; flex-direction: column;
  overflow: hidden;
  backdrop-filter: blur(4px);
}
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border-soft);
}
.sidebar-header h1 {
  margin: 0; font-size: 19px; font-weight: 800;
  background: var(--grad-brand);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
}
.header-actions { display: flex; gap: 6px; }
.header-actions button {
  width: 36px; height: 36px; padding: 0;
  background: var(--surface-3); color: var(--text);
  font-size: 18px; box-shadow: none;
}
.header-actions button:hover { background: #2c3a5e; }
.conversation-list { overflow-y: auto; flex: 1; }
.conversation-list .empty {
  padding: 30px 16px;
  text-align: center;
  color: var(--text-dim);
}

.convo {
  display: flex; align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-soft);
  cursor: pointer;
  gap: 12px;
  transition: background 0.12s ease;
}
.convo:hover { background: rgba(96, 165, 250, 0.08); }
.convo.active { background: rgba(46, 226, 122, 0.12); }
.convo-avatar {
  width: 42px; height: 42px;
  border-radius: 50%;
  background: var(--grad-primary);
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; color: #06150d;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}
/* Rotate avatar colors for a livelier list */
.convo:nth-child(5n+1) .convo-avatar { background: linear-gradient(135deg, #2ee27a, #11b3a5); color: #06150d; }
.convo:nth-child(5n+2) .convo-avatar { background: linear-gradient(135deg, #60a5fa, #6366f1); color: #07112b; }
.convo:nth-child(5n+3) .convo-avatar { background: linear-gradient(135deg, #a78bfa, #ec4899); color: #1c0c2b; }
.convo:nth-child(5n+4) .convo-avatar { background: linear-gradient(135deg, #fbbf24, #fb7185); color: #2b1605; }
.convo:nth-child(5n+5) .convo-avatar { background: linear-gradient(135deg, #2dd4bf, #60a5fa); color: #042220; }
.convo-body { flex: 1; min-width: 0; }
.convo-name {
  font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.convo-last {
  font-size: 13px; color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.convo-badge {
  background: var(--grad-primary); color: #06150d;
  border-radius: 10px;
  font-size: 12px; font-weight: 700;
  padding: 2px 8px;
  box-shadow: var(--shadow-glow);
}

/* --- Chat --- */
.chat {
  display: flex; flex-direction: column;
  background:
    radial-gradient(800px 500px at 80% 10%, rgba(96, 165, 250, 0.06), transparent 60%),
    radial-gradient(700px 500px at 10% 90%, rgba(45, 212, 191, 0.06), transparent 60%),
    var(--bg);
  overflow: hidden;
}
.chat-empty {
  flex: 1;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-dim);
}
.chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px;
  background: linear-gradient(180deg, rgba(27, 38, 67, 0.9), rgba(20, 29, 51, 0.9));
  border-bottom: 1px solid var(--border-soft);
  backdrop-filter: blur(4px);
}
.chat-name { font-weight: 600; font-size: 16px; }
.chat-id { font-size: 12px; color: var(--text-dim); }
.ai-toggle {
  display: flex; align-items: center; gap: 8px;
  cursor: pointer;
  user-select: none;
  font-size: 14px;
}
.ai-toggle input { width: auto; accent-color: var(--primary); }

.messages {
  flex: 1; overflow-y: auto;
  padding: 20px;
  display: flex; flex-direction: column; gap: 6px;
}
.msg {
  max-width: 70%;
  padding: 9px 13px;
  border-radius: 12px;
  position: relative;
  word-wrap: break-word;
  white-space: pre-wrap;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
}
.msg-in { background: var(--surface-2); align-self: flex-start; border-top-left-radius: 4px; }
.msg-out {
  background: linear-gradient(135deg, #0f7a63, #128c7e);
  align-self: flex-end; border-top-right-radius: 4px;
}
.msg-meta {
  display: flex; gap: 8px; align-items: center;
  font-size: 11px; color: var(--text-dim);
  margin-top: 4px;
}
.msg-sender-ai { color: var(--purple); font-weight: 600; }
.msg-sender-agent { color: var(--blue); font-weight: 600; }

.composer {
  background: linear-gradient(180deg, rgba(27, 38, 67, 0.9), rgba(20, 29, 51, 0.9));
  border-top: 1px solid var(--border-soft);
  padding: 12px 16px;
  display: flex; gap: 10px;
  align-items: flex-end;
  backdrop-filter: blur(4px);
}
.composer textarea { min-height: 44px; max-height: 200px; }
.composer-actions { display: flex; gap: 6px; }

/* --- Modal --- */
.modal {
  position: fixed; inset: 0;
  background: rgba(6, 10, 20, 0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
  backdrop-filter: blur(3px);
}
.modal-card {
  background: var(--surface-1);
  border: 1px solid var(--border-soft);
  border-radius: 16px;
  width: 90%; max-width: 600px;
  max-height: 90vh;
  display: flex; flex-direction: column;
  overflow: hidden;
  box-shadow: var(--shadow-soft);
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-soft);
  background: linear-gradient(135deg, rgba(46, 226, 122, 0.1), rgba(96, 165, 250, 0.1));
}
.modal-header h2 { margin: 0; font-size: 16px; }
.modal-header button {
  background: transparent; color: var(--text-dim);
  font-size: 20px; padding: 0; width: 28px; height: 28px;
  box-shadow: none;
}
.modal-header button:hover { color: var(--text); transform: none; }
.modal-body {
  padding: 20px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 16px;
}
.modal-body label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 13px; color: var(--text-dim);
}
.modal-body label.checkbox {
  flex-direction: row; align-items: center; gap: 8px;
  color: var(--text);
}
.modal-body label.checkbox input { width: auto; accent-color: var(--primary); }
.modal-body small { color: var(--text-dim); font-size: 12px; }
.modal-body small.warning { color: var(--amber); }
.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border-soft);
  display: flex; justify-content: flex-end; align-items: center; gap: 12px;
}
.modal-wide { max-width: 640px; }

select {
  font-family: inherit;
  font-size: 14px;
  width: 100%;
  background: var(--surface-1);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(46, 226, 122, 0.15);
}

/* Tabs */
.tabs {
  display: flex;
  gap: 4px;
  padding: 0 20px;
  border-bottom: 1px solid var(--border-soft);
}
.tab {
  background: transparent;
  color: var(--text-dim);
  border-radius: 0;
  padding: 12px 14px;
  font-weight: 600;
  border-bottom: 2px solid transparent;
  box-shadow: none;
}
.tab:hover { color: var(--text); transform: none; box-shadow: none; }
.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.modal-body section { display: flex; flex-direction: column; gap: 16px; }

.row-actions { display: flex; align-items: center; gap: 12px; }
.status { font-size: 13px; }
.status.ok { color: var(--primary); }
.status.err { color: var(--red); }
.status.pending { color: var(--amber); }

.copy-row { display: flex; gap: 6px; }
.copy-row input { flex: 1; }
.copy-row button { white-space: nowrap; }

.help { color: var(--text-dim); font-size: 13px; margin: 0 0 4px; line-height: 1.5; }
.help code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; color: var(--blue); }
small[data-secret].set { color: var(--primary); }

button.small { padding: 4px 10px; font-size: 12px; }
button.danger {
  background: linear-gradient(135deg, #fb7185, #e11d48);
  color: #fff; box-shadow: 0 4px 14px rgba(225, 29, 72, 0.3);
}

.section-title {
  font-size: 13px; text-transform: uppercase; letter-spacing: 0.5px;
  color: var(--text-dim); margin: 8px 0 0;
  display: flex; align-items: center; justify-content: space-between;
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-2 .span-2 { grid-column: 1 / -1; }

/* Built-in tool toggles */
.tool-list { display: flex; flex-direction: column; gap: 8px; }
.tool-item {
  background: var(--surface-2); border: 1px solid var(--border-soft); border-radius: 10px;
  padding: 12px;
  transition: border-color 0.15s ease;
}
.tool-item:hover { border-color: var(--border); }
.tool-item .tool-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.tool-item .tool-name { font-weight: 600; font-family: monospace; color: var(--text); }
.tool-item .tool-desc { font-size: 12px; color: var(--text-dim); margin-top: 4px; line-height: 1.4; }
.tool-item .tool-flags { display: flex; gap: 14px; margin-top: 8px; }
.tool-item .tool-flags label { flex-direction: row; align-items: center; gap: 6px; color: var(--text); font-size: 13px; }
.tool-item .tool-flags input { accent-color: var(--primary); }
.badge-builtin { font-size: 10px; background: rgba(96, 165, 250, 0.18); color: var(--blue); padding: 2px 6px; border-radius: 5px; }
.badge-custom { font-size: 10px; background: rgba(46, 226, 122, 0.18); color: var(--primary); padding: 2px 6px; border-radius: 5px; }

/* Custom action cards */
#custom-actions { display: flex; flex-direction: column; gap: 14px; }
.action-card {
  background: var(--surface-2); border: 1px solid var(--border-soft); border-radius: 10px;
  padding: 12px; display: flex; flex-direction: column; gap: 8px;
}
.action-row { display: flex; gap: 8px; align-items: center; }
.action-row .action-name { flex: 1; font-family: monospace; }
.action-row select { width: auto; }
.action-card label.mini { font-size: 12px; color: var(--text-dim); gap: 4px; }
.action-card textarea { font-family: monospace; font-size: 12px; }
.action-toggles { display: flex; gap: 16px; }

/* Pending approval + tool notes in chat */
.msg-note {
  align-self: center;
  max-width: 85%;
  background: rgba(96, 165, 250, 0.12);
  border: 1px solid rgba(96, 165, 250, 0.25);
  color: #cdddff;
  font-size: 12px;
  text-align: center;
  border-radius: 10px;
}
.msg-tool { align-self: center; max-width: 85%; background: transparent; color: var(--text-dim); font-size: 12px; text-align: center; }
.approval-card {
  align-self: center; max-width: 90%;
  background: linear-gradient(135deg, rgba(251, 191, 36, 0.12), rgba(251, 113, 133, 0.1));
  border: 1px solid rgba(251, 191, 36, 0.4); border-radius: 12px;
  padding: 12px 14px; color: #f6ead0; font-size: 13px;
}
.approval-card .approval-title { font-weight: 700; color: var(--amber); margin-bottom: 4px; }
.approval-card pre {
  background: rgba(0, 0, 0, 0.3); border-radius: 8px; padding: 8px; margin: 8px 0;
  font-size: 12px; overflow-x: auto; white-space: pre-wrap; word-break: break-word;
}
.approval-actions { display: flex; gap: 8px; margin-top: 6px; }
.approval-actions button { padding: 6px 14px; font-size: 13px; }
.approval-actions .reject {
  background: linear-gradient(135deg, #fb7185, #e11d48); color: #fff;
  box-shadow: 0 4px 14px rgba(225, 29, 72, 0.3);
}
