:root {
  --bg: #07090f;
  --bg-2: #0c101a;
  --panel: #101624;
  --panel-2: #161d30;
  --line: #1e2740;
  --text: #e6eaf4;
  --muted: #7c88a6;
  --accent: #f59e0b;
  --green: #22c55e;
  --red: #ef4444;
  --blue: #38bdf8;
  --radius: 14px;
  font-size: 15px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  background: radial-gradient(1200px 600px at 50% -200px, #131b31 0%, var(--bg) 60%);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  min-height: 100vh;
}

/* ---------- Top bar ---------- */
header {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 18px 28px 10px;
}
.kicker {
  font-size: 11px;
  letter-spacing: 2.5px;
  text-transform: uppercase;
  color: var(--muted);
}
h1 { font-size: 26px; font-weight: 650; }
.spacer { flex: 1; }
.pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 12.5px;
  color: var(--muted);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); }
.dot.off { background: var(--red); }
.dot.pulse { animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .3; } }

nav {
  display: flex;
  gap: 6px;
  padding: 8px 28px 16px;
  border-bottom: 1px solid var(--line);
}
nav button {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  padding: 8px 16px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
}
nav button:hover { color: var(--text); }
nav button.active {
  background: var(--panel);
  border-color: var(--line);
  color: var(--text);
}

main { padding: 22px 28px 60px; }
.view { display: none; }
.view.active { display: block; }

/* ---------- Organisation ---------- */
.org-header { margin-bottom: 8px; }
.org-header h2 { font-size: 20px; font-weight: 600; }
.org-header p { color: var(--muted); font-size: 13.5px; margin-top: 4px; }

#orgmap {
  width: 100%;
  background: linear-gradient(180deg, rgba(20,27,49,.35), rgba(7,9,15,0));
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.node { cursor: pointer; }
.node text { pointer-events: none; }
.node-name { fill: var(--text); font-size: 13px; font-weight: 600; text-anchor: middle; }
.node-role { fill: var(--muted); font-size: 10.5px; text-anchor: middle; }
.node-emoji { font-size: 22px; text-anchor: middle; dominant-baseline: central; }
.edge { stroke: var(--line); stroke-width: 1.2; }
.edge.active { stroke: var(--accent); stroke-width: 1.6; filter: drop-shadow(0 0 4px var(--accent)); }
.halo { opacity: .25; }
.node.working .halo { animation: halo 1.1s infinite; }
@keyframes halo { 0%,100% { opacity: .25; } 50% { opacity: .7; } }
.status-badge { font-size: 9.5px; text-anchor: middle; fill: var(--muted); }
.node.working .status-badge { fill: var(--accent); }

/* ---------- Panels / cards ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
}
.card h3 { font-size: 15.5px; display: flex; align-items: center; gap: 10px; }
.card .role { color: var(--muted); font-size: 12.5px; margin: 3px 0 12px 34px; }
.avatar {
  width: 26px; height: 26px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 15px; flex: none;
}
.skill.more { cursor: pointer; color: var(--accent); border-color: var(--accent); }
.skill.more:hover { background: #1a1408; }
#dSkills { max-height: 180px; overflow-y: auto; }
.skill {
  display: inline-block;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 11.5px;
  color: var(--muted);
  margin: 0 6px 6px 0;
}
.card-actions { margin-top: 12px; display: flex; gap: 8px; }

button.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
}
button.btn:hover { border-color: #33405f; }
button.btn.primary { background: var(--accent); border-color: var(--accent); color: #14100a; font-weight: 600; }
button.btn.danger { color: #fca5a5; }
button.btn:disabled { opacity: .5; cursor: default; }

/* ---------- Forms ---------- */
label { display: block; font-size: 12.5px; color: var(--muted); margin: 12px 0 5px; }
input, textarea, select {
  width: 100%;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  padding: 9px 12px;
  font-size: 13.5px;
  font-family: inherit;
}
textarea { resize: vertical; min-height: 70px; }
input:focus, textarea:focus { outline: none; border-color: #3b4a70; }

/* ---------- Missions ---------- */
.missions-layout { display: grid; grid-template-columns: 340px 1fr; gap: 18px; align-items: start; }
.mission-item {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  margin-bottom: 8px;
  cursor: pointer;
  background: var(--panel);
}
.mission-item:hover { border-color: #33405f; }
.mission-item.selected { border-color: var(--accent); }
.mission-item .t { font-weight: 600; font-size: 13.5px; }
.mission-del {
  background: none; border: none; cursor: pointer; font-size: 14px;
  color: var(--muted); padding: 2px 4px; border-radius: 6px; flex: none;
}
.mission-del:hover { background: #2a1215; }
.mission-del.armed, .btn.armed {
  background: var(--red) !important;
  color: #fff !important;
  border-color: var(--red) !important;
  font-size: 11px;
}

#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translate(-50%, 20px);
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 11px 20px;
  border-radius: 12px;
  font-size: 13.5px;
  max-width: 70vw;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 200;
  box-shadow: 0 8px 30px rgba(0,0,0,.5);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.cat-form { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 4px; }
#toast.error { border-color: var(--red); color: #fca5a5; }
.mission-item .s { color: var(--muted); font-size: 11.5px; margin-top: 3px; }
.badge {
  font-size: 10.5px; padding: 2px 8px; border-radius: 999px; margin-left: 6px;
  border: 1px solid var(--line); color: var(--muted); vertical-align: middle;
}
.badge.running { color: var(--accent); border-color: var(--accent); }
.badge.done { color: var(--green); border-color: var(--green); }
.badge.failed, .badge.stopped { color: var(--red); border-color: var(--red); }

#missionLog { display: flex; flex-direction: column; gap: 12px; max-height: 68vh; overflow-y: auto; padding-right: 6px; }
.evt { display: flex; gap: 10px; align-items: flex-start; }
.evt .avatar { margin-top: 2px; }
.evt .bubble {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13.5px;
  line-height: 1.55;
  max-width: 80%;
  overflow-wrap: anywhere;
}
.evt .meta { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.evt.deliverable .bubble { border-color: #14532d; background: #0b1a12; }
.evt.final .bubble { border-color: var(--accent); background: #1a1408; }
.evt.error .bubble { border-color: var(--red); }
.evt.system .bubble { color: var(--muted); font-style: italic; }
.evt.typing .bubble { border-style: dashed; color: #b7c1da; }
.caret { display: inline-block; width: 7px; background: var(--accent); animation: pulse .8s infinite; }

.bubble h1, .bubble h2, .bubble h3 { font-size: 14px; margin: 10px 0 4px; }
.bubble ul { margin: 6px 0 6px 18px; }
.bubble code { background: var(--bg-2); padding: 1px 5px; border-radius: 5px; font-size: 12px; }
.bubble p { margin: 6px 0; }

/* ---------- Drawer (agent detail / chat) ---------- */
#drawer {
  position: fixed; top: 0; right: -480px; width: 460px; height: 100vh;
  background: var(--bg-2);
  border-left: 1px solid var(--line);
  transition: right .25s ease;
  z-index: 50;
  display: flex; flex-direction: column;
}
#drawer.open { right: 0; }
#drawer .head { padding: 20px 22px 14px; border-bottom: 1px solid var(--line); }
#drawer .body { flex: 1; overflow-y: auto; padding: 18px 22px; }
#drawer .chatbox { border-top: 1px solid var(--line); padding: 14px 22px; display: flex; gap: 8px; }
#drawer .chatbox input { flex: 1; }
.chat-msg { margin-bottom: 12px; font-size: 13.5px; line-height: 1.55; }
.chat-msg .who { font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.chat-msg.me .bubble2 { background: var(--panel-2); }
.bubble2 {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 9px 13px;
  overflow-wrap: anywhere;
}
.close-x {
  position: absolute; top: 16px; right: 18px;
  background: none; border: none; color: var(--muted);
  font-size: 20px; cursor: pointer;
}

/* ---------- Modal ---------- */
#modalWrap {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: none; align-items: center; justify-content: center; z-index: 100;
}
#modalWrap.open { display: flex; }
#modal {
  width: 560px; max-width: 94vw; max-height: 88vh; overflow-y: auto;
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 24px;
}
#modal h3 { margin-bottom: 4px; }

.banner {
  background: #1a1408; border: 1px solid var(--accent); color: #fbbf24;
  padding: 10px 16px; border-radius: 10px; font-size: 13px; margin: 0 28px 14px;
}
.hidden { display: none !important; }

@media (max-width: 900px) {
  .missions-layout { grid-template-columns: 1fr; }
  #drawer { width: 100vw; right: -100vw; }
}
