:root {
  --bg: #0b0e14;
  --bg-2: #121723;
  --bg-3: #1a2030;
  --panel: #151b27;
  --border: #232c3d;
  --text: #e6ebf4;
  --text-dim: #97a3b6;
  --text-faint: #6b7689;
  --accent: #6c8cff;
  --accent-2: #8b5cf6;
  --green: #34d399;
  --amber: #fbbf24;
  --red: #f87171;
  --radius: 12px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

.hidden {
  display: none !important;
}

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--bg-3);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 9px;
  font-size: 14px;
  cursor: pointer;
  transition: filter 0.15s, background 0.15s, border 0.15s;
}
.btn:hover {
  filter: brightness(1.15);
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: #fff;
  font-weight: 600;
}
.btn-danger {
  background: transparent;
  border-color: var(--red);
  color: var(--red);
}
.btn-danger:hover {
  background: rgba(248, 113, 113, 0.12);
}
.btn-ghost {
  background: transparent;
}
.btn-block {
  width: 100%;
}
.btn-sm {
  padding: 6px 10px;
  font-size: 13px;
}
.icon-btn {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 9px;
  width: 42px;
  cursor: pointer;
  color: var(--text-dim);
}

/* ---------- Gate ---------- */
.gate {
  height: 100%;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 30% 20%, #1a2238 0%, var(--bg) 60%);
  padding: 20px;
}
.gate-card {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  box-shadow: var(--shadow);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}
.brand h1 {
  font-size: 20px;
  margin: 0;
}
.brand.small {
  font-weight: 600;
  font-size: 15px;
}
.logo {
  color: var(--accent);
  font-size: 22px;
}
.logo.big {
  font-size: 54px;
  opacity: 0.4;
}
.subtitle {
  color: var(--text-dim);
  font-size: 14px;
  margin: 14px 0 22px;
}
label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.key-row {
  display: flex;
  gap: 8px;
}
input[type="text"],
input[type="password"],
input[type="search"],
textarea,
select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
}
input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--accent);
}
.remember {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 14px 0 18px;
  font-size: 13px;
}
.remember input {
  width: auto;
}
.gate-msg {
  min-height: 18px;
  font-size: 13px;
  margin: 12px 0 0;
}
.gate-msg.error {
  color: var(--red);
}
.gate-msg.ok {
  color: var(--green);
}
.hint {
  color: var(--text-faint);
  font-size: 12px;
  margin-top: 18px;
  line-height: 1.5;
}

/* ---------- App layout ---------- */
.app {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: 100%;
}
.sidebar {
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}
.toolbar {
  padding: 12px 16px;
  display: flex;
  gap: 10px;
  align-items: center;
  border-bottom: 1px solid var(--border);
}
.filter {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  white-space: nowrap;
  margin: 0;
}
.filter input {
  width: auto;
}
.agent-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}
.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
}
.sidebar-nav .btn {
  text-align: left;
}
.sidebar-foot {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

/* ---------- Modal list rows (KB / phone managers) ---------- */
.kb-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 260px;
  overflow-y: auto;
}
.row-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-2);
}
.row-item .name {
  font-weight: 600;
  font-size: 14px;
}
.row-item .meta {
  color: var(--text-faint);
  font-size: 12px;
  margin-top: 3px;
  font-family: ui-monospace, monospace;
}
.check-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 8px 0;
  margin: 0;
  cursor: pointer;
}
.check-row input {
  width: auto;
  margin-top: 3px;
}
.check-row .name {
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
}
.check-row .meta {
  color: var(--text-faint);
  font-size: 12px;
  font-family: ui-monospace, monospace;
}
.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 20px 0 16px;
}
.modal-body h4 {
  margin: 0 0 12px;
  font-size: 14px;
}

.agent-item {
  padding: 12px;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
  margin-bottom: 4px;
}
.agent-item:hover {
  background: var(--bg-3);
}
.agent-item.active {
  background: var(--bg-3);
  border-color: var(--accent);
}
.agent-item .name {
  font-weight: 600;
  font-size: 14px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.agent-item .meta {
  color: var(--text-faint);
  font-size: 12px;
  margin-top: 4px;
}

.badge {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 7px;
  border-radius: 999px;
  white-space: nowrap;
}
.badge.flow {
  background: rgba(108, 140, 255, 0.16);
  color: var(--accent);
}
.badge.llm {
  background: rgba(52, 211, 153, 0.14);
  color: var(--green);
}
.badge.other {
  background: rgba(150, 163, 182, 0.14);
  color: var(--text-dim);
}

/* ---------- Detail ---------- */
.detail {
  overflow-y: auto;
  padding: 28px 36px;
  min-height: 0;
}
.empty-state {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--text-faint);
}
.detail-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 20px;
}
.detail-head h2 {
  margin: 0 0 6px;
  font-size: 22px;
}
.detail-head .sub {
  color: var(--text-faint);
  font-size: 12px;
  font-family: ui-monospace, monospace;
}
.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 18px;
}
.card h3 {
  margin: 0 0 14px;
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.kv {
  display: grid;
  grid-template-columns: 180px 1fr;
  gap: 8px 16px;
  font-size: 13px;
}
.kv dt {
  color: var(--text-dim);
}
.kv dd {
  margin: 0;
  word-break: break-word;
}

.node {
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 12px;
  overflow: hidden;
}
.node-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--bg-3);
}
.node-head .node-name {
  font-weight: 600;
  font-size: 14px;
}
.node-head .node-id {
  color: var(--text-faint);
  font-size: 11px;
  font-family: ui-monospace, monospace;
}
.node-type {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(139, 92, 246, 0.16);
  color: var(--accent-2);
  font-weight: 700;
}
.node-body {
  padding: 14px;
}
.node-body textarea {
  min-height: 90px;
  resize: vertical;
  line-height: 1.5;
}
.node-kb {
  margin-top: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 10px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: var(--bg-2);
}
.node-kb-label {
  color: var(--text-dim);
  font-size: 12px;
}
.node-kb-ids {
  font-size: 12px;
  font-family: ui-monospace, monospace;
  color: var(--accent);
  word-break: break-all;
}
.node-kb-badge {
  margin-left: auto;
}
.node-edges {
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-dim);
}
.node-edges .edge {
  padding: 4px 0;
  border-top: 1px dashed var(--border);
}
.node-edges .arrow {
  color: var(--accent);
}

textarea.global-prompt {
  min-height: 80px;
  resize: vertical;
  line-height: 1.5;
}

.loading {
  color: var(--text-dim);
  font-size: 14px;
  padding: 20px;
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 20px;
}
.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
  max-width: 520px;
  box-shadow: var(--shadow);
  max-height: 88vh;
  overflow-y: auto;
}
.modal-head {
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-head h3 {
  margin: 0;
  font-size: 16px;
}
.modal-body {
  padding: 22px;
}
.modal-body .field {
  margin-bottom: 14px;
}
.modal-foot {
  padding: 16px 22px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
.field-hint {
  font-size: 12px;
  color: var(--text-faint);
  margin-top: 4px;
}

/* ---------- Toast ---------- */
.toast-root {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 100;
}
.toast {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  padding: 12px 16px;
  border-radius: 9px;
  font-size: 13px;
  box-shadow: var(--shadow);
  animation: slidein 0.2s ease;
  max-width: 340px;
}
.toast.success {
  border-left-color: var(--green);
}
.toast.error {
  border-left-color: var(--red);
}
@keyframes slidein {
  from {
    transform: translateX(20px);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--text-faint);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
